cfgetsfputs

w+ not working when trying to read file content


Code:

#include <stdio.h>

void main() {
    FILE *ptr;
    char buff[255];
    ptr = fopen("Test.txt", "w+");

    if (ptr != NULL) {
        printf("Success\n");
    }

    fputs("Hello", ptr);

    fgets(buff, 255, (FILE *)ptr);
    printf("%s", buff);
    fclose(ptr);
}

The file "Text.txt" has the content "Hello" when I opened it, but I just can't print it out with fgets. What did I do wrong here?


Solution

  • There are multiple problems in your code:

    Here is a corrected version:

    #include <stdio.h>
    
    int main(void) {
        FILE *ptr;
        char buff[255];
    
        ptr = fopen("Test.txt", "w+");
        if (ptr == NULL) {
            printf("Cannot open Test.txt\n");
            return 1;
        }
        printf("Success\n");
    
        fputs("Hello", ptr);
        rewind(ptr);
        if (fgets(buff, sizeof buff, ptr)) {
            printf("%s", buff);
        } else {
            printf("Cannot read from stream\n");
        }
        fclose(ptr);
        return 0;
    }