• Advertisement

Archived

This topic is now archived and is closed to further replies.

Reading a File

This topic is 5842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement