int Level[160];
void Read_Level(char *levelName)
{
int i;
FILE *fich;
fich = fopen(levelName, 'r');
for (i=0;i<159;i++)
{
fscanf(fich,"%d",&Level[i]);
}
fclose(fich);
}
I imagine the problem is in the for loop, you go from 1 to 160, which is more than you need, 0-159 if the file only contains 160 chars then at the end of the loop you're trying to read a number that doesn't exist.
I'd also put 'r' in single quotes as its a char, not a string
and I'd use char *levelName because thats how I normally pass file names to functions.
You could always include a printf("%d,"Level); in the loop too, then you could see when the the program crashes (ie the last number shown when it freezes)
Well, Im not sure if this is right but make sure you are using a char (buffer) when using this.
Also, it is not neccesary to have the char levelName[255] with the [255]. You may also be overflowing the buffer, I cant remember but does the scanf search/rea/make the \0 ?
Also, you may want to set the int 'i' to an actual number, not just make it a variable capable of manipulating because right when starting itll read it and see scanf for Level when i is nothing, so simple make the integer i = 0. or -1, depending if 0 is a number in your file.
Also, use the itoa function instead of the scanf, it is just like scanf except itoa only looks for integers and feeds them into a buffer easy for use.
Im not saying anything is worng, but if your stuck, maybe try something that i stated?
There are two reasons that can make your program crash:
1) fopen(levelName, 'r') -> "r" (this is certainly what made your PSP freeze). 'r' is a char, and fopen needs a string (char*), these are incompatible.
2) verify that the file could be open, else it will crash if the file doesn't exist.
Brunni wrote:There are two reasons that can make your program crash:
1) fopen(levelName, 'r') -> "r" (this is certainly what made your PSP freeze). 'r' is a char, and fopen needs a string (char*), these are incompatible.
2) verify that the file could be open, else it will crash if the file doesn't exist.