Advertisement

Reading a File

Started by January 23, 2002 03:15 PM
4 comments, last by skreuzer 22 years, 10 months ago
Hello All, I am sure this is a very simple question, but it has been quite some time since I had to open a file and read it. Anyway, I have a file called levels.ini and it looks like this Level1 128 150 1000 1001 3600 Level2 128 150 1000 1001 7200 Level3 128 150 1000 1001 10800 I want to be able to pass "Level1" to a function that reads the file until it finds "Level1" and then reads the numbers and game grid into variables. Can anyone point me to any reference regarding this. SK skreuzer@mac.com
Well, you can use the very basics of FILE IO, or you can make it fancy, but to keep it simple you can do it like the following:

FILE * fpInput;
fpInput = fopen ("", "w");

fscanf (fpInput, "<format specifier>", var1, var2);

fclose (fpInput);

The format specifier in this case will change depending on what part of the file you want to read. To handle the first portion of the file you will probably want to have a character, lets say char sInput[30], that takes character data. for int''s, let say iInput.

fscanf (fpInput, "%[^\n]\n", sInput);
fscanf (fpInput, "%d\n", iInput);

the \n at the end will tell scanf to "skip" past that on each line so it may proceed to the next and starts right at the beginning of the next line.

Hopefully this helps you out and the little examples I posted actually work



Cray
CrayLead Programmer, MTA (http://www.mtavc.com)cray@multitheftauto.com
Advertisement
The above poster is on the right track. However, the "Level" name poses a minor problem. First off there is no way to determine when you''ll see "Level" unless your specificaly looking for the whole word. I highly suggest using a special character like ! before you define the word "Level" so it would look like this.. "!Level 1" Then you can search through the file for the specific special character that your using rather than searching for a whole word. This also allows you to determine how many levels you have just by counting the number of special characters in the file...
Thank you all for your Help. With the above advice, this is what I came up with. It is part of the CLevelManager Class and it works like a charm
  void CLevelManager::LoadLevel(int BoardNumber){ char GridBuffer = '0', LevelString[10]; sprintf(LevelString, "Level%d", BoardNumber); while(!feof(LevelFile)) {  fscanf(LevelFile, "%s", Buffer);  if(strcmp(Buffer, LevelString) == 0)  {	   fscanf(LevelFile, "%d\n", &BackgroundPatternID);   fscanf(LevelFile, "%d\n", &FirstBorderID);   fscanf(LevelFile, "%d\n", &FixedBlockID);   fscanf(LevelFile, "%d\n", &DestructibleBlockID);   fscanf(LevelFile, "%d\n", &GameTime);  } }}  


Edited by - skreuzer on January 25, 2002 12:53:16 PM
There''s actually a bunch of Windows functions for dealing with .ini files. You''d simply need to rename each of your sections (from "Level1" to "[Level1]", etc) and place an identifier before each of your values. Then you''d use Get/WritePrivateProfileString(). Look it up in MSDN.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Since you are using C++, you could use file streams.

  #include <fstream.h>#include <iostream.h>int main() {	char buf[100];	bool found=false;	int values[5];	fstream in("levelfile.txt");	while(in.getline(buf,100)) {		if(strcmp(buf,"Level1")) {			found=true;			break;		}	}	if(found) {		for(int i=0;i<5;i++) {			in >> values[i];		}		for(int i=0;i<5;i++) {			cout << values[i] << endl;		}	}	return 0;}  



---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement