DirectX and File I/O

Started by
3 comments, last by m0rte 22 years, 5 months ago
Hello, im trying to open a txt file and read from it to set the resolution etc, but when i do, it always sets it to 640x480. Can some one help? below is the code that handles it: FILE *ConfigFile; ... ConfigFile = fopen("Config.txt", "r"); ... if(fscanf(ConfigFile, "1024x768") != NULL) { lpDD->SetDisplayMode(1024, 768, 16); } else if(fscanf(ConfigFile, "800x600") != NULL) { lpDD->SetDisplayMode(800, 480, 16); } else { lpDD->SetDisplayMode(640, 480, 16); } ... fclose(ConfigFile); i''ve checked the file path, checked if theres anything actually in file (wrote all this code when i was half asleep), and its all ok. blah!
Advertisement
Well, there are a number of problems with this. For instance, with each branch of your if you're doing an fscanf, each of which will move the file pointer. So, it looks at one line for 1024x768. If it's not that, looking again with fscanf makes it look at the next line which probably doesn't have what you want. So, assuming that this resolution string is on the first line, use rewind at the beginning of each branch before the fscanf to make sure that the file pointer is always in the same place each time you check. Secondly, I'm not sure that fscanf should be used in quite that way. You might consider doing something like this:

    unsigned int x_res = 0, y_res = 0;fscanf("resolution = %ux%u", &x_res, &y_res);// if we couldn't find the setting, use defaultsif(x_res == 0 || x_res == 0)    {x_res = 640; y_res = 480;}  


Brush up on fscanf usage a bit; I've gotten into a lot of trouble with it over the years as well, so I should do the same.

Edited by - merlin9x9 on November 12, 2001 3:35:57 PM
You''re using fscanf incorrectly.

Try the following:

  char buff[20];if (fscanf(ConfigFile, "%s", buff) > 0){	if (strstr(buff, "1024x768))	{		// Set 1024x768	}	else if (strstr(buff, "800x600))	{		// Set 800x600	}	else	{		// Set 640x480	}}  

Mark Fassett

Laughing Dragon Games

http://www.laughing-dragon.com

Ooops... someone got there first

Mark Fassett

Laughing Dragon Games

http://www.laughing-dragon.com

kool beans thanks for your help

This topic is closed to new replies.

Advertisement