[Solved] File Streaming Question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

[Solved] File Streaming Question

Post 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.
Last edited by zodiac976 on Tue Jul 07, 2009 8:08 am, edited 1 time in total.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: File Streaming Question

Post 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:
Last edited by programmerinprogress on Tue Jul 07, 2009 7:50 am, edited 2 times in total.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: File Streaming Question

Post 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:.
User avatar
Netwatcher
Chaos Rift Junior
Chaos Rift Junior
Posts: 378
Joined: Sun Jun 07, 2009 2:49 am
Current Project: The Awesome Game (Actual title)
Favorite Gaming Platforms: Cabbage, Ground beef
Programming Language of Choice: C++
Location: Rehovot, Israel

Re: File Streaming Question

Post by Netwatcher »

The obvious answers are always the ones we are ignorant to.
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655

blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: File Streaming Question

Post 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;
}
Last edited by zodiac976 on Tue Jul 07, 2009 8:03 am, edited 1 time in total.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: File Streaming Question

Post 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
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: File Streaming Question

Post by K-Bal »

You are lucky that your code works. You need a break for each case.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: File Streaming Question

Post 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
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: File Streaming Question

Post 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.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: File Streaming Question

Post 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
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: File Streaming Question

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: [Solved] File Streaming Question

Post 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];
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply