1

I've written a program to mess around with writing pointers into files(fwrite) and reading into pointers from files(fread). However the program doesn't seem to write a single thing into the file, nor does it seem to read anything from the file; it just prints the final incrementation of my pointer 5 times and exits. Can anyone spot the error/mistake in my syntax that seems to be doing this?

#include <stdio.h>

int main() {
    FILE *fTest;
    int *testPtr;
    int x = 10;
    
    if ((fTest = fopen("test.c", "wb")) == NULL) {
        printf("Error!");
    }

    testPtr = &x;
    int i;
    for (i = 0; i < 5; i++) {
        fwrite(testPtr, sizeof(int), 1, fTest);
        *testPtr += 1;
    }
    
    for (i = 0; i < 5; i++) {
        fread(testPtr, sizeof(int), 1, fTest);
        printf("%d", *testPtr);
    }

    fclose(fTest);
}

6
  • 1
    Use perror on failure of fopen Commented Jul 13, 2015 at 16:52
  • Are you aware that you are doing binary I/O? In many cases, textual I/O using some human-readable format (perhaps JSON...) is preferable and much easier to debug. Commented Jul 13, 2015 at 16:54
  • You might want to learn about rewind and fseek Commented Jul 13, 2015 at 16:55
  • check the returned value from fwrite() and fread() to assure the operations are successful. 'wb' does not enable reading the file. suggest using 'w+b' for the mode, After writing several integers to the file, the file pointer will be at the end of the file. To read what was previously written, must set the 'file pointer' back to the beginning of the file. Suggest using fseek() to perform that operation. Commented Jul 13, 2015 at 19:37
  • when the file open fails, the posted code is displaying a message and continuing to execute the code. However, there will be no valid pointer in fTest. suggest using perror() to display the correct message, the using exit() to exit the program. Note: exit() requires the stdlib.h header Commented Jul 13, 2015 at 19:44

3 Answers 3

5

Steps to take:

  1. Write the data to the file.
  2. Close the file.
  3. Open the file again in read mode.
  4. Read the data from the file.

That should work.

Also, the output file name, test.c, seems a bit strange. Is that on purpose?

#include <stdio.h>

int main() {
    FILE *fTest;
    int *testPtr;
    int x = 10;
    char const* file = "test.data"; // Using .data instead of .c

    testPtr = &x;

    int i;

    // Write the data.
    if ((fTest = fopen(file, "wb")) == NULL) {
        printf("Error!");
    }
    for (i = 0; i < 5; i++) {
        fwrite(testPtr, sizeof(int), 1, fTest);
        *testPtr += 1;
    }

    fclose(fTest);

    // Read the data.
    if ((fTest = fopen(file, "rb")) == NULL) {
        printf("Error!");
    }

    for (i = 0; i < 5; i++) {
        fread(testPtr, sizeof(int), 1, fTest);
        printf("%d", *testPtr);
    }

    fclose(fTest);
}
Sign up to request clarification or add additional context in comments.

1 Comment

yeah its just a file name I assigned to it. Like I said I'm just messing around with files and pointers to see how I can implement both, I'll be deleting it after.
1

Left aside the fact that you don't check thre return value of fwrite() I would assume that you do write into "test.c", after you run the program the file should exist with a size of 5 * sizeof(int) bytes. But you can't read from it for two reasons:

  1. you open the file write-only. Change "wb" to "w+b" to allow reading
  2. after writing, you must reset the read-write pointer to the beginning of the file: call fseek(fTest, 0, SEEK_SET ); before reading

Comments

1

The problem is that you're reading from the file while it's opened in write mode.

Add this code between your write loop and read loop and it will work:

fclose(fTest);
if ((fTest = fopen("test.c", "rb")) == NULL) {
    printf("Error!");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.