Home > Blockchain >  Read a file specified as an argument and return its' lines
Read a file specified as an argument and return its' lines

Time:11-18

I have an exercise in which I have to read a file containing strings and I have to return the content using one/multiple arrays (this is because the second part of this exercise asks for these lines to be reversed, I'm having problems - and therefore ask for help - with the input). So far, I have this:

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

#define LENGTH 1024

int main(int argc, char *argv[]){

    char* input[LENGTH];

    if(argc==2){

        FILE *fp = fopen(argv[1], "rt");

        if(fp!=NULL){

            int i=0;
            while(fgets(input, sizeof(input), fp)!=NULL){
                input[i] = (char*)malloc(sizeof(char) * (LENGTH));
                fgets(input, sizeof(input), fp);
                i  ;
            }
            printf("%s", *input);
            free(input);

        }
        else{
            printf("File opening unsuccessful!");
        }
    }
    else{
        printf("Enter an argument.");
    }

    return 0;

}

I also have to check whether or not memory allocation has failed. This program in its' current form returns nothing when run from the command line.

EDIT: I think it's important to mention that I get a number of warnings:

passing argument 1 of 'fgets' from incompatible pointer type [-Wincompatible-pointer-types]|

attempt to free a non-heap object 'input' [-Wfree-nonheap-object]|

EDIT 2: Example of input:

These
are
strings

... and the expected output:

esehT
era
sgnirts

In the exercise, it's specified that the maximum length of a line is 1024 characters.

CodePudding user response:

You probably want something like this.

Comments are in the code

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

#define LENGTH 1024

int main(int argc, char* argv[]) {

  if (argc == 2) {

    FILE* fp = fopen(argv[1], "rt");

    if (fp != NULL) {
      char** lines = NULL;         // pointer to pointers to lines read
      int nboflines = 0;           // total number of lines read

      char input[LENGTH];          // temporary input buffer

      while (fgets(input, sizeof(input), fp) != NULL) {
        char* newline = malloc(strlen(input)   1);        // allocate memory for line ( 1 for null terminator)
        strcpy(newline, input);                           // copy line just read
        newline[strcspn(newline, "\n")] = 0;              // remove \n if any
        nboflines  ;                                      // one more line
        lines = realloc(lines, nboflines * sizeof(char*)); // reallocate memory for one more line
        lines[nboflines - 1] = newline;                    // store the pointer to the line
      }

      fclose(fp);

      for (int i = 0; i < nboflines; i  )    // print the lins we've read
      {
        printf("%s\n", lines[i]);
      }
    }
    else {
      printf("File opening unsuccessful!");
    }
  }
  else {
    printf("Enter an argument.");
  }

  return 0;
}

Explanation about removing the \n left by fgets: Removing trailing newline character from fgets() input

Disclaimers:

  • there is no error checking for the memory allocation functions
  • memory is not freed. This is left as an exercise.
  • the way realloc is used here is not very efficient.
  • you still need to write the code that reverses each line and displays it.

You probably should decompose this into different functions:

  • a function that reads the file and returns the pointer to the lines and the number of lines read,
  • a function that displays the lines read
  • a function that reverses one line (to be written)
  • a function that reverses all lines (to be written)

This is left as an exercise.

  • Related