69,373
社区成员
发帖
与我相关
我的任务
分享
#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;
}
//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" );
}
#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);
}