C++ 输入文件中的行反转仅执行一行?
我试图弄明白为什么在使用文件中的getline时只得到一行。我使用反向翻转所有字母,然后将字符串转换为字符串流。然后,我从流中提取每个单词,并按照提取它们的顺序将它们颠倒过来。所以,如果我拉动这条线,这是一条线,输出是,这是。我的问题是,它只是得到第一行,然后停止。我以为这和新行的角色有关,但我试着使用。忽略,但没有用。我也试过了,没有。忽略。任何帮助都将不胜感激C++ 输入文件中的行反转仅执行一行?,c++,loops,reverse,getline,stringstream,C++,Loops,Reverse,Getline,Stringstream,我试图弄明白为什么在使用文件中的getline时只得到一行。我使用反向翻转所有字母,然后将字符串转换为字符串流。然后,我从流中提取每个单词,并按照提取它们的顺序将它们颠倒过来。所以,如果我拉动这条线,这是一条线,输出是,这是。我的问题是,它只是得到第一行,然后停止。我以为这和新行的角色有关,但我试着使用。忽略,但没有用。我也试过了,没有。忽略。任何帮助都将不胜感激 int main() { fstream inFile; fstream outFile;
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;
}
我能够通过清除stringstream并从一开始就重新开始这个位置来解决这个问题
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 << " ";
}
}
请发一封信。我们不能真正地了解你的代码有什么问题。你的调试结果在哪里?我对C++仍然很陌生,还没有学会如何使用调试器。Sorryl首先学习如何使用它。这是编程中的一项基本技能。没有理由授权堆栈溢出社区完成该部分。