Jump to content
  • Advertisement
Sign in to follow this  
TheOne1

reading file i/o

This topic is 5008 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

I have a .dat file that holds the following: Name -> John Doe Age -> 18 How can I read each line so that it only reads what is after the '>' character? Is there any specific string class function? (I know I can use ignore(X) but, that would be to hectic and is different for each line)

Share this post


Link to post
Share on other sites
Advertisement
if i were you .Ý would read all line to a buffer first.and then with strstr() function i would find '>' character .(and get to pointer to that character in buffer.and then increase the Poniter once .now i have a pointer to where i want.read from buffer from starting that pointer until the NULL(end of buffer)..

const int MAX=80;

char MyBuffer[MAX];
char MySecondBuffer[MAX];
char* PtrToWhereiwant=NULL; //for getting the part i need
while(MyInputObj.getline(MyBuffer,MAX))
{
PtrToWhereiwant=strstr(MyBuffer,'>');
++PtrToWhereIwant;//now it is pointing to where i want...
strcpy(MySecondBuffer,PtrToWhereIwant); //i get what i need in MySecondBuffer .
//you now can do whatever you want with it
}





source code maybe somekind of this(not compiled there maybe so many error but the idea is this:...)

Share this post


Link to post
Share on other sites
Quote:
Original post by TheOne1
I have a .dat file that holds the following:

Name -> John Doe
Age -> 18

How can I read each line so that it only reads what is after the '>' character? Is there any specific string class function? (I know I can use ignore(X) but, that would be to hectic and is different for each line)


Since you're smart (oppose "masochistic") enough to be using "string class function"s, let's stick with that shall we? :)

Within an existing string, you can search for the first occurrance of a character with the string's "find" member function. This is overloaded in a few different ways, and there are also some other find-type functions you may find useful (doc). Given that, here's one straightforward way to do it:


using namespace std;
ifstream myfile("whatever.txt");
string line;
// Read a line - you'll probably want to do this in a loop yes?
getline(myfile, line);
// Find the first '>'. We will be told the character position, or string::npos
// if not found.
int arrowloc = line.find('>');
if (arrowloc == string::npos) {
// complain loudly, possibly throw an exception or return
}
// Now, we want a substring ranging from the next character after the arrow,
// to the end of the string. I'll do this by constructing that substring and
// assigning it back to the 'line' var; there are other ways too.
line = string(line, arrowloc + 1);


Another option would be to scan through the file a character at a time while reading, and then grab the rest of the line:

using namespace std;
ifstream myfile("whatever.txt");
string line;
// Eat characters up to and including '>'
while (myfile.get() != '>'); // <-- note, empty loop.
getline(myfile, line); // read rest of line.


Of course, this way you ignore the text before the arrow, which looks kind of important to me :)

You could also parse lines for 'tokens', i.e. words and numbers, and look for the '->' word. Look into std::stringstream (in header <sstream>).

Share this post


Link to post
Share on other sites
A tip: You might not want to read files by parsing strings, 'cause in my opinion, it's more of a hassle then just using binary i/o.
Let's make the setup of a file into a simple little structure:
FILE BLOCK:
UINT size; //Size of the name
char *name; //Pointer to the name
UINT age; //Age of the person
ifstream fcur("something.dat",ios::binary); //Little rusty on file I/O though...
UINT name_size=0;
char *name;
UINT age;
fcur.read((char*)&name_size,sizeof(UINT));
fcur.read(name,name_size);
fcur.read((char*)&age,sizeof(UINT));
...
fcur.close();

Though it may not seem important, but it actually saves you around 5-10 bytes of space (trivial, but still).
Simple enough. Please correct any of my errors.

Share this post


Link to post
Share on other sites
thanks for the replies.....

but I was wondering, how do you think professional game developers usually do this kind of thing? For example, for Fallout2, there is a .CFG (configuration) file and when opened it looks like this:

[debug]
mode=environment
output_map_data_info=0
show_load_info=0
show_script_messages=0
show_tile_num=0

[preferences]
brightness=1.000000
combat_difficulty=1
combat_messages=1
combat_speed=50
combat_taunts=1
game_difficulty=1
.... and so on and they only read the information after the width, height, etc. identifiers. Do they only read the numbers or do they read the whole line? Or I wonder if the identifiers like game_difficutly are actual variables inside the code....

I know it's different but I'm thinking it's sort of similar. Basically, I'm creating a text rpg right now, and I'm trying to save the characters name, age, etc, and have all that information saved in a .dat file and when loading the game I'll be able to retrieve that information. I wanted the Name ->, Age ->, etc. identifiers so if I open up the .dat file I know what I'm looking at.

but thanks, Zahlman. I think the example you gave me will work just fine.

[Edited by - TheOne1 on March 2, 2005 11:54:50 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by TheOne1
thanks for the replies.....

but I was wondering, how do you think professional game developers usually do this kind of thing? For example, for Fallout2, there is a .CFG (configuration) file and when opened it looks like this:


Yes, that's a standard format (Windows INI file).

Quote:

.... and so on and they only read the information after the width, height, etc. identifiers. Do they only read the numbers or do they read the whole line? Or I wonder if the identifiers like game_difficutly are actual variables inside the code....


They likely read the whole line. Of course, since variable names don't exist at runtime, they won't be "actual variables inside the code"; but you could for example make a std::map<std::string, float> , then read the two halves of the line (a string up until an = sign, then interpret the RHS as a float), and store it in the map.

The reason for reading the whole line, of course, is that it makes the LHS text meaningful; thus you can re-order the file contents without messing up the program. Having stuff in your data file that's completely unused is bad, because you add complexity to your code just to be able to ignore the unused stuff. This is one of the reasons for cutting out all the fat and using a binary file format. However, the decision is rarely if ever trivial, and you should be appropriately informed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!