1

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;
}
Germano Massullo
  • 2,572
  • 11
  • 40
  • 55

3 Answers3

4

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

unwind
  • 391,730
  • 64
  • 469
  • 606
1

In 'openFile()', you open the file with 'r', but 'fputs' wants to 'w' the file.

Kai
  • 165
  • 1
  • 2
  • 11
1

Hm: &stringVector[0] ?

Which is exactly the same as doing : a = 1 - 1, why don't you do a = 0?

-> stringVector = &stringVector[0]

SeedmanJ
  • 444
  • 2
  • 8