 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
ShUr1k3n
Joined: 16 Oct 2005 Posts: 42
|
Posted: Wed Mar 08, 2006 8:06 am Post subject: File Read problem ?! |
|
|
Hey guys....
Can any1 plz tell me what the problem with this code ?!
| Code: |
int Level[161];
void Read_Level(char levelName[255])
{
int i;
FILE *fich;
fich = fopen(levelName, "r");
for (i=1;i<=160;i++)
{
fscanf(fich,"%d",&Level[i]);
}
fclose(fich);
} |
The File contain 160 numbers (between 0 and 40) in each LINE.
When the program goes into this code the PSP freezes.. And restart...
Whats the problem ?!
:S |
|
| Back to top |
|
 |
fish
Joined: 08 Feb 2006 Posts: 25
|
Posted: Wed Mar 08, 2006 11:45 am Post subject: |
|
|
not a very exact reply, but there's some things I'd do differently, whether they make a difference or not I'm not sure...
| Code: |
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[i]); in the loop too, then you could see when the the program crashes (ie the last number shown when it freezes) |
|
| Back to top |
|
 |
sg57
Joined: 14 Oct 2005 Posts: 154
|
Posted: Wed Mar 08, 2006 11:50 am Post subject: |
|
|
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[i] 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? |
|
| Back to top |
|
 |
Brunni
Joined: 08 Oct 2005 Posts: 186
|
Posted: Wed Mar 08, 2006 8:02 pm Post subject: |
|
|
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.
| Code: | int Level[160];
void Read_Level(char *levelName)
{
int i;
FILE *fich;
fich = fopen(levelName, "r");
if (fich != NULL)
{
for (i=1;i<=160;i++)
{
fscanf(fich,"%d",&Level[i]);
}
fclose(fich);
}
} |
_________________ Sorry for my bad english
Oldschool library for PSP - PC version released |
|
| Back to top |
|
 |
Warrick
Joined: 06 Dec 2005 Posts: 9
|
Posted: Wed Mar 08, 2006 8:54 pm Post subject: Try to use Sce Functions |
|
|
Hello first of all, 160 integers are 320 bytes not 160 bytes.
I think this will help you:
| Code: |
int Level[160];
void Load_Levels(char* filename)
{
int i,fd;
int* iPtr;
long size;
if ((fd = sceIoOpen(filename, PSP_O_RDONLY, 0777)) > 0)
{
size = sceIoLseek(fd, 0, PSP_SEEK_END);
sceIoLseek(fd, 0, PSP_SEEK_SET);
iPtr = (unsigned char *) malloc(size+1); // You say 160 bytes!!! but 160 int mean 320 bytes!!!
memset(iPtr, 0, size + 1);
if (iPtr != 0) sceIoRead(fd, iPtr, size);
else
{
printf("No Free Memory\n");
sceIoClose(fd);
return;
}
sceIoClose(fd);
for (i=0;(i<size && i<160);i++) Level[i] = (int) iPtr[i];
free(iPtr);
}
} |
I think that you have to change int Level[160] for unsigned char Level[160] if you want to use 1 byte for data (from 0-255 values).
I hope this help.
Kind Regards,
Warrick |
|
| Back to top |
|
 |
Orion_
Joined: 27 Jan 2005 Posts: 69
|
Posted: Wed Mar 08, 2006 9:09 pm Post subject: Re: Try to use Sce Functions |
|
|
| Warrick wrote: | | Hello first of all, 160 integers are 320 bytes not 160 bytes. |
I would say 640 bytes, not 320. |
|
| Back to top |
|
 |
ShUr1k3n
Joined: 16 Oct 2005 Posts: 42
|
Posted: Thu Mar 09, 2006 2:26 am Post subject: |
|
|
| 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.
| Code: | int Level[160];
void Read_Level(char *levelName)
{
int i;
FILE *fich;
fich = fopen(levelName, "r");
if (fich != NULL)
{
for (i=1;i<=160;i++)
{
fscanf(fich,"%d",&Level[i]);
}
fclose(fich);
}
} |
|
If u look at MY code (1st POST) u will see that i put "r" and not 'r'...
So the problem isn't that... |
|
| Back to top |
|
 |
Warrick
Joined: 06 Dec 2005 Posts: 9
|
Posted: Thu Mar 09, 2006 2:38 am Post subject: Re: Try to use Sce Functions |
|
|
| Orion_ wrote: | | Warrick wrote: | | Hello first of all, 160 integers are 320 bytes not 160 bytes. |
I would say 640 bytes, not 320. |
Yes, sorry for confussion i have short int on my mind ;).
Regards,
Warrick |
|
| Back to top |
|
 |
DustinFraze
Joined: 06 Jan 2006 Posts: 13
|
Posted: Thu Mar 09, 2006 6:17 pm Post subject: |
|
|
| Wrong topic. Disreguard. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|