使用C编辑文件

qmb5sa22  于 2022-11-04  发布在  Unix
关注(0)|答案(2)|浏览(151)

Input.txt:

File entry structures:
[0] ""  0       -1
[1] ""  0       -1
[2] ""  0       -1
[3] ""  0       -1
[4] ""  0       -1

我想创建一个新文件(output.txt)。C程序将第一个出现的“”替换为“Testfile”,这样看起来就像这样:
Output.txt:

File entry structures:
[0] "Testfile"  0       -1
[1] ""  0       -1
[2] ""  0       -1
[3] ""  0       -1
[4] ""  0       -1

我目前的工作:


# include <stdio.h>

int main() {
    FILE *input_file, *output_file;
    int  size, fblock;
    char index[81];
    char name[81];

    input_file = fopen("input.txt", "r");

    output_file = fopen("output.txt", "w");

    // ignore "File entry structures:" line when reading/writing

    while (fscanf(input_file, "%80s %80s %d %d", index, name, &size, &fblock) == 4) {
        fprintf(output_file, ... );
    }

我不知道该为fprintf的第二个参数提供什么,我的目标是以与输入文件相同的方式编写它。

prdp8dxp

prdp8dxp1#

你工作太辛苦了,而我拖延得太多了,这可以通过一个非常简单的状态机来实现:


# include <stdlib.h>

# include <stdio.h>

FILE * xfopen(const char *path, const char *mode);

int
main(int argc, char**argv)
{
    int c;
    int seen = 0;
    FILE *in = argc > 1 ? xfopen(argv[1], "r") : stdin;
    FILE *out = argc > 2 ? xfopen(argv[2], "r") : stdout;

    while( (c = fgetc(in)) != EOF ){
        if( seen == 0 && c == '"' ){
            int d = fgetc(in);
            if( d == '"' ){
                fputs("\"Testfile", out);
                seen = 1;
            } else {
                ungetc(d, in);
            }
        }
        fputc(c, out);
    }
    return 0;
}

FILE *
xfopen(const char *path, const char *mode)
{
    FILE *fp = path[0] != '-' || path[1] != '\0' ? fopen(path, mode) :
        *mode == 'r' ? stdin : stdout;
    if( fp == NULL ){
        perror(path);
        exit(EXIT_FAILURE);
    }
    return fp;
}
dxpyg8gm

dxpyg8gm2#

可能有很多方法可以解决这个问题,所以这里有一个建议的方法供你参考。在你的程序中,我添加了一些字符串检查和操作,以产生所需的结果,使文字“Testfile”与你的数据的第一条记录相关联。下面是你的程序的修订版本。


# include <stdio.h>

# include <string.h>

int main()
{
    FILE *input_file, *output_file;
    int  size, fblock, i;
    char index[81];
    char name[81];
    char h1[12], h2[12], h3[12]; /* Some work variables to bring across the heading for line #1 in the output file */

    input_file = fopen("input.txt", "r");

    output_file = fopen("output.txt", "w");

    // Ignore "File entry structures:" line when reading/writing

    i = fscanf(input_file, "%s %s %s", h1, h2, h3); /* The header line has three strings/words */

    fprintf(output_file, "%s %s %s\n", h1, h2, h3);  /* In the example the first line of the output file has this heading */

    while (1)
    {
        i = fscanf(input_file, "%80s %80s %d %d", index, name, &size, &fblock);

        if (i == -1) break;

        if (i == 4)
        {
            if (strcmp(index, "[0]") == 0)
            {
                strcpy(name, "\"Testfile\""); /* Replaces the null value with the literal */
            }
            fprintf(output_file, "%s %s %d %d\n", index, name, size, fblock);
        }
    }

   fclose(input_file);
   fclose(output_file);

   return 0;
}

我完成了“fprintf”函数的格式设置,以供您查看。此外,““中的“strcmp”和“strcpy”函数<string.h>用于提供“Record [0]"的搜索/替换。
当我使用您的示例数据运行此程序时,输出文件中的数据显示如下。

File entry structures:
[0] "Testfile" 0 -1
[1] "" 0 -1
[2] "" 0 -1
[3] "" 0 -1
[4] "" 0 -1

正如我所指出的,可能还有其他方法可以得到您想要的结果,但这应该为您提供一个起点。
希望能有所帮助。
祝你好运

相关问题