使用C 替换文件中的字符
Replace a character in a file using C++
我的输入文本文件为4行,每行是80个字符的固定长度。我想用一个空间替换每个逗号。我编写了如下所示的代码,并编译并以代码::块IDE运行。问题在于输出文件包含额外的线路,以帮助我纠正错误。我是C 的初学者。
inputfile
outputfile
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream in("circArc.txt", ios::in | ios::binary);
if(!in)
{
cout << "Cannot open file";
return 1;
}
ofstream out("readInt.txt", ios::out | ios::binary);
if(!out)
{
cout << "Cannot open file";
return 1;
}
string str;
char rep[80]; //replace array
while(in)
{
getline(in,str);
for(int i=0; i<80; i++)
{
if(str[i] == ',')
rep[i] = ' ';
else
rep[i] = str[i];
out.put(rep[i]);
}
out << endl;
}
in.close();
out.close();
return 0;
}
使用C 替换文件中字符的一种方法。
#include <iostream>
#include <fstream>
int main()
{
std::fstream fs("testFile.txt", std::fstream::in | std::fstream::out);
if (fs.is_open()) {
while (!fs.eof()) {
if (fs.get() == ',') {
fs.seekp((fs.tellp() - static_cast<std::streampos>(1)));
fs.put(' ');
fs.seekp(fs.tellp());
}
}
fs.close();
} else {
std::cout << "Faild to open" << 'n';
}
return 0;
}
使用
的问题while(in)
{
getline(in,str);
是您没有检查getline
是否成功。无论如何,您都在使用str
。
替换
while(in)
{
getline(in,str);
...
}
while(getline(in,str))
{
...
}
保持while()循环,但要在while循环之前/外部进行一次,然后在最后一行中内部循环:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream in("circArc.txt", ios::in | ios::binary);
if(!in)
{
cout << "Cannot open file";
return 1;
}
ofstream out("readInt.txt", ios::out | ios::binary);
if(!out)
{
cout << "Cannot open file";
return 1;
}
string str;
char rep[80]; //replace array
getline(in,str);
while(in)
{
for(int i=0; i<80; i++)
{
if(str[i] == ',')
rep[i] = ' ';
else
rep[i] = str[i];
out.put(rep[i]);
}
out << endl;
getline(in,str);
}
in.close();
out.close();
return 0;
}
我认为这里的问题是您的while
循环中的退出条件。您可以使用:
while(getline(in, str)) {
if (in.eof()) {
break;
}
/** This will get you out if you are reading past the end of file
* which seems to be the problem.
*/
...
问题在于,如果存在std::getline
,则可以删除末端字符,因此您不能(轻松)(轻松)确定最终字符是否是线结束。
在我看来,您不需要为此任务的数据格式而造成疑问,因此您可以一次处理一个字符:
ifstream in("circArc.txt", ios::in | ios::binary);
ofstream out("readInt.txt", ios::out | ios::binary);
for(char c; in.get(c); out.put(c))
if(c == ',')
c = ' ';
如果您真的想按行处理行处理,则需要检查读取的行是否包含一个线末端字符,并且如果输入中有一个,则仅在输出中包含一条线之端:
ifstream in("circArc.txt", ios::in | ios::binary);
ofstream out("readInt.txt", ios::out | ios::binary);
for(std::string line, eol; std::getline(in, line); out << line << eol)
{
// only add an eol to output if there was an eol in the input
eol = in.eof() ? "":"n";
// replace ',' with ' '
std::transform(std::begin(line), std::end(line), std::begin(line),
[](char c){ if(c == ',') return ' '; return c; });
}
相关文章:
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 如何从文本文件中读取数值,直到遇到字符类型?
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 尝试将字符串从文件读取到无符号字符向量中
- 如何在C++中反转文本文件中字符的顺序?
- 生成随机.txt文件,10行,10列。充满随机:"*"、"#"、"@"。然后将随机字符替换为'P'
- 如何存储 unicode 字符并将其输出到文件?
- 无法将整个文本文件复制到字符数组
- 仅读取文件末尾的最后 3 个字符,并将其另存为 int
- 将未签名的字符写入输出文件
- 从我的 2DgridArray 中的文本文件中获取每个字符
- ifstream 读取的字符与文件中写入的字符不同
- VSCode C++调试文件名中包含中文字符的文件时插件损坏
- 从文本文件 (C++) 生成字符矩阵
- 使用指针编译代码后,.cpp文件将变为随机字符
- 在c++中读取文件时,它如何自动将字符放入字符数组,将数字放入整数变量
- 跨平台UTF-8字符文件数据编码/解码