c - How to use strcmp to compar names in a csv Data -
i want open csv file , compare contents of first row. have code open file written, not know how can compare first line strcmp open file.
to open file
#include <stdio.h> #include <stdlib.h> int main(void) { setvbuf(stdout, null, _ionbf, 0); setvbuf(stderr, null, _ionbf, 0); file *f; char file_name[255]; char data[127]; printf("which file want open : "); scanf("%s",file_name); f = fopen("c:\\projekt\\datei.csv","r"); if(f == null){ printf("could not open %s\n" ,file_name); exit(0); } while( fgets(data, sizeof(data), f) != 0 ) fputs(data, stdout); return 0; }
whats in file
"date" "time" "volt" "amp" "wirkfaktor" 26.10.13 08:00:00 237.802 1160.7682 0.7461853792 26.10.13 08:00:01 237.658 1168.92273 0.7203561543 26.10.13 08:00:02 237.815 1158.57273 0.7344799394 26.10.13 08:00:03 237.566 1174.5682 0.6960312563 26.10.13 08:00:04 238.063 1151.67273 0.80126914 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { setvbuf(stdout, null, _ionbf, 0); setvbuf(stderr, null, _ionbf, 0); file *f; char file_name[255]; char data[127]; printf("which file want open : "); scanf("%s",file_name); f = fopen("c:\\projekt\\datei.csv","r"); if(f == null){ printf("could not open %s\n" ,file_name); exit(0); } while( fgets(data, sizeof(data), f) != null ){ //fputs(data, stdout); } int = 1; int i; char heading[5][20]; if ( sscanf( data, "%19s%19s%19s%19s%19s", heading[0], heading[1], heading[2], heading[3], heading[4] ) != 5 ) { = 0; } if ( ) { char *expected[5] = { "\"date\"t\"time\"t\"volt\"t\"amp\"t\"wirkfaktor\"" }; ( = 0; < 5; i++ ) if ( strcmp( expected[i], heading[i] ) != 0 ) = 0; } if ( !good ) { printf( "heading line incorrect\n" ); exit( 0 ); } return 0; }
to verify first line of file correct, first extract 5 strings using sscanf, , compare strings expected strings, using strcmp.
int = 1; char heading[5][20]; if ( sscanf( data, "%19s%19s%19s%19s%19s", heading[0], heading[1], heading[2], heading[3], heading[4] ) != 5 ) = 0; if ( ) { char *expected[5] = { "\"date\"", "\"time\"", "\"volt\"", "\"amp\"", "\"wirkfaktor\"" }; ( = 0; < 5; i++ ) if ( strcmp( expected[i], heading[i] ) != 0 ) = 0; } if ( !good ) { printf( "heading line incorrect\n" ); exit( 1 ); }
Comments
Post a Comment