read word by word from file in C++

后端 未结 5 774
清歌不尽
清歌不尽 2020-12-01 12:53

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          


        
相关标签:
5条回答
  • 2020-12-01 13:03

    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

    0 讨论(0)
  • 2020-12-01 13:11

    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"));
    }
    
    0 讨论(0)
  • 2020-12-01 13:17

    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)
    {
        ...
    }
    
    0 讨论(0)
  • 2020-12-01 13:18

    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"
    
    0 讨论(0)
  • 2020-12-01 13:22

    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';
        }
    }
    
    0 讨论(0)
提交回复
热议问题