What the Tech logo

What the Tech? It's as easy as 1,2,3! ( Log In | Register )

Easy as 1,2,3!
Reply to this topicStart new topic
> C++ fstreams and strings, Not the output I'm expecting
txman
post Feb 28 2009, 02:58 AM
Post #1


Authentic Member
**

Group: Authentic Member
Posts: 30
Joined: 5-July 07
Member No.: 71,221
Operating System: Windows XP Home Edition SP2



Alright, I'm teaching myself C++, and I'm stuck on recieving information from files. Here's my code.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
int x = 0;
string out_wanker[500];
string input_wanker;
ofstream out_file ("testfile.txt");
cout<<"Give me a file stream!"<<endl;
getline(cin, input_wanker, '\n');
out_file<<input_wanker<<" "<<"zarghly";
out_file.close();
ifstream in_file ("testfile.txt");

do
{
in_file>>out_wanker[x];
cout<<out_wanker[x]<<" ";
++x;
}while (out_wanker[x] != "zarghly");

return 0;
}


Ignore the strange variable names, I'm a strange person. but what I'm trying to do is write a string and read it back in. My hope is that "zarghly" would work as a terminator for the loop. However, thats not how it's working.

Sample input.
This is a test of how to work with file streams.

Sample output.
This is a test of how to work with file streams. zarghly (followed by roughly 500 spaces and a termination message)

So why isn't my loop terminating as it should?
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies (1 - 3)
txman
post Feb 28 2009, 08:35 PM
Post #2


Authentic Member
**

Group: Authentic Member
Posts: 30
Joined: 5-July 07
Member No.: 71,221
Operating System: Windows XP Home Edition SP2



After a nights sleep, I looked at it again and got this as a solution.

as this file hadn't read in a value for out_wanker[x] yet, it didn't read it into the loop as having a value, so the value in it was blank when it checked it to terminate the loop.

ifstream in_file ("testfile.txt");

do
{
in_file>>out_wanker[x];
cout<<out_wanker[x]<<" ";
++x;
}while (out_wanker[x] != "zarghly");

changed that to

ifstream in_file ("testfile.txt");
in_file>>out_wanker[x];
do
{
cout<<out_wanker[x]<<" ";
++x;
in_file>>out_wanker[x];
}while (out_wanker[x] != "aaaaa");
Go to the top of the page
 
+Quote Post
appleoddity
post Mar 1 2009, 12:21 AM
Post #3


SuperMember
Group Icon

Group: Tech Team
Posts: 1,662
Joined: 7-January 09
From: Flint, Michigan
Member No.: 83,485
Operating System: Windows XP, Server 2003/2008, Linux



I'd have to pull out my C++ books because I don't use this language enough to remember these input/output operations. However, I'm not understanding your second post.

Your second code modification looks "iffy?"

If I am remembering things properly, you are reading a file into a character array one character at a time. You are using x as your pointer in the character array to determine where to store the next character read from the file.

So, heres what I see happening. I am going to reference your first posting because this second posting doesn't look right at all. You are reading a character in, and storing it in the character array out_wanker in the location pointed to by x. You are then incrementing the pointer. Finally, you check the character array against the string "zarghly". However, here's the problem. You are using the pointer x to determine where to start looking for the string "zarghly" at. At this point in the loop the X variable is always pointing to the next position right after the last character read.

Therefore, this is what you are doing. Lets say the out_wanker variable contains "abcd1234", and your pointer (x) is now equal to 8. You then attempt to read from the 8th position, the string "zarghly." All you will get is garbage, because the 8th position on forward has no data in it yet. Instead, you need to start reading from a earlier position in the character array to look for the string zarghly. Because the string "zarghly" is 7 characters long, you will need to use x - 7 as your starting position. The resulting comparison would look like this: out_wanker[x-7] != "zarghly". This way you are looking 7 characters back in the string to see if you have loaded the string "zarghly" in yet.

Now, a technicality persists here. If you haven't read at least 7 characters into the string yet, you will actually point to an out of bounds portion of memory again because x-7 will be less than the beginning of the string. So, you will have to make sure your program reads in 7 characters first before doing any comparisons looking for "zarghly".

My suggestion is that you use the "\n" character to signify when to stop reading. There are certainly better ways to do this, but to use the most basic commands for learning purposes you could just use the comparison (out_wanker[x-1] != '\n'). Your program would then read until the end of the line and stop.

Its late and I'm real tired, so I'm going to stop here, but I hope I cleared up what I see as happening in this program. I could be wrong because I don't remember a couple of things about how the input/output operations work and I'm not sure that you can even do a string comparison on a character array the way that you are doing it. But, I think it is ok. smile.gif I'll look it up at a better time.
Go to the top of the page
 
+Quote Post
jpshortstuff
post Mar 1 2009, 09:10 AM
Post #4


SuperHelper
Group Icon

Group: Classroom Teacher
Posts: 5,096
Joined: 28-April 07
From: UK
Member No.: 69,799
Operating System: Windows XP (Professional), Windows Vista (Home Business), Windows 7 (Ultimate), Ubuntu Linux



Would it not be easier to use the eof() function as your termination condition, rather than having a random string?

What would happen if your random string was part of the file? You would terminate prematurely.

Have a look at this link, see if it helps:
http://www.cplusplus.com/doc/tutorial/files.html

Hope that helps.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Time is now: 21st November 2009 - 11:54 PM
Advertisements do not imply our endorsement of that product or service. The forum is run by volunteers who donate their time and expertise. We make every attempt to ensure that the help and advice posted is accurate and will not cause harm to your computer. However, we do not guarantee that they are accurate and they are to be used at your own risk.
Member site: Alliance of Security Analysis Professionals | UNITE Against Malware
Memory Forums | Auto Repair Forum
© Geeks to Go, Inc. | All Rights Reserved | Privacy Policy