• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1skreuzer  Members

Posted 23 January 2002 - 09:15 AM

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

### #2craymail  Members

Posted 23 January 2002 - 09:24 AM

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

### #3Anonymous Poster_Anonymous Poster_*  Guests

Posted 23 January 2002 - 09:57 AM

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...

### #4skreuzer  Members

Posted 24 January 2002 - 05:32 AM

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

### #5Oluseyi  Members

Posted 24 January 2002 - 08:14 AM

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!

### #6CaptainJester  Members

Posted 25 January 2002 - 02:43 AM

Since you are using C++, you could use file streams.

  #include #include 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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.