File Handling

Pages: 12
May 23, 2011 at 3:56pm
I guess I am understanding what a vector is.
I had a look at sort. Now I have need help understanding another thing.
When I retrieve The data in the files, it is in the form of a string. But I only want to compare a part [the score] of that string, that also when it is considered to be an float value. How can I do so?
May 23, 2011 at 4:41pm
atof http://cplusplus.com/reference/clibrary/cstdlib/atof is one way...

Que alternatives

Anyway, you have to parse the string by extrapolating the data you want.
Check out http://cplusplus.com/reference/string/string
string.find( ... ) string.substr( ... )
May 23, 2011 at 5:51pm
Mathhead200:
Que alternatives
Here's one, lol.

The way I prefer to get integers from a file then convert from a string to int/float, is to use stringstream. Check the post I made before:

http://www.cplusplus.com/forum/beginner/42893/#msg231937


store file data into vectors
mean?
I just meant to store the data from the file in the program while running. Then if a new score was made, you could sort and re-write back to the file.
Last edited on May 23, 2011 at 5:55pm
May 23, 2011 at 6:16pm
All these methods to convert string to float were very helpful. Thanks for them.
Correct me if I am wrong, the parameter entered in string.find() has to be an string literal?
If so, how am I to search for the score, since it can be anything below 40 (i.e. upto where ever long double will allow me to go in negative).
Another thing will be that I will have to look at individual lines. Any help on how to see just a single line of the file, before continuing on to the next?
May 23, 2011 at 7:15pm
You could use a certain file structure. And then get data from the file like so:

getline( stream, string, delimiter );
so:

1
2
3
4
fstream file( "test.txt" );
string input = " ";

getline( file, input, "::" );


This, will store into input, everything from the current line in the file until it finds ::

file:
name::87

In your case, you'd want to have something like this:

1
2
3
4
5
getline( file, input, "::" ); //This will get the name
//save the name somewhere

getline( file, input, '\n' ); //Now the second part of the line will be saved into input up until a newline( \n ) has been reached
//Here you would convert the string input into a new, suitable variable to sort 
Last edited on May 23, 2011 at 7:19pm
May 23, 2011 at 8:07pm
So the delimiter will act similar to a break point? stopping the code from reading the file further when it encounters the delimiter?
May 23, 2011 at 8:47pm
Yeah, that's right.
May 24, 2011 at 4:53am
When I compile the program I get the error that the getline function expects 2 arguments, 3 provided.
May 24, 2011 at 7:30am
Sorry, that would be my fault. I belive you need a constant:

std::getline( file, sName, '\t' );
std::getline( file, sName, '\n' );

Note the 3rd param in single quotes.

Here's some escape sequences:
http://msdn.microsoft.com/en-us/library/6aw8xdf2%28v=vs.71%29.aspx

Look in the bottom table on the site. You'll need to remake your file that the program reads from.
I tend to use the '\t', then when making the file, use a tab to seperate the data to be read.

Edit:
Ignore the "std::" before getline, I never type using namespace std; when I code, so std:: for me is needed.
Last edited on May 24, 2011 at 7:33am
May 24, 2011 at 7:32am
Ah! OK thanks. And I am already using tabs!
May 24, 2011 at 7:40am
No problem! Happy coding. lol.
May 24, 2011 at 7:42am
That worked. Thanks.
But I need to know another thing. Is there a way to count the number of lines in the file?
I will use it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ifstream high_scores ("high_score.txt");
while (high_scores.good())
{
	getline (high_scores,highscores,'\t');
	for (long long int x = 0; x < highscores.size();x++)
	{
		highscores[x] ^= Key;
		cout <<  highscores[x];
	}
	string the_scores;
	getline (high_scores,the_scores,'\n');
	for (long long int x = 0; x < the_scores.size();x++)
	{
		the_scores[x] ^=Key;
		cout << the_scores[x];
	}
	cout << endl;
}
user_score.close();
}


Now I was thinking of using a for loop that will repeat this whole segment inside the while loop so that I can get the data for each line.
May 24, 2011 at 7:47am
Create a variable, i.e count = 0

