0

I am writing a C program in Ubuntu operating system. I provide an input text file containing string of characters.The program is required to read and manipulate ONE CHARACTER at time and print the whole content of input file in reverse.

When I compile program, I get following error:

reverseFileContent.c: In function ‘main’:
reverseFileContent.c:51:16: warning: passing argument 1 of ‘feof’ makes pointer from integer without a cast [-Wint-conversion]
    while(!feof(inputFile))

Can you please explain the error message to me.

Below is the program:

 #include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


#define BUFFER_SIZE 256
int main (int argc, char *argv[]){
    FILE  *inputFile, *outputFile; 
    int fileSize;
    int pointer;
    char buffer[BUFFER_SIZE];

    /* Check for correct user's inputs. */
    if( argc !=3 ) {
        fprintf(stderr, "USAGE: %s inputFile outputFile.\n", argv[0]);
        exit(-1);
    }

    /* Make sure input file exists. */
    if( (inputFile = fopen(argv[1], O_RDONLY))) {
        fprintf(stderr, "Input file doesn't exist.\n"); 
        exit(-1);
    }

    /* Create output file, if it doesn't exist.  Empty the file, if it exists. */

    if((outputFile = fopen(argv[2], "a+"))) {
        fclose(inputFile);
        exit(-1);
     }


    /* Find the size of the input file. */
    fileSize = fseek(inputFile, 0, SEEK_END);


    /* Read input file and write to output file in reversed order.
           * Use lseek() to move the file pointer to the ith position.
                 * To set the file pointer to a position use the SEEK_SET flag in lseek().
                 */
    for(pointer=fileSize-1; pointer>=0; pointer--) {

        /* INSERT YOUR CODE HERE */
        /*
          * In a loop:
          * - Read input file into a buffer
          * Don't read one byte at a time! Try to read BUFFER_SIZE (i.e., 256) bytes at a time.
          * - Write content in the buffer to the output file
        */
        while(!feof(inputFile))
        {
            fgets(buffer, BUFFER_SIZE, inputFile); //reads 256 bytes at a time
            fputs (buffer , outputFile );
        }

    }

    fclose(inputFile);
    fclose(outputFile);

    return(0);
}
Chuchoo
  • 823
  • 2
  • 17
  • 36
  • 4
    `open` returns `int`. `feof` needs `FILE *`. You want to open your file with `fopen`. – user58697 Mar 16 '18 at 03:22
  • Also, [in addition to using `fopen` as user58697 suggests], `inputFile` and `outputFile` should be `FILE *` and not `int`. And, you should also use the corresponding `fclose` calls at the end. – Craig Estey Mar 16 '18 at 03:25
  • 3
    Also, you're using `feof` incorrectly: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – William Pursell Mar 16 '18 at 03:54
  • The function feof() tests the end-of-file indicator for the stream pointed to by stream, returning nonzero if it is set. The end-of-file indicator can only be cleared by the function clearerr(). – Tsakiroglou Fotis Mar 16 '18 at 09:31
  • Thank you all. I have modified and updated my program based on the feedback I got. Program compiles fine now. But output is not printed in outputFile when I execute program. What am I missing in program now ? – Chuchoo Mar 19 '18 at 04:48
  • I have updated the above program. – Chuchoo Mar 19 '18 at 04:49
  • 1
    Please update the text (not comment) to reflect your new question. You've fixed your original error, so the original question no longer makes sense. More important: All programmers must learn how to debug when they write something that doesn't work as intended. You'll be better off asking how to debug rather than that someone else debug your program for you. – Gene Mar 19 '18 at 04:58
  • Thanks Gene. Answer to this question is provided. I will open up another question now regarding how to debug with two arguments provided. Thank you all. – Chuchoo Mar 19 '18 at 13:18

0 Answers0