在C ++中逐字逐句读取

时间:2013-12-04 10:21:07

标签: c++

此函数应逐字读取文件 它确实有效,直到最后一个字,运行停止

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();

    }
}

任何人都能看出问题是什么以及如何解决?

5 个答案:

答案 0 :(得分:42)

首先,不要循环while (!eof()),它将无法按预期工作,因为{<1}}将在读取失败后才会设置到文件结束。

其次,普通输入操作符eofbit在空格上分隔,因此可用于读取“单词”:

>>

答案 1 :(得分:9)

我已经为你编辑了这个功能,

void readFile()
{
    ifstream file;
    file.open ("program.txt");
    if (!file.is_open()) return;

    string word;
    while (file >> word)
    {
        cout<< word << '\n';
    }
}

答案 2 :(得分:2)

你在这里做的是从输入流一次读取一个字符,并假设所有字符在&#34;之间。 &#34;代表一个词。但它不太可能成为&#34; &#34;在最后一个字之后,这可能是它不起作用的原因:

"word1 word2 word2EOF"

答案 3 :(得分:2)

如果可以,我可以为您提供相同任务的新代码,在我的代码中,您可以创建一个所谓的&#39;文档&#39;(不是真的)并保存,并且可以再次打开。它也存储为字符串文件(不是文档)。 这是代码:

#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;
}

只需使用此代码并进行更改即可达到您的目的。 梦想大,想大,做大事

答案 4 :(得分:1)

正如其他人所说的那样,您可能只是在阅读文件的末尾,因为您只是在检查x != ' '。相反,你还必须在内部循环中检查EOF(但在这种情况下不要使用char,而是使用足够大的类型):

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();
}

但话又说回来,你可以使用流的流媒体运营商,这些运营商已经在空白处分开(并且更好地考虑了多个空格和其他类型的白色空间):

void readFile(  )
{
    std::ifstream file("program.txt");
    for(std::string word; file >> word; )
        std::cout << word << '\n';
}

更进一步,您可以使用标准算法完全摆脱手动循环:

#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"));
}
相关问题