Page 1 of 1

[Solved] File Streaming Question

Posted: Tue Jul 07, 2009 4:26 am
by zodiac976
I am looking for a quick way to get data from a file.
Take this file for instance:

weapon.txt
knife
1
2
shortsword
2
5
longsword
5
8(end of file)

Normally when I want to grab data from the file I do this:

Code: Select all

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
     string temp;   //Just a dump for data I don't need
     string weapName;
     int minDamage, maxDamage;

     ifstream file("weapon.txt");
          file >> temp;
          file >> temp;
          file >> temp;
          file >> temp;
          file >> temp;
          file >> temp;
          file >> weapName;(longsword)
          file >> minDamage;(5)
          file >> maxDamage;(8)
     file.close();

     return 0;
}
I just want to quickly access certain sections of a file
to store in variables to use.

My question is if there is any better way to do this so
I dont have to make so many "file >>"? It gets annoying
when the file happens to get very big.

EDIT: I thought about maybe storing it in an STL container
then extracting from there but I am not sure which container
would be best...something to label the names of each weapon,
its min and max damage then just extract them into their
correct variables.

Re: File Streaming Question

Posted: Tue Jul 07, 2009 6:12 am
by programmerinprogress
you could use, a loop

Code: Select all

for(int i = 0; !file.eof();i++) // while stream isnt end of file 
{
     switch(i)// keep track of place in stream 
     {
         case 6:  file >> weapName;break; // 7th line down
         case 7:  file >> minDamage;break;  // 8th line down 
         case 8:  file >> maxDamage;break; // 9th line down 
         default: file >> temp;  break;  // filters out those first few things you dont want (fixed! :D)
     }

}
file.close(); 
I thought of that off the top of my head, i'm not sure whether the for loop condition would work, but i'm guessing it would since it just equates to a boolean value.

by the way, .eof() just means end of file, using this will allow the compiler to determine when end of file has been reached, the loop will then finish.

tell me if this works, I think I understood what you were going for :lol:

Re: File Streaming Question

Posted: Tue Jul 07, 2009 6:17 am
by zodiac976
Thanks, I will test it out and see if it does work. I should
have thought of something like that myself....my brain is
rusty or something :lol:.

Re: File Streaming Question

Posted: Tue Jul 07, 2009 6:31 am
by Netwatcher
The obvious answers are always the ones we are ignorant to.

Re: File Streaming Question

Posted: Tue Jul 07, 2009 6:32 am
by zodiac976
Actually that did work but I had to change a few things
to get it to work, here is the working code :mrgreen:

weapon.txt

Code: Select all

knife
1
2
shortsword
2
5
longsword
5
8
main.cpp

Code: Select all

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	string garbage, weapName;
	int minDamage, maxDamage;

	ifstream file("weapon.txt");

	for(int i = 0; !file.eof(); ++i)   // while stream isn't end of file
	{
		switch(i)   // keep track of place in stream
		{
			case 6:
				file >> weapName;  // 7th line down
			case 7:
				file >> minDamage; // 8th line down
			case 8:
				file >> maxDamage; // 9th line down
			default:
				getline(file, garbage);   //I did this to dump unwanted data, it won't work otherwise
				break;
		}
	}

	file.close(); 

	cout << weapName << " (" << minDamage << "-" << maxDamage << ")\n";
	cin.get();

	return 0;
}

Re: File Streaming Question

Posted: Tue Jul 07, 2009 7:06 am
by programmerinprogress
oh yeah, duh, it wouldnt take in any data, because the stream doesn't pick up any of the data, you could just file >> temp on the default part and then break afterwards, it would probably look more consistent, also, you wouldn't have to rely on everything being on a seprate line, which would be a pain

Re: File Streaming Question

Posted: Tue Jul 07, 2009 7:18 am
by K-Bal
You are lucky that your code works. You need a break for each case.

Re: File Streaming Question

Posted: Tue Jul 07, 2009 7:49 am
by programmerinprogress
K-Bal wrote:You are lucky that your code works. You need a break for each case.

wow, i'm sloppy today, of course you do!

in this case, because 'i' can only be one value which doesn't get altered until after each iteration, a break statement is not essential, but really, you need to put one in there, just incase you expand the program and end up changing the value of i before the next interation of the loop.

long story short, my bad :lol:


EDIT: my poor excuse for that blunder is the fact that I find it incredibly hard to format my code in a BBS style window, you can't use tab, and you cant backspace unless youre sure that the text area is selected or you lose thw whole thing! so when i'm doing all of these checks, I tend to miss out these details, such as breaks, but like I said that's a pretty lame excuse

Re: File Streaming Question

Posted: Tue Jul 07, 2009 7:59 am
by zodiac976
K-Bal wrote:You are lucky that your code works. You need a break for each case.
I usually do breaks after each case but the code still
works either way.

Re: File Streaming Question

Posted: Tue Jul 07, 2009 8:00 am
by zodiac976
programmerinprogress wrote:
K-Bal wrote:You are lucky that your code works. You need a break for each case.

wow, i'm sloppy today, of course you do!

in this case, because 'i' can only be one value which doesn't get altered until after each iteration, a break statement is not essential, but really, you need to put one in there, just incase you expand the program and end up changing the value of i before the next interation of the loop.

long story short, my bad :lol:


EDIT: my poor excuse for that blunder is the fact that I find it incredibly hard to format my code in a BBS style window, you can't use tab, and you cant backspace unless youre sure that the text area is selected or you lose thw whole thing! so when i'm doing all of these checks, I tend to miss out these details, such as breaks, but like I said that's a pretty lame excuse
That's why I left the breaks out but anyways good point :P

Re: File Streaming Question

Posted: Tue Jul 07, 2009 8:06 am
by zodiac976
programmerinprogress wrote:oh yeah, duh, it wouldnt take in any data, because the stream doesn't pick up any of the data, you could just file >> temp on the default part and then break afterwards, it would probably look more consistent, also, you wouldn't have to rely on everything being on a seprate line, which would be a pain
Yep, if you use file >> it pulls the data until a white space but
if you use getline() it pulls everything until a newline. I guess
it depends on the format of your file.

Re: [Solved] File Streaming Question

Posted: Fri Jul 17, 2009 7:06 pm
by MarauderIIC
I did the line number thing for a while. Then I got tired of screwing with it when I went to change it. So instead, I formatted my data in the file in key-value pairs. Like this:

Code: Select all

<room>
num=0
desc=Your mom and pop are here
items=1,0
</room>
<room>
num=1
items=1
desc=Boogie room
</room>
So I read the whole thing. Searched for <room>. Got all lines between <room> and </room>. Split the entries on '='. Used a STL map<string, string>. Then added the key as whatever was before = and the value as whatever was after =. Error checking for invalid keys. Then using them was like cout << myData[MY_DESC];