从输入文件反转的行只做一行

Line reversal from input file only doing one line?

本文关键字:一行 文件 输入      更新时间:2023-10-16

我试图弄清楚为什么我在使用文件中的 getline 时只得到一行。我使用反向翻转所有字母,然后将字符串转换为字符串流。然后,我将每个单词从流中提取出来,并按照提取它们的顺序反转它们。因此,如果我正在拉行"这是一条线",则输出是"行 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;

}

我能够通过清除字符串流并从开始时重新开始位置来解决问题。

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