caddy-caddy
caddy-caddy

Reputation: 205

Reading a text file and printing its contents to the screen in C

I'm writing a function that reads the given file and prints its contents to the screen. At the moment I have the following:

int textdump(const char *filename)
{
  int n = 0;
  char ch;
  FILE *fileHandle;
  fileHandle = fopen(filename, "r");
  if ((fileHandle = fopen(filename, "r")) == NULL)
  {
    return -1;
    fclose(fileHandle);
  }
  while ((ch = fgetc(fileHandle) != EOF) )
  {
    printf("%c", ch);
    n++;
  }

  fclose(fileHandle);
  if (fclose(fileHandle) == EOF)
  {
    return EXIT_FAILURE;
  }
  return n;
}

The function reads the text files successfully and returns the number of characters in each file correctly. But then I tried to print the characters and now I even can't run the program - I get "Failed to run - doc cannot be null, can't parse test results".

Upvotes: 2

Views: 1719

Answers (4)

Shreevardhan
Shreevardhan

Reputation: 12641

This should be enough

int textdump(const char * filename) {
    int n = 0, c;
    FILE * fp = fopen(filename, "r");
    if (fp == NULL) return -1;
    while ((c = fgetc(fp)) != EOF) putchar(c), n++;
    fclose(fp);
    return n;
}

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

To summarize the issues with above code,

  • In your code, why you're fopen()/ fclose()-ing twice? Get rid of that part. ---------------(1)
  • You need not fclose() what is not already opened. ----------------------------------------------(2)
  • All the statements after return is of no effect. ------------------------------------------------------(3)
  • Take care of operator precedence while using fgetc(). -----------------------------------------(4)
  • fgetc() returns int value. Change acccordingly. -----------------------------------------------(5)

So, your code will look like

int textdump(const char *filename)
{
int n = 0;
int ch = 0;
FILE *fileHandle = NULL;
//fileHandle = fopen(filename, "r");  //not reqd  --- (1)
    if ((fileHandle = fopen(filename, "r")) == NULL){
    return -1;
    //fclose(fileHandle); // not reqd  --- (2), (3)
}
while ( (ch = fgetc(fileHandle)) != EOF ){   //notice here   -- (4), (5)
  printf("%c", ch);
  n++;
}

fclose(fileHandle);
/*
if(fclose(fileHandle) == EOF){ -- (1)
    return EXIT_FAILURE;
 }*/
 return n;
 }

Upvotes: 6

David Ranieri
David Ranieri

Reputation: 41017

int textdump(const char *filename)
{
    int n = 0;
    FILE *fileHandle;
    /* char ch; fgetc wants an int (able to store EOF) not a char */
    int ch;
    fileHandle = fopen(filename, "r");
    if (fileHandle == NULL){
        /* return -1; */ 
        /* fclose(fileHandle); */
        /* you can not close what is not oppened */
        perror("fopen");
        return -1;
    }
    while ((ch = fgetc(fileHandle)) != EOF) /* Beware of the precedence */
        printf("%c", ch);
        n++;
    }
    /* You don't need to check the result of fclose
    if(fclose(fileHandle) == EOF){ 
    return EXIT_FAILURE;
    }
    */
    fclose(fileHandle);
    return n;
}

Upvotes: 4

Gopi
Gopi

Reputation: 19864

while ( (ch = fgetc(fileHandle) != EOF) )

should be

while ( (ch = fgetc(fileHandle)) != EOF)

You need to take care of operator precedence. With the posted code ch = fgetc() will not be evaluated first as you think so add parenthesis to make sure that you bind them together as shown above.

Upvotes: 4

Related Questions