read word by word from file in C++

后端 未结 5 850
清歌不尽
清歌不尽 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

提交回复
热议问题