From: Robert Taylor [taylor_robert@jpmorgan.com] Sent: Friday, October 20, 2000 5:36 AM To: Info-VAX@Mvb.Saic.Com Subject: Re: VMS C Compiler For whats its worth heres the code. I needed it quickly so no sub-routines or anything. If anyone feels like taking up the task I thought future enhancements could be : A) Allowing a specified delimiter B) Allowing ignored columns to be of variable length and thus different in each file being scanned. C) Allowing more than one column to be ignored. I know this isn't a C group but it might be of use to somebody. I'm done with it for now. #include #include #include #include /* Compare 2 CSV files, ignoring specified columns */ main( int argc, char *argv[] ) { char file1_rec [1024]; char file2_rec [1024]; FILE *Fp1 = 0; FILE *Fp2 = 0; int current_col = 1; int idx = 0; int line_num = 0; long ignore_col; if ( argc != 4 ) { printf("Usage : CSV_COMPARE FILENAME1 FILENAME2 COLUMN_TO_IGNORE"); } else { ignore_col = atoi(argv[3]); printf("File1 : %s\nFile2 %s\nIgnore Column %s\n", argv[1], argv[2], argv[3] ); printf("-=-=-=-=-=-=-=-=-=-=-=-\n"); /* Open file1 and file 2 */ Fp1 = fopen(argv[1],"r"); Fp2 = fopen(argv[2],"r"); do{ /* Test if EOF FILE1 has been reached, further if it has test that FILE2 has been completely read also */ if (!fgets(file1_rec,sizeof(file1_rec),Fp1)) { printf("End of file1 detected - quitting\n"); if (fgets(file2_rec,sizeof(file2_rec),Fp2)) { printf("Warning FILE2 still has more data\n"); } break; } /* Test if EOF FILE2 has been reached */ if (!fgets(file2_rec,sizeof(file2_rec),Fp2)) { printf("End of file1 detected - quitting\n"); printf("This means FILE1 wasn't completely read - ERROR\n"); break; } current_col = 1; ++line_num; /* So we have a record from each file, now compare content */ for ( idx = 0; idx <= (strlen(file1_rec)-1); ++idx ){ if ( file1_rec[idx] == ',' ){ ++current_col; } else{ if ( current_col != ignore_col && file1_rec[idx] != file2_rec[idx] ) { printf ("Mismatch Line %d , char %d \n", line_num, idx); printf ("*FILE1* %s", file1_rec ); printf ("*FILE2* %s", file2_rec ); break; } } } } while (1); /* Close file 1 and file 1 */ fclose(Fp1); fclose(Fp2); } return 0; }