1

I wrote to file with fwrite and after that, I read the content of file but when I print it into a buffer and print it to stdout by "printf" I see nothing...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

int main()
{
    uint8_t *my_buffer = malloc(64UL);

    FILE *fp1 = fopen("my_buffer_file", "w");
    fprintf(fp1, "0123456789012345678901234567890123456789012345678901234567890123");
    FILE *my_buffer_fd = fopen("my_buffer_file", "r" );
    int my_new_ret = fread(my_buffer, 1, 64UL, my_buffer_fd);
    int size = my_new_ret;
    printf("my_buffer = |%s|\n", my_buffer);
    printf("size = |%d|\n", size);


    return 0;
}

Someone know why it happens so?

  • You never closed the file you wrote to. – Scott Hunter Dec 22 '22 at 17:49
  • Buffering. You need to close the output file before you can read from it, to make sure everything gets written out. – Mark Ransom Dec 22 '22 at 17:50
  • @MarkRansom: Close it, or just `fflush` it to force the user-mode buffers to the kernel (where other handles can see the data, even if it hasn't yet been written to disk). Or use `setvbuf` to disable buffering before writing. – ShadowRanger Dec 22 '22 at 17:54
  • @ShadowRanger yeah, if I wanted to write out a full answer I would have mentioned that. – Mark Ransom Dec 22 '22 at 17:56
  • @ChrisTurner: It's not *quite* the same; this code likely wouldn't write the data even if the OP *had* included a newline, because while `printf` is going to `stdout` (which is typically line-buffered when printing to a terminal), this is printing to a file (which is almost always block-buffered by default, with a buffer size typically in the several hundred to several thousand bytes). Still covers the basic problem though, user-mode buffering means written data isn't reliably visible through anything *but* the handle which wrote it unless buffers are flushed or buffering is disabled. – ShadowRanger Dec 22 '22 at 17:56
  • 2
    Also, probably not a bad idea to check that the files actually opened. – Chris Dec 22 '22 at 17:56
  • @ShadowRanger yeah, not exactly the same, but still talking about buffering etc... and I couldn't see one specifically talking about writing to a file even though I'm sure it has been asked many times before – Chris Turner Dec 22 '22 at 18:00

1 Answers1

2

You need to close fp1 before you can read from the same file, like this-

int main()
{
    uint8_t  *my_buffer = malloc(64UL);
    FILE *fp1 = fopen("my_buffer_file", "w");
    fprintf(fp1, "0123456789012345678901234567890123456789012345678901234567890123");

    fclose(fp1);    // New line here

    FILE *my_buffer_fd = fopen("my_buffer_file", "r" );
    int my_new_ret = fread(my_buffer, 1, 64UL ,my_buffer_fd);
    int size = my_new_ret;
    printf("my_buffer = |%s| \n", my_buffer);
    printf("size = |%d|\n", size);
    return 0;
}

Output:

my_buffer = |0123456789012345678901234567890123456789012345678901234567890123| 
size = |64|
Chris
  • 26,361
  • 5
  • 21
  • 42
Burritoos
  • 38
  • 8