
C++ fstreams and strings
#1
Posted 28 February 2009 - 02:58 AM
Register to Remove
#2
Posted 28 February 2009 - 08:35 PM
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");
#3
Posted 01 March 2009 - 12:21 AM
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.

The help you have been given is free. If you have been happy with our help please consider donating to support this forum.
If you would like to say thanks for the help I have given you please View My Profile and Leave a Comment.
Your encouragement is welcome.
#4
Posted 01 March 2009 - 09:10 AM
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...rial/files.html
Hope that helps.
Proud Graduate of the TC/WTT Classroom
At weekends (GMT) I may not be able to reply promptly due to various commitments. Please be patient and I will respond as soon as I can.My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here

Need help remembering those important computer maintenance tasks? Let SCars do it for you.

0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users