Reputation: 395
this function should read a file word by word and it does work till the last word, where the run stops
void readFile( )
{
ifstream file;
file.open ("program.txt");
string word;
char x ;
word.clear();
while ( ! file.eof() )
{
x = file.get();
while ( x != ' ' )
{
word = word + x;
x = file.get();
}
cout<< word <<endl;
word.clear();
}
}
any one see what is the problem and how it can be solved??
Upvotes: 25
Views: 200222
Reputation: 169
text file :
code :
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream file("DB_name_age.txt");
int index;
string name;
int age;
if(file.is_open())
{
while(file >>index >> name >>age)
{
cout << index <<" "<<name <<" "<<age << endl;
}
}else{
cout<< "file open fail" <<endl;
}
return 0;
}
Upvotes: 2
Reputation: 230
If I may I could give you some new code for the same task, in my code you can create a so called 'document'(not really)and it is saved, and can be opened up again. It is also stored as a string file though(not a document). Here is the code:
#include "iostream"
#include "windows.h"
#include "string"
#include "fstream"
using namespace std;
int main() {
string saveload;
cout << "---------------------------" << endl;
cout << "|enter 'text' to write your document |" << endl;
cout << "|enter 'open file' to open the document |" << endl;
cout << "----------------------------------------" << endl;
while (true){
getline(cin, saveload);
if (saveload == "open file"){
string filenamet;
cout << "file name? " << endl;
getline(cin, filenamet, '*');
ifstream loadFile;
loadFile.open(filenamet, ifstream::in);
cout << "the text you entered was: ";
while (loadFile.good()){
cout << (char)loadFile.get();
Sleep(100);
}
cout << "" << endl;
loadFile.close();
}
if (saveload == "text") {
string filename;
cout << "file name: " << endl;
getline(cin, filename,'*');
string textToSave;
cout << "Enter your text: " << endl;
getline(cin, textToSave,'*');
ofstream saveFile(filename);
saveFile << textToSave;
saveFile.close();
}
}
return 0;
}
Just take this code and change it to serve your purpose. DREAM BIG,THINK BIG, DO BIG
Upvotes: 2
Reputation: 45968
As others have said, you are likely reading past the end of the file as you're only checking for x != ' '
. Instead you also have to check for EOF in the inner loop (but in this case don't use a char, but a sufficiently large type):
while ( ! file.eof() )
{
std::ifstream::int_type x = file.get();
while ( x != ' ' && x != std::ifstream::traits_type::eof() )
{
word += static_cast<char>(x);
x = file.get();
}
std::cout << word << '\n';
word.clear();
}
But then again, you can just employ the stream's streaming operators, which already separate at whitespace (and better account for multiple spaces and other kinds of whitepsace):
void readFile( )
{
std::ifstream file("program.txt");
for(std::string word; file >> word; )
std::cout << word << '\n';
}
And even further, you can employ a standard algorithm to get rid of the manual loop altogether:
#include <algorithm>
#include <iterator>
void readFile( )
{
std::ifstream file("program.txt");
std::copy(std::istream_iterator<std::string>(file),
std::istream_itetator<std::string>(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
Upvotes: 2
Reputation: 2185
I have edited the function for you,
void readFile()
{
ifstream file;
file.open ("program.txt");
if (!file.is_open()) return;
string word;
while (file >> word)
{
cout<< word << '\n';
}
}
Upvotes: 11
Reputation: 4961
what you are doing here is reading one character at a time from the input stream and assume that all the characters between " " represent a word. BUT it's unlikely to be a " " after the last word, so that's probably why it does not work:
"word1 word2 word2EOF"
Upvotes: 2
Reputation: 409442
First of all, don't loop while (!eof())
, it will not work as you expect it to because the eofbit
will not be set until after a failed read due to end of file.
Secondly, the normal input operator >>
separates on whitespace and so can be used to read "words":
std::string word;
while (file >> word)
{
...
}
Upvotes: 61