And when you collect the data from the file and you know you're at the end of the line. After the last data for the line is collected:

++count;

Then you'll have a variable with the correct number of lines.
Last edited on May 24, 2011 at 7:47am
May 24, 2011 at 8:28am
Please have a look at this code and tell me if there is something wrong with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
ofstream high_score ("high_score.txt",ios::out|ios::app);
for (int x = 0;x < 14;x++)
{
	UserName[x] ^= Key;
	high_score <<  UserName[x];
}
for (long long int x = 0;x < Name.size();x++)
{
	Name[x] ^= Key;
	high_score<<  Name[x];
}
for (long long int x = 0;x < 12;x++)
{
	Score[x] ^= Key;
	high_score << Score[x];
}
for (long long int x = 0;x < SCORES.size();x++)
{
	SCORES[x]^= Key;
	high_score << SCORES[x];
}
high_score << "\n";
high_score.close();

	ifstream high_scores ("high_score.txt");
	while (high_scores.good())
	{
	string the_scores;
	for (int count = 0; count < 10; ++count)
	{
		getline (high_scores,highscores,'\t');
		for (long long int x = 0; x < highscores.size();x++)
		{
			highscores[x] ^= Key;
			cout <<  highscores[x];
		}
								
		getline (high_scores,the_scores,'\n');
		for (long long int x = 0; x < the_scores.size();x++)
		{
			the_scores[x] ^=Key;
			cout << the_scores[x];
		}
		cout << endl;
	}
}
user_score.close();
}

because the output is :

User Name:      Nisheeth            Score : 40
########
ore:     40'
....
####### denotes gibberish, or rather text that is yet in encoded form (XOR Encryption)
.... repetition of the same thing as above

And there is always an extra space of about 5 lines.
By the way I haven't done any thing to sort out the scores, and the syntax is correct, the problem is somewhere in the algorithm.
Last edited on May 24, 2011 at 8:30am
May 24, 2011 at 6:58pm
Someone?
May 24, 2011 at 9:12pm
I would start by commenting out the encrypting lines and see if that's the problem, or if it's still not right (i.e. something else is the problem.)
May 29, 2011 at 9:05am
I tried it out and found that the problem is not in encryption, but in the extraction part. That is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

ifstream high_scores ("high_score.txt");
while (high_scores.good())
{
	string the_scores;
	string the_name;
	string aSCORE;
	for (int count = 0; count < 10; ++count)
	{
		getline (high_scores,highscores,'\t');
		for (long long int x = 0; x < highscores.size();x++)
		{
			//highscores[x] ^= Key;
			cout <<  highscores[x];
		}
								
		getline (high_scores,the_scores,'\n');
		for (long long int x = 0; x < the_scores.size();x++)
		{
			//the_scores[x] ^=Key;
			cout << the_scores[x];
		}
		cout << endl;
	}
}


If I don't use encryption, it returns:
Username: Nisheeth		Score: 30
		Score: 30
		Score: 30


The repetition is multiple times.
Any Idea about the problem?
Jun 1, 2011 at 6:18am
Your file stream is probably becoming corrupted or ending before the conditional check in the outer most loop (while loop). Note your have 3 nested loops here. My guess is the condition in the while should be added to the outer for-loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
...
string the_scores;
string the_name;
string aSCORE;
for (int count = 0; high_scores.good() && count < 10; ++count)
//                  ^^^^^^^^^^^^^^^^^^^^^
{
	getline (high_scores,highscores,'\t');
	for (long long int x = 0; x < highscores.size();x++)
	{
		//highscores[x] ^= Key;
		cout <<  highscores[x];
	}
							
	getline (high_scores,the_scores,'\n');
	for (long long int x = 0; x < the_scores.size();x++)
	{
		//the_scores[x] ^=Key;
		cout << the_scores[x];
	}
	cout << endl;
}
...
Last edited on Jun 1, 2011 at 6:19am
Topic archived. No new replies allowed.
Pages: 12