Skip to main content
gamedev.net
🔒 Locked

File I/O problem...

Started by Moe 12 replies 550+ views
Original Post
Moe
Moe
I can''t figure out what is causing the problem that I am having. I have a file with the following contents
  
//Prototype Options File 

windowedmode 1
width 800 
height 600
backgroundred 100
backgroundgreen 255
backgroundblue 100
  
Now I want to load in the file, check what the values are in the file, and use them in my program. Here is the code that I use to load in the file and check its values:
  
//open the file

FILE * pFile = fopen("system//startup.txt", "r");


//check to see if it opened

if(pFile == NULL)
{
	logfile.LogtoFile("D3D - failed to open startup file\n");
	return E_FAIL;
}

//check to make sure its the right file

if(fscanf(pFile, "//Prototype Options File ") == EOF)

{
	logfile.LogtoFile("D3D - failed to verify startup file type\n");
	return E_FAIL;
}

//try reading in the windowed mode variable from the file

if(fscanf(pFile, "windowedmode %d", &Windowed) == EOF)
{
	logfile.LogtoFile("D3D - failed to load windowed mode variable\n");
	return E_FAIL;
}

//try reading in the resolution

//width...

if(fscanf(pFile, " width %d ", &ScreenWidth) == 0)
{
	logfile.LogtoFile("D3D - failed to load screen width from file\n");
	return E_FAIL;
}

//height...

if(fscanf(pFile, " height %d", &ScreenHeight) == 0)
{
	logfile.LogtoFile("D3D - failed to load screen height from file\n");
	return E_FAIL;
}

  
Now for some stupid reason, my logfile says that it was unable to read the screen width from the file. I know that all my variables are declared and that my logfile functions are messing anything up. What am I doing wrong and how can I fix it?
ATronic
ATronic
Try removing the space after the %d.

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
Oluseyi
Oluseyi
Even better, use .ini files and scrap all your code, since Windows includes a host of functions for working with them and the registry:
*PrivateProfileString
*PrivateProfileSection
Moe
Moe
For the time being I want to keep things fairly simple. I just want to get the basics working, then I will enhance things if necessary. I will try and take out the space and see if that fixes it as soon as I can.
Moe
Moe
Nope, taking the space out after the %d didn''t work. Anyone else have any other ideas of why its not working? I tried checking the values of the variables after I tried loading them in and they were both 0, so I really have no idea whats going on. I also tried taking out the spaces before the thing, like " screenwidth..." into "screenwidth...". Does anyone know where there are some good I/O tutorials? I read the one at NeXe but it wasn''t very in depth.
Moe
Moe
I still can''t get this thing to work. I just can''t seem to figure it out. I read through the help files for fscanf and it said that fscanf will return the number of fields that it succesfully read (so if you read in 3 fields and they all were read properly, it would return 3), so I tried changing it to something like:
  
if(fscanf(pFile, "windowedmode %d\n", Windowed) != 1)
//....return bad....


That should work, shouldn''t it? Well, it isn''t. I also tried making the modifications that you suggested, and it won''t work either. It also turned out that it wasn''t loading in the windowedmode thing properly either. I don''t know what the heck is going on here.

If necessary, I can zip up the source and e-mail it to someone. I will be sure to post back on here how it gets fixed, if I do fix it via e-mail.
Advanced Bug
Advanced Bug
You cannot use fscanf to search for string in a file. As HellRiZZer already stated you must use "%s" conversion specifier to read option names. Make a loop that calls fscanf(pFile, "%s%d", optionName, optionValue) for each option and then check the optionName. For example:
  
char name[256];
int value;
FILE* file = fopen("startup.txt", "r");
while(1)
{
int items = fscanf(file, "%s%i", name, &value);
if(feof(file))
break;
if(items != 2)
{
printf("Error reading option: %s\n", name);
break;
}
if(!strcmp(name, "windowedmode"))
printf("windowedmode = %i\n", value);
else if(!strcmp(name, "width"))
printf("width = %i\n", value);
else if(!strcmp(name, "height"))
printf("height = %i\n", value);
else
{
printf("Unknown option: %s\n", name);
break;
}
}
fclose(file);
Moe
Moe
Actually, you can. I have seen someone do it with thier model loading code, and I think I will e-mail him to see if he can help me.
Advanced Bug
Advanced Bug
Moe, you are right. I just didn''t know about this feature of fscanf, so I apologize for my previous post.
After reading some more information about fscanf I wrote the following code that can read data from your file (at least on my computer it does it):

  
FILE * pFile = fopen(FILENAME, "r");
fscanf(pFile, " //Prototype Options File ");

