I am learning C in 2020 – Beginner – Open, Read, Write and Close file

 

Operation System : Windows 10

IDE : Visual Studio Code

Problem

Goal

Learn how to open, write, read and close a file given in as a command line argument

Pseudo-code

  • Read a given file
  • write contents into another out.txt file
  • Close file

Solution

Include dependencies

I added the errno header to track when errors are thrown. There may be a better way to track them, but for now, this is the only method I know. So if you have suggestions, I’ll accept them with open arms πŸ˜€ .

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

Begin…

Since the task involves obtaining the input file’s path from the command line arguments, we need to check if other arguments were passed asides from the name of the program. Luckily the first argument of the main function indicates the number of command line arguments passed.

int main(int argc, char *argv[])
{
    int SIZE = argc
    // first argument is file_in and next argument file_out
.....

Below, the code simply prints an error if no other command line arguments were provided.If there are two CLAs, the second argument is assumed to be the input file and is opened using the fopen function, which returns a FILE object. If the user typed in a third argument, the program takes it as the output file, where the content of the input file should be copied.

int errnum;
    FILE *in_file;
    FILE *out_file;
    if (SIZE <= 1)
    {
        fprintf(stderr, "No input file");
        return 0;
    }
    if (SIZE > 1)
    {
        in_file = fopen(argv[1], "r");
    }
    else
    {
        in_file = fopen("in.txt", "r");
    }
    if (SIZE > 2)
    {

        out_file = fopen(argv[2], "w");
    }
    else
    {
        out_file = fopen("out.txt", "w");
    }

    if (in_file == NULL)
    {
        errnum = errno;
        fprintf(stderr, "Error code for in_file was %d\n", errnum);
        return 1;
    }
    if (out_file == NULL)
    {
        errnum = errno;
        fprintf(stderr, "Error code for out_file was %d\n", errnum);
        return 1;
    }

After which, the code checks if the valid objects were returned after opening the input and output file. If a null was returned, an error is logged and the program is stopped.

Writing into file

Here the program reads 99 characters from the input stream, our successfully β€œr” mode opened file, and stores these characters in the line char array. All chars stored in the line variable are then written in the output file.

char line[100];
    while (fscanf(in_file, "%99[^\n]s", line) == 1)
    {
        fprintf(out_file, "%s\n", line);
    }

What is “%99[^\n]s” ?

This confused me at first, so I googled and stack overflowed. Apparently, this means:

Read 99 characters from the input stream and do not break when a space is encountered.

^ – ignore the following character

So every 99 chars are captured until a new line.

Read this stack overflow

Finally….

Close the files.

 fclose(in_file);
    fclose(out_file);
    return 0;
}

Run

C:\......\practice\c>gcc program.c -o a && a people.csv

Author Notes

This is most likely not the cleanest implementation, just a heads up.

Links

  1. FILE
  2. Errno header
  3. What is scanf [^\n]?