Joined
·
469 Posts
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:
When I read the data back in (after the program is restarted) I use the following magic:
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.
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 );
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 );
Thanks.