如何通过 C++ 在读取文本文件时忽略特定的新行
how to disregard specific new lines in reading from a text file by c++
无论在哪里,新行之后都有一个新行或("")和一个空格("),我想忽略"",只打印输出中的空格,我该怎么做?
这是一个示例:
newegg
bizrate
想要将其更改为:
newegg bizrate
我很困惑,因为我想我不能通过逐行阅读来做到这一点! 下面是我的粗略代码,我不知道如何继续...提前非常感谢。
ifstream file ("input.txt");
ofstream output("output.txt");
string line;
if(file.is_open())
{
while (!file.eof())
{
getline (file, line);
if (line.find("n"+' ') != string::npos)
{
??
}
这样做。函数 getline() 将读取到n
字符
getline(file, line);
cout<<line;
while (!file.eof())
{
getline(file, line);
if (line[0]==' ')
{
cout <<" "<<line;
}
else
{
cout <<"n"<<line;
}
}
函数getline()
(此处的文档)将读取并丢弃n
字符,因此无需在字符串中搜索它。
只需执行以下操作:
bool first = true;
while (!file.eof())
{
getline(file, line);
// you may want to check that you haven't read the EOF here
if (!first)
{
cout << " ";
}
else
{
first = false;
}
cout << line;
}
你可能想要这个:
#include <cctype>
#include <iostream>
#include <sstream>
int main() {
std::istringstream input(""
"neweggn"
" bizraten"
"End");
std::string line;
while(std::getline(input, line)) {
while(std::isspace(input.peek())) {
std::string next_line;
std::getline(input, next_line);
line += next_line;
}
std::cout << line << 'n';
}
}
请注意:EOF的测试可能是错误的。
相关文章:
- 使用新行和不使用新行读取文件
- 如何检测将文本文件读入 int 数组的新行
- (C++) 使用诅咒打印从文件中读入的新行
- 从文本文件读取时,如何知道是否已到达新行?
- 从文件中读取给我额外的新行字符C
- 使用 C++ 中的向量在新行上显示文本文件中的列
- 将 Unicode 字符存储在.txt文件中的新行中
- endl 不会在输出文件中创建新行
- 写入文件时需要帮助获取新行
- C++将字符串写入文本文件中的行;新行问题不起作用
- C++ 在文本文件中的特定字符后插入新行
- 如何通过 C++ 在读取文本文件时忽略特定的新行
- 使用getchar()获取字符并将其保存到每次C 的新行中的文件中
- 无法读取文件的新行
- 将新行字符 () 添加到 CSV 文件中
- 读取整数并在按新行时打印,直到C++文件末尾
- 如何跳到文件中的新行并重复循环
- seekp() 位置在文件中新行的开头
- C++如果未打开,则在文件末尾添加新行
- 复制另一个文件时,在新文件中创建新行