Skip to main content
gamedev.net
🔒 Locked

File Loading

Started by glVertex3f Jun 24, 2004 at 12:06 AM 36 replies 4.9k views
Original Post
glVertex3f
glVertex3f
I run this function but it crashes my program. Im new to file i/o so be gentle...

void LoadWorld(char *sFileName)
{
  FILE *pFile;

  if((fopen(sFileName, "rb")) == NULL)
  {
    MessageBox(NULL, "Failed to load World", "Error", MB_OK);
    exit(0);
  }

  for(y = 0; y < 12; y++)
  {
    for(x = 0; x < 32; x++)
    {
      fscanf(pFile, "%i", i);
      World1_1[y][x] = i;
    }
  }

  fclose(pFile);
}
Samith
Samith
Try fscanf(pFile, "%i", &i);

EDIT: A bit more explanation: You need to pass the address of the variable you want the data read into, otherwise it will try to write at the memory that is the value of i. Obviously this won't work, so you have to make sure you're giving it a valid location in memory to write to.

Raduprv: :p
glVertex3f
glVertex3f
nope, still get the "send error report" stuff.. :-/
Samith
Samith
Are you sure the World1_1 array is big enough? Like, if it's a 10x30 array you obviously can't be writing at 11 and 31.

Anyway, did you step through it with the debugger? Where is the error occurring?
glVertex3f
glVertex3f
my world array is

int World1_1[12][32];


and I dont have a debugger... do I? :-/
Conner McCloud
Conner McCloud
pFile = fopen(...)
if(pFile == NULL)

You never assigned pFile to anything, so its just pointing to some random place in memory.

CM
glVertex3f
glVertex3f
DOH! Thats embarrising... slipped by all of us lol...

THANKS CONNER!
glVertex3f
glVertex3f
One more thing...

my map file is a long series of numbers...

this function seems to only read the first number and not the others...

I tried using fseek but no change...
Raduprv
Raduprv
Try to adapt my code:
	i=0;	f=fopen("admin_list.txt", "rb");	if(!f)return;	while(1)		{			fscanf(f,"%s %i",&admin_list.name,&admin_list.privilege);			i++;			if(!fgets(strLine, 100, f))break;		}	fclose(f);
Samith
Samith
Try using fgets(). Use it like this:

fgets((char*)&i, sizeof(int), pFile);
glVertex3f
glVertex3f
  for(y = 0; y < 12; y++)  {    for(x = 0; x < 32; x++)    {      fseek(pFile, seek, SEEK_SET);      fscanf(pFile, "%i", &i);      World1_1[y][x] = i;      seek++;    }  }


shouldnt this go over one number eachtime its read and put that value to the next grid slot in my array?

[Edited by - glVertex3f on June 24, 2004 1:49:51 AM]
glVertex3f
glVertex3f
i tried, didnt work for me..
glVertex3f
glVertex3f
void LoadWorld(char *sFileName){  FILE *pFile;  int seek = 0;  if((pFile = fopen(sFileName, "rb")) == NULL)  {    MessageBox(NULL, "Failed to load World", "Error", MB_OK);    exit(0);  }  while(1)  {    fscanf(pFile, "%i", &i);    for(y = 0; y < 12; y++)    {      for(x = 0; x < 32; x++)      {        //fseek(pFile, seek, SEEK_SET);        //fscanf(pFile, "%i", &i);        World1_1[y][x] = i;        //seek++;      }    }    if(!fgets((char*)&i, sizeof(int), pFile))      break;  }  fclose(pFile);}


Im not sure if im doin that fgets right though...
glVertex3f
glVertex3f
fseek.. its commented out ;-)

and im not sure what you mean about replacing fscanf.. I still used it


(sorry if im not getting this)

btw..

I ran a console app that loads the map file and prints out the values and instead of numbers betwen 0 and 5, it prints out seemingly random 6 digit numbers.. any idea why?

Raduprv
Raduprv
Oh, sorry, I am tired.
You did it wrong.
Remove the while(1)
remove the scanf near the while
uncomment the scanf inside the for
put the fgets after the scanf
remove the fgets from where you have it now.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.