Tech Support Guy banner
Status
Not open for further replies.
1 - 13 of 13 Posts

· Registered
Joined
·
469 Posts
Discussion Starter · #1 ·
Hello. I'm having trouble saving data to an ASCII file and then reading it back into the program. I'm able to save the data alright (I can view it in a text editor). However, I keep getting a segmentation fault when I try to read it back into the program.

Here's how I save the data:
Code:
   file_ptr = fopen( file_name, "w" );
      fprintf( file_ptr, "avg_get_time\t%u\0", values.avg_get_time );
      fprintf( file_ptr, "avg_loop_time\t%llu\0", values.avg_loop_time );
      ret = fclose( file_ptr );
When I read the data back in (after the program is restarted) I use the following magic:
Code:
   file_ptr = fopen( file_name, "r" );

      /* retrieve data from file            */
      n = 0;
      printf( "Trying to copy data from file . . .\n" );
      while( (file_data[n] = fgetc( file_ptr )) != EOF ) {
         printf( "%c", file_data[n] );
         n++;
      }
      ret = fclose( file_ptr );

      /* parse saved data from file         */
      m = 0;
      printf( "\nTrying to parse data from file . . .\n" );
      /* THE CODE CRASHES HERE */
      for( n = strchr( file_data, '\t' )+1; file_data[n-1] != '\0'; n++ ) {
          ascii_num[m] = file_data[n];
          m++;
      }
      printf( "%s\n", ascii_num );
I keep getting a segmentation fault in the last for loop. I've tried using '\n' and '\0'. Does anyone have any other suggestions?

Thanks.
 

· Registered
Joined
·
6,693 Posts
Code:
[SIZE=2]
      for( n = strchr( file_data, '\t' )+1; file_data[[COLOR=Red]n-1[/COLOR]] != '\0'; n++ ) {
          ascii_num[m] = file_data[[COLOR=Red]n[/COLOR]];
          m++;
      }
[/SIZE]
In the last loop, you're passing the value \0 to the function ascii_num!
In the for statement, you've just to replace the n-1 expression by n.
 

· Registered
Joined
·
431 Posts
There are a number of problems with your code, but the most fatal aspect of it is that strchr() does not return an int - it returns char *. You are using that value as an array index, which C will happily let you do, but it is a sure path to dumping core.

I'm not sure what chicon was trying to say, as ascii_num looks to be an array, not a function.
 

· Registered
Joined
·
6,693 Posts
AGCurry is right, the strchr() returns a char * and ascii_numb is not a function as I believed. Sorry, I only focused on the way the loop increment is handled.
 

· Registered
Joined
·
469 Posts
Discussion Starter · #5 ·
Ok, I decided to handle this a little differently using fgets() and strtok(). This is what I have so far:
Code:
      fgets( file_data, s, file_ptr );
      strtok( file_data, "\t" );
      ascii_num = strtok( NULL, "\n" );
The man page for strtok indicates that NULL should be passed for the first arguement on subsequest calls, but I keep getting an "incompatible types in assignment" error on the last line there when I try to compile. The man page isn't much help here because it says that strtok returns a character pointer, and that is what ascii_num[16] is.

Any thoughts? Thanks.
 

· Registered
Joined
·
431 Posts
fgets( file_data, s, file_ptr );
strtok( file_data, "\t" );
ascii_num = strtok( NULL, "\n" );

If ascii_num is declared as char[16], you can use it as a char * on the right side of assignments but not on the left side. Try declaring it as "char *ascii_num" .
 

· Registered
Joined
·
469 Posts
Discussion Starter · #8 ·
AGCurry said:
fgets( file_data, s, file_ptr );
strtok( file_data, "\t" );
ascii_num = strtok( NULL, "\n" );

If ascii_num is declared as char[16], you can use it as a char * on the right side of assignments but not on the left side. Try declaring it as "char *ascii_num" .
Um, I don't think that's quite right because that's how I'm using it in my code, and I've been able to verify that it's reading back the number properly from the file.

The problem now is the 64-bit number is not being stored properly to the file. (I'm not sure why it quit working). Here is the code I'm using to store the values:
Code:
      fprintf( file_ptr, "%u\t%ull", values.avg_get_time, values.avg_loop_time );
For example, if avg_loop_time is 23540235014, the value stored in the file is "2065398534ll". For some reason it seems that %ull is not recognised by fprintf(). Any suggestions?

Thanks.
 

· Registered
Joined
·
469 Posts
Discussion Starter · #10 ·
Chicon said:
Just try this :
fprintf( file_ptr, "%l\t%ll", values.avg_get_time, values.avg_loop_time );

refered documentation
Thanks, Chicon. It turned out I was using %ull, when I needed to be using %llu. So, now, the problem is here:
Code:
         values.avg_loop_time = (unsigned long long)atoll( c );
In the above statement c is "23540235014". However, it appears that only the lower 32-bits are being converted by atoll(). I've been able to verify this with the following code:
Code:
      fgets( file_data, s, file_ptr );
      c = strtok( file_data, "\t\n" );
      values.avg_get_time = atoi( c );
      printf( "%s\n", c );
      printf( "%u\n", values.avg_get_time );
      while( c != NULL ) {
         c = strtok( NULL, "\t\n" );
         values.avg_loop_time = (unsigned long long)atoll( c );
         printf( "%s\n", c );
         printf( "%llu\n", values.avg_loop_time );
      }
Any thoughts?

Thanks.
 

· Registered
Joined
·
6,693 Posts
For the moment, I've no clue. I'm searching for well documented sites C++ programming in 64 bits. Unfortunately, I can't try the coding as I don't have a 64 bit processor.
 

· Registered
Joined
·
3,258 Posts
I'm not sure what your compiler is (version-wise) or if you are using something like mingw.
I don't know if the processor you are working with is 64bit or 32 - or how you have defined the target or if you've included the headers which would resolve these issues -- but you could try using a format specifier of %I64u instead of %llu and see what it does.
 
1 - 13 of 13 Posts
Status
Not open for further replies.
Top