-
Filestream help!
Ok so I need to read from a file that the user will enter in. They then enter a search string to look for in the file. All I am trying to do here is just take it step by step and make sure everything works. I can get it to read the file name and print that out, I can get it to read the string name and print that out- but when I try to print out the "words" in the file- it will only print out the last word in my file. EX: file is named filename
contents of file are Hi my name is Jenna
search string is my
will print: filename my Jenna
How can i get it to print filename my Hi my name is Jenna ????
here is my code:
Code:
int main()
{
string filename;
string search;
string words;;
char ch;
ifstream name;
cout << "Please enter the name of the input file" << endl;
cin >> filename;
name.open(filename.c_str());
cout << "Please enter your search string" << endl;
cin >> search;
name >> words;
while(!name.eof())
{
name.get(ch);
name >> words;
}
cout << filename << " " << search << " " << words << endl;
}
I know if has something to do with name.get(ch) in thw while loop because that will only get one word but why is it getting the last word as opposed to the first word and also how can i make it get the whole content of the file?
Thanks!
-
In the while loop
Code:
string tmp;
while(!name.eof())
{
name.get(ch);
name >> words;
}
Everytime you take a character it moves the file pointer next. and also name >> words, replaces the contents of words
Code:
string tmp;
while(!name.eof())
{
name >> tmp;
words += " ";
words += tmp;
}
-
>> I know if has something to do with name.get(ch)
Actually, the name.get(ch) does nothing important. It reads in the whitespace between words, but that whitespace is ignored anyway when you read in the next word. So the name.get(ch) can be there or not, it doesn't matter. I'd remove it since it is unnecessary.
The problem is that if you want to print out each word in the file, you have to put the output inside the loop. Right now, you are reading each word in a loop, and when the loop ends (after the last word is read) it outputs the search word and the last word read.
Benzakhar's edited version would work, but I would keep it the way you have it because it lends itself better to the final solution (i.e. it uses each word read in individually within the loop as you will have to do when searching for the search string).
-
I realized my final solution was poor and I edited last minute before I saw your post :).
Wow what fast posting :D
-
re: filestream help
Ok well when I put the output inside the loop it does this:
Code:
Please enter the name of the input file
file
Please enter your search string
my
file my name
file my jenna
file my jenna
it still is only printing the last word in my file.
I want it to say
Code:
file my Hi my name is jenna
-
You should be able to figure out why that happens. Mold your output to do what you want.
If you want the file to be printed first, but not in the loop, then you should output it first, but not in the loop. Use that logic to do the rest. You don't have to end each output with endl. If you don't add that or a new line, then it will still all appear on the same line.