69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
struct student
{
char xuehao[10];
char name[10];
float score[3];
} stu[3];
int main( void )
{
FILE *stream;
int numwritten,numread;
student stu2[3];
//initialize data
strcpy(stu[0].xuehao,"14");
strcpy(stu[1].xuehao,"12");
strcpy(stu[2].xuehao,"16");
strcpy(stu[0].name,"名在");
strcpy(stu[1].name,"我自己");
strcpy(stu[2].name,"服务机构");
stu[0].score[0] = 88.0f;
stu[0].score[1] = 66.0f;
stu[0].score[2] = 99.0f;
stu[1].score[0] = 99.0f;
stu[1].score[1] = 99.0f;
stu[1].score[2] = 99.0f;
stu[2].score[0] = 99.0f;
stu[2].score[1] = 99.0f;
stu[2].score[2] = 93.0f;
// Open file in text mode:
if( (stream = fopen("fread.out", "w+t" )) != NULL)
{
// Write 25 characters to stream
numwritten = fwrite( stu, sizeof( student ), 3, 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( stu2, sizeof( student ), 3, stream );
printf( "Number of items read = %d\n", numread );
printf( "My name is = %s\n", stu2[1].name );
fclose( stream );
}
else
printf( "File could not be opened\n" );
}