if(fscanf(pFile, "windowedmode %d ", &Windowed) == 0)
{
// ERROR

}
if(fscanf(pFile, "width %d ", &ScreenWidth) == 0)
{
// ERROR

}
if(fscanf(pFile, "height %d ", &ScreenHeight) == 0)
{
// ERROR

}

What I don''t know how to solve is how to check if fscanf(pFile, " //Prototype Options File "); fails. It always returns 0.
SHilbert
SHilbert
Okay, this works for me:
    
#include <stdio.h>
#include <stdlib.h>

void err(const char * str)
{
printf("%s", str);
exit(1);
}

int main(int argc, char ** argv)
{
int windowed, width, height, bgred, bggreen, bgblue;

FILE * fp = fopen("test.txt", "r");

if(!fp)
err("Error, cannot load file\n");

if(fscanf(fp, " //Prototype Options File ") != 0)

err("Error, please verify file type\n");

if(fscanf(fp, " windowedmode %d ", &windowed) != 1)
err("Error, cannot read windowed mode\n");

if(fscanf(fp, " width %d ", &width) != 1)
err("Error, cannot read screen width\n");

if(fscanf(fp, " height %d ", &height) != 1)
err("Error, cannot read screen height\n");

if(fscanf(fp, " backgroundred %d ", &bgred) != 1)
err("Error, cannot read BG red component\n");

if(fscanf(fp, " backgroundgreen %d ", &bggreen) != 1)
err("Error, cannot read BG green component\n");

if(fscanf(fp, " backgroundblue %d ", &bgblue) != 1)
err("Error, cannot read BG blue component\n");

fclose(fp);

printf("File loaded in OK. Values are:\n"
"Windowed Mode: %d\n"
"Width: %d\n"
"Height %d\n"
"BG Red: %d\n"
"BG Blue: %d\n"
"BG Green %d\n",
windowed,
width,
height,
bgred,
bggreen,
bgblue);

return 0;
}

An important thing to remember is that fscanf() treats a space as any number of whitespace characters. However, if you don't add a space, it won't allow *ANY* whitespace between items. It appears that this was your problem. I can't seem to replicate the error with the screen width (perhaps the code is a little different from what you pasted here, or perhaps the forums butchered the code). Also, I've used != rather than == 0 or == EOF, primarily because if it hits EOF and you're checking for zero, you miss the error, and if the text is garbled and you check for EOF only, you also miss the error.

Hope that helps

EDIT: Also, a clarification for the return value of fscanf()... it returns EOF if it hits the end of the file, and otherwise the number of items read in. So if you don't have any format specs in the string (like when you read in the first comment) it'll return zero, because it read in no items.

ANOTHER EDIT: My line spacing got butchered. Ignore the ugliness

If life was like Counter-Strike...

Edited by - SHilbert on December 5, 2001 4:50:42 PM
Rube
Rube
You might want to consider changing your delimiter from a space to something like a tab character or an "=".

Might make the matching in your code easier since it seems your issue is in getting a proper match.

Yeah, avoid putting info into the Registry. Bloated enough as it is =)

R.
Moe
Moe
Yeah, it turns out that I needed a space where I didn''t have one, like so:
  
fscanf(pFile, "width %d ", ScreenWidth); //sans error checking...


quote:

Moe, you are right. I just didn''t know about this feature of fscanf, so I apologize for my previous post


Bah, don''t worry about it. As long as it was a learning experience. I by no means take credit for the idea of using fscanf with file loading. I learned that from Shilbert''s Milkshape model loading code. (I hope you don''t mind Shil!)

Moe''s site

Topic Locked

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

Sign in to reply to this topic.