从输入文件换行只做一行?

时间:2016-02-17 04:12:12

标签: c++ loops reverse getline stringstream

我想弄清楚为什么我在使用文件中的getline时只获得一行。我正在使用reverse来翻转所有字母,然后将字符串转换为字符串流。然后我将每个单词拉出流并按照我提取它们的顺序将它们反转。因此,如果我正在拉线"这是一条线",输出是"线a是这个"。我的问题是它只是获得第一行然后停止。我认为它与换行符有关,但我试图使用.ignore无济于事。我没有.ignore也尝试过。任何帮助将不胜感激。

int main()
{
        fstream inFile;
        fstream outFile;
        string fileName("");
        string destName("");
        char c = 0;
        string wrdRev("");
        int numCount = 0;
        int capCount = 0;
        int lowCount = 0;
        int wordCount = 0;
        int charCount = 0;
        string buffer("");
        istringstream strInput;
        string output("");
        string lineRev("");

        cout << "Please enter file name: ";
        getline(cin, fileName);
        cout << endl;

        inFile.open(fileName, ios::in);

        if (inFile.good() != true) {
                cout << "File does not exist!\n" << endl;
                return 0;
        }
        else{
                reverse(fileName.begin(), fileName.end());
                destName += fileName;
        }


        outFile.open(destName, ios::in);

        if (outFile.good() == true){
                cout << "File '" << destName << "' already exists!\n" << endl;
                return 0;
        }
        else {
                outFile.clear();
 outFile.open(destName, ios::out);


                while(inFile.get(c)){

                        if(isupper(c)){
                                capCount++;
                        }
                        else if(islower(c)){
                                lowCount++;
                        }
                        else if(isdigit(c)){
                                numCount++;
                        }
                        else if(isspace(c)){
                                wordCount++;
                        }
                }

                inFile.clear();
                inFile.seekg(0, inFile.beg);

                while(inFile >> buffer){
                        charCount = charCount + buffer.length();
                }

                inFile.clear();
                inFile.seekg(0, inFile.beg);

                while(getline(inFile, wrdRev)){
                        inFile.clear();
                        reverse(wrdRev.begin(), wrdRev.end());

                        strInput.str(wrdRev);

                        while(strInput >> output){
                                strInput.ignore();
                                reverse(output.begin(), output.end());
                                cout << output << " ";
                        }
                        inFile.clear();
                        inFile.ignore(8192, '\n');
                        cout << endl;
                }

                outFile << "There are " << capCount << " upper-case letters." << endl;
                outFile << "There are " << lowCount << " lower-case letters." << endl;
                outFile << "There are " << numCount << " digits." << endl;
outFile << "There are " << charCount << " characters." << endl;
                outFile << "There are " << wordCount << " words.\n" << endl;

        }

        inFile.close();
        outFile.close();

        return 0;


}

1 个答案:

答案 0 :(得分:0)

我能够通过清除字符串流并在开头回到位置来解决问题。

while(getline(inFile, wrdRev)){
    inFile.clear();
    reverse(wrdRev.begin(), wrdRev.end());
    strInput.str(wrdRev);
    strInput.clear();
    strInput.seekg(0);

    while(strInput >> output){
        strInput.ignore();
        reverse(output.begin(), output.end());
        outFile << output << " ";
    }           
}