449,189 Members | 1,370 Online Need help? Post your question and get tips & solutions from a community of 449,189 IT Pros & Developers. It's quick & easy.

# beginner fscanf question

 P: n/a Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 .... It was easy to read this values into an integer variable: FILE *fp = fopen("test.dat","r"); int mem_word = 0; while (fscanf(fp,"%x", &mem_word) != EOF) .... Now, I have the following format where my hex number is splitted by spaces. So I wonder if there is an easy way to parse such a line and not doing a lot of space removing etc. 0C 10 00 12 // 00000000 0C 10 00 12 // 00000000 .... Thanks for helpful comments. Aug 15 '07 #1
10 Replies

 P: n/a Roman Zeilinger wrote: Now, I have the following format where my hex number is splitted by spaces. So I wonder if there is an easy way to parse such a line and not doing a lot of space removing etc. I don't think so. But it's trivial to remove the spaces and then use sscanf(). 0C 10 00 12 // 00000000 0C 10 00 12 // 00000000 Or you could write your own simple parser Aug 15 '07 #2

 P: n/a Roman Zeilinger #include int main( void ) { FILE *fp = fopen( "sh.dat", "r" ); unsigned int d[ 4 ]; unsigned int x; if ( fp == NULL ) return EXIT_FAILURE; while ( fscanf( fp, "%x%x%x%x // 00000000", d, d + 1, d + 2, d + 3 ) == 4 ) { x = ( ( ( ( ( d[ 0 ] << 8 ) + d[ 1 ] ) << 8 ) + d[ 2 ] ) << 8 ) + d[ 3 ]; printf( "x = %x\n", x ); } return 0; } but if that's not the case then you will need to do a bit more of work in that while loop: while ( fscanf( fp, "%x%x%x%x", d, d + 1, d + 2, d + 3 ) == 4 ) { x = ( ( ( ( ( d[ 0 ] << 8 ) + d[ 1 ] ) << 8 ) + d[ 2 ] ) << 8 ) + d[ 3 ]; printf( "x = %x\n", x ); while ( ( c = fgetc( fp ) ) != '\n' && c != EOF ) /* empty */ ; } You also will need to add a definition of 'c' (as an int, not a char!). You could use something simpler if you can be sure that there will be never more than 12 characters following your quadruplet of hex numbers (again except trailing white space characters): #include #include int main( void ) { FILE *fp = fopen( "sh.dat", "r" ); unsigned int d[ 4 ]; unsigned int x; char buffer[ 13 ]; if ( fp == NULL ) return EXIT_FAILURE; while ( fscanf( fp, "%x%x%x%x%12[^\n]", d, d + 1, d + 2, d + 3, buffer ) == 5 ) { x = ( ( ( ( ( d[ 0 ] << 8 ) + d[ 1 ] ) << 8 ) + d[ 2 ] ) << 8 ) + d[ 3 ]; printf( "x = %x\n", x ); } return 0; } Regards, Jens -- \ Jens Thoms Toerring ___ jt@toerring.de \__________________________ http://toerring.de Aug 15 '07 #3

 P: n/a You could use something simpler if you can be sure that there will be never more than 12 characters following your quadruplet of hex numbers (again except trailing white space characters): #include #include int main( void ) { FILE *fp = fopen( "sh.dat", "r" ); unsigned int d[ 4 ]; unsigned int x; char buffer[ 13 ]; if ( fp == NULL ) return EXIT_FAILURE; while ( fscanf( fp, "%x%x%x%x%12[^\n]", d, d + 1, d + 2, d + 3, buffer ) == 5 ) { x = ( ( ( ( ( d[ 0 ] << 8 ) + d[ 1 ] ) << 8 ) + d[ 2 ] ) << 8 ) + d[ 3 ]; printf( "x = %x\n", x ); } return 0; } Great thanks Jens. It works fine for one line so I have to figure out how I can parse the whole txt file :) Cheers! Aug 15 '07 #4

 P: n/a I modified the code from jens slightly, but it looks like the program sticks with the first line it reads from "icache.dat". Why does the program not move to the next line of the text file after the fscanf and remains with the first one? int main( void ) { FILE *fp = fopen( "icache.dat", "r" ); unsigned int d[ 4 ]; unsigned int x; if ( fp == NULL ) return EXIT_FAILURE; while ( fscanf( fp, "%x%x%x%x%", d, d + 1, d + 2, d + 3 ) != EOF ) { x = ( ( ( ( ( d[ 0 ] << 8 ) + d[ 1 ] ) << 8 ) + d[ 2 ] ) << 8 ) + d[ 3 ]; printf( "x = %x\n", x ); } return 0; } Cheers! Aug 15 '07 #5

 P: n/a Roman Zeilinger wrote: I modified the code from jens slightly, but it looks like the program sticks with the first line it reads from "icache.dat". Why does the program not move to the next line of the text file after the fscanf and remains with the first one? Time for the C FAQ, I suspect. http://c-faq.com There's loads of discussion of the pitfalls and snares of scanf.. Aug 15 '07 #6

 P: n/a Roman Zeilinger

 P: n/a jt@toerring.de (Jens Thoms Toerring) writes: Roman Zeilinger I modified the code from jens slightly, butit looks like the program sticks with thefirst line it reads from "icache.dat". Whydoes the program not move to the next line ofthe text file after the fscanf and remains with thefirst one? >int main( void ) { FILE *fp = fopen( "icache.dat", "r" ); unsigned int d[ 4 ]; unsigned int x; > if ( fp == NULL ) return EXIT_FAILURE; > while ( fscanf( fp, "%x%x%x%x%", d, d + 1, d + 2, d + 3 ) != EOF ) { x = ( ( ( ( ( d[ 0 ] << 8 ) + d[ 1 ] ) << 8 ) + d[ 2 ] ) << 8 ) + d[ 3 ]; printf( "x = %x\n", x ); } return 0;} If there are some unusable data in that line as in your origi- nal example had you don't skip them. fscanf() will try to read them also as 4 hex numbers, fail for obvious reasons, return 0 and not move forward in the file. For this reason it is almost always better to test the return value from fscanf for success (== 4 in this case) rather than for one particular failure (from the possible EOF, 0, 1, 2 or 3). -- Ben. Aug 15 '07 #8

 P: n/a Roman Zeilinger wrote: > Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ... It was easy to read this values into an integer variable: FILE *fp = fopen("test.dat","r"); int mem_word = 0; while (fscanf(fp,"%x", &mem_word) != EOF) ... Now, I have the following format where my hex number is splitted by spaces. So I wonder if there is an easy way to parse such a line and not doing a lot of space removing etc. 0C 10 00 12 // 00000000 0C 10 00 12 // 00000000 ... Thanks for helpful comments. I like to read lines and proccess strings. /* BEGIN beginner.c */ #include #include #include #include #include #define FILE_NAME ("test.dat") #define LINE ("0C 10 00 12") #define LINELENGTH 11 #define str(x) # x #define xstr(x) str(x) void hex_squeeze(char *s1); int main(void) { unsigned long mem_word; char array[sizeof LINE]; int rc; char *endptr; char *fn = FILE_NAME; FILE *fp = fopen(fn, "r"); assert(LINELENGTH == sizeof LINE - 1); fp = fopen(fn, "r"); if (fp == NULL) { printf("fopen problem with %s\n", fn); exit(EXIT_FAILURE); } do { rc = fscanf(fp, "%" xstr(LINELENGTH) "[^\n]%*[^\n]", array); if (!feof(fp)) { getc(fp); } if (rc == 0) { array = '\0'; } if (rc == 1) { hex_squeeze(array); mem_word = strtoul(array, &endptr, 16); printf("%lx\n", mem_word); } } while (rc != EOF); fclose(fp); return 0; } void hex_squeeze(char *s) { char *p = s; do { if (isxdigit((unsigned char)*s)) { *p++ = *s; } } while (*s++ != '\0'); *p = '\0'; } /* END beginner.c */ -- pete Aug 16 '07 #9

 P: n/a In article <5i*************@mid.uni-berlin.de> Jens Thoms Toerring

 P: n/a Chris Torek Jens Thoms Toerring

### This discussion thread is closed

Replies have been disabled for this discussion. 