Editing files using C

Viewed 117

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.

2 Answers

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

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.

Related