Advertisement

Reading from a file question.......Using C

Started by June 30, 2000 09:50 PM
1 comment, last by SpikyQube 24 years, 5 months ago
Ok, this is part of a text game i''m making, just to get myself some experience, but now I have this stupid problem with this function here: #include "Game Basic.h" int welcome(void) { char game; char line[200]; char char_buffer[99]; int int_buffer; int i; int s; int temp_x; int temp_y; memset( file_name, ''\0'', 99 ); printf("What is your previous character''s name?\n"); scanf("%s", file_name); if( ( read_file = fopen( file_name, "r" ) ) == NULL ) { printf("Error Opening File!\n"); exit(0); } while( !feof( read_file ) ) { memset( line, ''\0'', 200 ); memset( char_buffer, ''\0'', 99 ); int_buffer = 0; fscanf( read_file, "%s %d", line, int_buffer); i = 0; s = 0; if( strstr( line, "position_x" ) != NULL ) { temp_x = int_buffer; } else if( strstr( line, "position_y" ) != NULL ) { temp_y = int_buffer; } else { while( !feof( read_file ) ) { memset( line, ''\0'', 200 ); memset( char_buffer, ''\0'', 99 ); fscanf( read_file, "%s %s", line, char_buffer); if( strstr( line, "player_name" ) != NULL ) { attribute.player_name = &char_buffer; } else if( strstr( line, "player_race" ) != NULL ) { attribute.race = &char_buffer; } else if( strstr( line, "player_class" ) != NULL ) { attribute.player_class = &char_buffer; } } } } initial_position(); position = &grid[temp_x][temp_y]; fclose( read_file ); return 1; } Ok, this is PART of the code for this function, cuz the others doesn''t contribute to the problem. From the MSVS debugger, I get the conclusion that the access violation I have with this function is with the fscanf function, cuz i checked, and like there''s nothing in read_file!!!! so line[] is trying to access nothing. My question is how come read_file has nothing in it, cuz i swear that i have file named "gilon" in the directory, it has everything (character stats, position...etc...), and when it asks for the character name, i put down "gilon"....I also cheked the debugger, and file_name has "gilon". So i dunno what''s wrong ;( This is the code for creating the file when a player quits the game: write_file = fopen( file_name, "w" ); fprintf(write_file, "position_x= %d\n", position -> x); fprintf(write_file, "position_y= %d\n", position -> y); fprintf(write_file, "hit_point= %d\n", attribute.hit_point); fprintf(write_file, "vitality= %d\n", attribute.vitality); fprintf(write_file, "constitution= %d\n", attribute.constitution); fprintf(write_file, "agility= %d\n", attribute.agility); fprintf(write_file, "strength= %d\n", attribute.strength); fprintf(write_file, "charisma= %d\n", attribute.charisma); fprintf(write_file, "dex= %d\n", attribute.dex); fprintf(write_file, "intelligence= %d\n", attribute.intelligence); fprintf(write_file, "wisdom= %d\n", attribute.wisdom); fprintf(write_file, "separator 0\n"); fprintf(write_file, "player_name= %s\n", attribute.player_name); fprintf(write_file, "player_race= %s\n", attribute.race); fprintf(write_file, "player_class= %s", attribute.player_class); fclose( write_file ); So this will give you an idea of how the files will look like. If you need more info just ask me I''ve been stare at this code for like a day, nothing comes into my mind seems to make it work....... Thanx a lot!!! I want to work for Squaresoft ;)
I want to work for Squaresoft ;)
I can see where you problem is.
    //this is a problemfscanf(read_file,"%s %d",line, int_buffer);//it should readfscanf(read_file,"%s %d", line, ∫_buffer);    


Try making that change. It should work now.

========================================================
If something sounds stupid but works, it's not stupid
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Advertisement
WOW!! thanx a lot, I feel stupid for missing this thing, hahahaha.....

I want to work for Squaresoft ;)
I want to work for Squaresoft ;)

This topic is closed to new replies.

Advertisement