cfputs

Simply fputs() example returns EOF


This simple program, gives me troubles in fgets(), returning EOF, that is an error value for fgets() I don't understand where is the problem

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

FILE* openFile(const char* path)
{
    FILE* file;
    file = fopen(path, "r");
    if(file == NULL)
    {
        perror(path);
        exit(EXIT_FAILURE);
    }
    return file;
}


int main()
{
    FILE* file;
    char stringVector[6] = "hello";
    file = openFile("/home/user/workspace/fputs/src/testo.txt");

    if(fputs(&stringVector[0], file) == EOF)
    {
        printf("error in fputs");
        fclose(file);
        exit(EXIT_FAILURE);
    }

    fclose(file);
    return 0;
}

Solution

  • You are opening the file for reading, yet trying to write data to it? That doesn't make sense.