C语言fread函数怎么用?

dhqiuhihqoqhoqhoqwhf 2010-06-12 06:16:14
#include<stdio.h>
#include<stdlib.h>

struct Books{
char name[30];
char author[20];
char press[20];
char presstime[10];
char c;
int number;
float price;
int id;
}bk[200],*bp,*bp1;
int count=20;
void main()
{
FILE *fp;
float f;
if((fp=fopen("tushuxinxi","ab"))==NULL){
printf("Cannot open the file!");
getchar();
exit(0);
}
printf("name:");
gets(bk[count].name);
printf("author:");
gets(bk[count].author);
printf("press:");
gets(bk[count].press);
printf("presstime:");
gets(bk[count].presstime);
printf("class:");
scanf("%c",&bk[count].c);
printf("number:");
scanf("%d",&bk[count].number);
printf("price:");
scanf("%f",&f);
bk[count].price=f;
bk[count].id=count;
bp=&bk[count];
fwrite(bp,sizeof(struct Books),1,fp);
printf("The book is already stored away!\n");
rewind(fp);
fread(bp1,sizeof(struct Books),1,fp);/*读取文件信息*/
puts(bp1->name); /*输出部分信息*/
fclose(fp);
}
编译和连接都没问题,但是当执行到fread语句时提示出错信息:Expression string!=NULL。不知道怎么回事,请各位帮帮忙!
...全文
219 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yunyun1886358 的回复:]
改好了, bp1没有分配内存空间。

C/C++ code

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

struct Books{
char name[30];
char author[20];
char press[20];
char presstime[10];
char c;
int number;
float price;
i……
[/Quote]运行没问题,但输出的信息是乱码。
yunyun1886358 2010-06-12
  • 打赏
  • 举报
回复
改好了, bp1没有分配内存空间。

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

struct Books{
char name[30];
char author[20];
char press[20];
char presstime[10];
char c;
int number;
float price;
int id;
}bk[200],*bp,*bp1;
int counter=20;
void main()
{
FILE *fp;
float f;
if((fp=fopen("tushuxinxi","ab"))==NULL){
printf("Cannot open the file!");
getchar();
exit(0);
}
printf("name:");
gets(bk[counter].name);
printf("author:");
gets(bk[counter].author);
printf("press:");
gets(bk[counter].press);
printf("presstime:");
gets(bk[counter].presstime);
printf("class:");
scanf("%c",&bk[counter].c);
printf("number:");
scanf("%d",&bk[counter].number);
printf("price:");
scanf("%f",&f);
bk[counter].price=f;
bk[counter].id=counter;
bp=&bk[counter];
fwrite(bp,sizeof(struct Books),1,fp);
printf("The book is already stored away!\n");
rewind(fp);
bp1 = new Books();
fread(bp1,sizeof(struct Books),1,fp);/*读取文件信息*/
puts(bp1->name); /*输出部分信息*/
fclose(fp);
delete bp1;
}
elegant87 2010-06-12
  • 打赏
  • 举报
回复

//MSDN
Reads data from a stream.

size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
buffer

Storage location for data

size

Item size in bytes

count

Maximum number of items to be read

stream

Pointer to FILE structure

#include <stdio.h>

void main( void )
{
FILE *stream;
char list[30];
int i, numread, numwritten;

/* Open file in text mode: */
if( (stream = fopen( "fread.out", "w+t" )) != NULL )
{
for ( i = 0; i < 25; i++ )
list[i] = (char)('z' - i);
/* Write 25 characters to stream */
numwritten = fwrite( list, sizeof( char ), 25, stream );
printf( "Wrote %d items\n", numwritten );
fclose( stream );

}
else
printf( "Problem opening the file\n" );

if( (stream = fopen( "fread.out", "r+t" )) != NULL )
{
/* Attempt to read in 25 characters */
numread = fread( list, sizeof( char ), 25, stream );
printf( "Number of items read = %d\n", numread );
printf( "Contents of buffer = %.25s\n", list );
fclose( stream );
}
else
printf( "File could not be opened\n" );
}


wade_2003 2010-06-12
  • 打赏
  • 举报
回复

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

struct Books{
char name[30];
char author[20];
char press[20];
char presstime[10];
char c;
int number;
float price;
int id;
}bk[200],*bp,*bp1;
int count=20;
void main()
{
FILE *fp;
float f;
if((fp=fopen("tushuxinxi","ab"))==NULL){
printf("Cannot open the file!");
getchar();
exit(0);
}
printf("name:");
gets(bk[count].name);
printf("author:");
gets(bk[count].author);
printf("press:");
gets(bk[count].press);
printf("presstime:");
gets(bk[count].presstime);
printf("class:");
scanf("%c",&bk[count].c);
printf("number:");
scanf("%d",&bk[count].number);
printf("price:");
scanf("%f",&f);
bk[count].price=f;
bk[count].id=count;
bp=&bk[count];
fwrite(bp,sizeof(struct Books),1,fp);
printf("The book is already stored away!\n");
rewind(fp);
bp1 = (struct Books*)malloc(sizeof(struct Books));//mark
fread(bp1,sizeof(struct Books),1,fp);/*读取文件信息*/
puts(bp1->name); /*输出部分信息*/
fclose(fp);
}


应该是bp1没有分配内存吧
liutengfeigo 2010-06-12
  • 打赏
  • 举报
回复
http://zhidao.baidu.com/question/81314373.html

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