使用C 替换文件中的字符

Replace a character in a file using C++

本文关键字:字符 文件 替换 使用      更新时间:2023-10-16

我的输入文本文件为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; });
}