reading from file

Started by
4 comments, last by XDarkScar 20 years, 11 months ago

void load()
{
	ifstream fin;
	fin.open("Game Data.txt");
	string data;
	if(fin.fail())
	{
		cout << "Error, Unable to find Data File\n";
		return;
	}

	fin >> data >> gold;
	getline(fin, rank);
	fin >> data >> men;
	fin >> data >> a_men;
	fin >> data >> d_men;
	fin >> data >> spys;
	fin >> data >> army_tired;
	getline(fin, aname);
	getline(fin, cname);;

	fin.close();
}
   
game data.txt gold 0 rank Private * men 50 a_men 0 d_men 0 spys 0 army_tired 0 aname army's name * cname commanders name * I want to get the full name from the * can someone help me with this code? the code I have up there dosn't work, some efforts of mine.. Easy way of programming: Code, Graphics, Swearing.... [edited by - XDarkScar on April 30, 2003 12:03:17 AM]
Easy way of programming: Coding, Debugging, Swearing.
Google / Game Tutorials / GameDev.Net / My Project server. Check it out.
Project: N/A / Percent Complete: 0% / Due Date: N/A
Advertisement
Maybe I could stare at it some more, but I don''t really understand what you''re trying to do here.

Is:
quote:gold 0
rank Private *
men 50
a_men 0
d_men 0
spys 0
army_tired 0
aname army''s name *
cname commanders name *
The content of the file you are reading? Is that with or without the asterisks?

What do you mean by saying "the full name from the *"?

Please elaborate more, and provide details. In the code you have, you don''t declare gold, rank, men, a_men etc, so it''s hard to guess what you want to happen.

If this is something simple as reading in a configuration, I suggest you try a standard .ini file instead. Their format is:

[section1]
key1=value1
key2=value2
[section2]
key1=value1
...

And Windows'' provide functions to read and write them.
Look here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/registry_functions.asp

The list in the end of the document contains the functions of interest, eg:
GetPrivateProfileInt()
GetPrivateProfileSection()
GetPrivateProfileSectionNames()
GetPrivateProfileString()

ok let me reword it


game data.txt

gold 0
Private *
men 50
a_men 0
d_men 0
spys 0
army_tired 0
armys name *
commanders name *

the * are not included in the file, its the lines that I want 2 take the whole name from


Easy way of programming: Code, Graphics, Swearing....
Easy way of programming: Coding, Debugging, Swearing.
Google / Game Tutorials / GameDev.Net / My Project server. Check it out.
Project: N/A / Percent Complete: 0% / Due Date: N/A
If you decide to do your own configuration reading:

Guessing from your previous posts on the subject, you are trying to read something like:

John Smith

into a std::string object. With std::ifstream, the extraction operator reads until encountering a space. For example:

stuff.txt
-----------------

Bob McBoby


read_in.cpp
-----------------

#include <iostream>
#include <string>

int main()
{
std::ifstream inFile("stuff.txt");
if (inFile.fail())
{
std::cerr << "An error occured on opening stuff.txt";
// handle error, e.g. return -1;
}

std::string word;
while (!inFile.eof()) // while (inFile) or while (!inFile.eof())
{
inFile >> word; // reads one word in and assigns it 'word'
std::cout << "The word read is: " << word << std::endl;
}

// etc

return 0;
}



As you can see, the output would be:

The word read is: Bob
The word read is: McBoby


If I understand you correctly, you want to read in a string with spaces in it. Now, remember from your previous posts about std::getline()? Well, the first parameter for this function is an input stream. We used std::cin, but you can use your own input stream object that you created, inFile. For example:

stuff.txt
-----------------

Bob McBoby


read_in.cpp
-----------------

#include <iostream>
#include <string>

int main()
{
std::ifstream inFile("stuff.txt");
if (inFile.fail())
{
std::cerr << "An error occured on opening stuff.txt";
// handle error, e.g. return -1;
}

std::string line;
while (!inFile.eof()) // while (inFile) or while (!inFile.eof())
{
std::getline(inFile, line, '\n'); // reads the whole line and assigns it to 'line'
std::cout << "The line read is: " << word << std::endl;
}

// etc

return 0;
}



The output will be:

The line read is: Bob McBoby

Basically, what I'm trying to tell you in my ramble, is use the std::getline() function to read in the full names. Remember, though, that if a newline character ('\n') is left in the input stream, std::getline() will stop reading straight away as it will recognise it is the delimiter - have a look through your posts that I replied to.

Edit: Just noticed a formatting problem. :/

[edited by - Lektrix on May 1, 2003 11:46:03 AM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
ahh had a feeling I was doign it write, anyway tx, I had 2 edit it some, was taking in a lot of stuff ints strings ect, I got it working with fin.get();

Easy way of programming: Code, Graphics, Swearing....
Easy way of programming: Coding, Debugging, Swearing.
Google / Game Tutorials / GameDev.Net / My Project server. Check it out.
Project: N/A / Percent Complete: 0% / Due Date: N/A
Yes, if you don't know how streams work you can get some very unexpected results.

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on May 1, 2003 11:46:57 AM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]

This topic is closed to new replies.

Advertisement