使用C替换文本文件中的行 [英] Replace line in text-file using C

查看:166
本文介绍了使用C替换文本文件中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C用heet更改文本文件中包含#符号的行.

I want to change lines which contain the # symbol in a text file with heet using C.

我已经尝试过这种方法,但是并不能完全解决问题,它只是替换了&字符.不会像我想要的那样覆盖整个字符串.

I have tried it this way, but it did not work thoroughly, it just replaces the characters & overwrites not the whole string, like I want.

还有其他技巧可以从文件中删除或删除整行吗?因此,我们可以轻松地替换它.

Is there any other trick to remove or delete a whole line from the file? So, we can easily replace it.

myfile.txt: (执行前)

Joy
#Smith
Lee
Sara#
Priyanka
#Addy

代码:

#include <stdio.h>
#include <string.h>

int main() {
    FILE *pFile;
    fpos_t pos1, pos2;
    int line = 0;
    char buf[68]
    char *p;
    char temp[10] = "heet";

    pFile = fopen("myfile.txt", "r+");

    printf("changes are made in this lines:\t");    
    while (!feof(pFile)) {
        ++line;
        fgetpos(pFile, &pos1);          

        if (fgets(buf, 68, pFile) == NULL)  
            break;

        fgetpos(pFile, &pos2);

        p = strchr(buf, '#');

        if (p != NULL) {
            printf("%d, " , line);
            fsetpos(pFile, &pos1);  
            fputs(temp, pFile);
        }
        fsetpos(pFile, &pos2);
    }

    fclose(pFile);
    return 0;
}

myfile.txt: (执行后)

Joy  
heetth  
Lee  
heet#  
Priyanka  
heety  

输出:

changes are made in this lines: 2, 4, 6,  

myfile.txt: (我想获取)

Joy  
heet  
Lee  
heet  
Priyanka  
heet  

推荐答案

执行所需操作的最佳方法是使用sed之类的实用程序.它比您(或我)所写的任何东西都更快,并且使用的内存更少.

The best way of doing what you want is to use a utility like sed. It is faster and uses less memory than anything you (or I) would write.

此外,让我们假设您想继续自己编写它.

That aside, let's assume you want to go ahead and write it yourself anyway.

文件就像一个长字节数组.如果要增加或减少一行的长度,它将影响文件其余部分中每个字节的位置.结果可能比原始结果更短(或更长时间).由于结果可能会更短,因此就地修改文件是个坏主意.

A file is just like a long array of bytes. If you want to increase or decrease the length of one line, it affects the position of every byte in the rest of the file. The result can be shorter (or longer) than the original. As the result can be shorter, modifying the file in place is a bad idea.

以下伪代码说明了一种简单的方法:

The following pseudo-code illustrates a simple approach:

open original file
open output file
allocate a line buffer that is large enough
read a line from the original file
do
  return an error if the buffer is too small
  manipulate the line
  write the manipulated line to the output file
  read a line from the original file
loop until read returns nothing

sed更加智能.我曾经看到有关sed的工作原理的解释,但是我的Google业力似乎找不到它.

sed does it much smarter. I once saw an explanation on how sed works, but my google karma can't seem to find it.

修改: 使用sed的方法:

How to do it using sed:

 sed -e 's/.*\#.*/heet/g' myfile.txt

sed的s或替代命令可以将一个字符串或正则表达式替换为另一字符串.

The s, or substitute, command of sed can replace one string, or regular expression, with another string.

以上命令解释为:

heet替换其中任何具有#的行.最后的g告诉sed全局执行此操作,即在整个文件中执行此操作.

replace any line that has a # somewhere in it with heet. The final g tells sed to do this globally, i.e. in the entire file.

Edit2: 缺省情况下,sed写入标准输出. 要重写文件,您应该将输出重定向到文件,然后重命名. 在Linux中,请执行以下操作(您可以使用system从C运行命令行内容):

By default, sed writes to standard output. To rewrite the file you should redirect the output to a file and then rename it. In linux, do the following (you can run command line stuff from C with system):

sed -e 's/.*\#.*/heet/g' myfile.txt > temp_file123.txt
rm myfile.txt
mv temp_file123.txt myfile.txt

来自C:

system("sed -e 's/.*\#.*/heet/g' myfile.txt > temp_file123.txt");
system("rm myfile.txt");
system("mv temp_file123.txt myfile.txt");

如果只想调用system来完成此操作,请将所有命令行内容放入Shell脚本中.

If you want to do it with just one call to system, put all the command line stuff in a shell script.

这篇关于使用C替换文本文件中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