使用C编辑文件

发布于 2025-02-07 12:07:16 字数 960 浏览 1 评论 0原文

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的第二个参数上。 。我的目标是以与输入文件中相同的方式编写。

Input.txt:

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

I want to create a new file (output.txt). The C program replaces the first occurrence of "" with "Testfile", so it looks like:

Output.txt:

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

My work so far:

#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, ... );
    }
    

    

I'm lost on what to provide to the second parameter of fprintf. My goal is to write it in the same way as it was in the input file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云归处 2025-02-14 12:07:16

你努力工作。我拖延了太多。这可以使用非常简单的状态机来完成:

#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;
}

You're working too hard. And I am procrastinating too much. This can be done with a very simple state machine:

#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;
}
一人独醉 2025-02-14 12:07:16

可能有很多解决此问题的方法,因此,这是一种建议进行此操作的方法。在您的程序中,我添加了一些字符串检查和操纵,以产生与数据记录相关的字面“ 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”功能来自“&lt; string.h&gt;”用于提供“记录[0]”的搜索/替换。

当我使用您的示例数据运行此程序时,这是输出文件中的数据显示方式。

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

正如我指出的那样,可能还有其他方法可以找到您想要的结果,但这应该为您提供一个起点。

希望有帮助。

问候。

There probably are a lot of ways to solve this, so here is a suggested way to do this for your review. In your program, I added in some string checking and manipulation to produce the desired outcome of having the literal "Testfile" associated with the first record of your data. Following is a revised version of your program.

#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;
}

I completed the formatting for the "fprintf" function for your review. Also, the "strcmp" and "strcpy" functions from "<string.h>" were used to provide the search/replace for "Record [0]".

When I ran this program using your sample data, here is how the data in the output file appeared.

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

As I noted, there are probably other ways to get to the results you want, but this should provide a starting point for you.

Hope that helps.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文