从输入文件反转的行只做一行
Line reversal from input file only doing one line?
我试图弄清楚为什么我在使用文件中的 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 << " ";
}
}
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 如何使代码打印文本文件中的第一行?
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 将字符串存储到文件下一行的变量中
- 为什么这个.c文件只有一行"../xx/xx.c"?
- 比较两个没有一行共同点的大文件
- 如何在c ++中以一行(水平)打印两个文件的数据?如给定的.这两种形式来自两个不同的文本文件
- 到达分隔符时跳到文本文件的下一行
- 从文本文件中读取一行并将其存储在 C++ 中的 2D 矢量中
- 从文本文件中随机选取一行
- 在获取从文件到矢量的每一行之后,数字将被更改或销毁
- 从文件中的一行读取特定数据-C++
- 从文本文件中的每一行读取数字,并计算数字重复的次数
- 为什么这一行不输出我的文本文件?
- 从文件读取并在一行中写入 cout
- 如何有效地读取C 中的最后一行文件
- 将一行文件读入对象中的两个变量