[英]What I don't see the content of the file after reading?
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...我用
fwrite
写入文件,然后读取文件的内容,但是当我将其打印到缓冲区并通过“printf”将其打印到标准输出时,我什么也没看到...
#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 need to close fp1 before you can read from the same file, like this-您需要先关闭 fp1,然后才能读取同一个文件,就像这样-
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|
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.