我有一个函数,从文件中读取一定数量的字节,并将这些字节放入一个字符串 . 使用fread我可以打印缓冲区中每个元素的内容,b,但如果我尝试打印整个字符串(b)没有打印出来 . 这是在for循环中的代码中完成的;我打印b [i]的内容 . 然后在for循环之后我尝试打印b,然后将b的内容放入我创建的结构中以供其他地方使用 .
有关为什么会发生这种情况的任何想法?
void load_frame(struct Entry P[], int framenum){
char b[256];
FILE*fp;
char *temp = malloc(256);
fp = fopen("BACKING_STORE.bin", "rb");
printf("We opened the file\n");
fseek(fp, 256*framenum,SEEK_SET);
fread(b, sizeof(b), 1, fp);
fclose(fp);
int i;
printf("Looking at the contents of buffer: \n");
for(i = 0; i < 10; i ++){
printf("%u\n", b[i]);
}
//Print contents of whole buffer
printf("%s",b);
//b[10] = "\0";
//Put b into char* content part of structure Entry
P[framenum].content = b;
printf("Content is: %s\n", P[framenum].content);
P[framenum].frame=framenum;
}