标签:style class code c ext color
fread是一个函数。从一个文件流中读数据,最多读取count个元素,每个元素size字节,如果调用成功返回实际读取到的元素个数,如果不成功返回 0。
#include<stdio.h>
#include<string.h>
intmain(
void
)
{
FILE
*
stream;
charmsg[]=
"thisisatest"
;
charbuf[20];
if
((stream=
fopen
(
"DUMMY.FIL"
,
"w+"
))==NULL){
fprintf
(stderr,
"Cannotopenoutputfile.\n"
);
return0;
}
fwrite
(msg,1,
strlen
(msg)+1,stream);
fseek
(stream,0,SEEK_SET);
fread
(buf,1,
strlen
(msg)+1,stream);
printf
(
"%s\n"
,buf);
fclose
(stream);
return0;
}
#include<stdio.h>
intmain(
void
)
{
FILE
*
stream;
charlist[30];
inti,numread,numwritten;
/*Openfileintextmode:*/
if
((stream=
fopen
(
"fread.out"
,
"w+t"
))!=NULL)
{
for
(i=0;i<25;i++)
list[i]=(
char
)(
‘z‘
-i);
/*Write25characterstostream*/
numwritten=
fwrite
(list,
sizeof
(
char
),25,stream);
printf
(
"Wrote%ditems\n"
,numwritten);
fclose
(stream);
}
else
printf
(
"Problemopeningthefile\n"
);
if
((stream=
fopen
(
"fread.out"
,
"r+t"
))!=NULL)
{
numread=
fread
(list,
sizeof
(
char
),25,stream);
printf
(
"Numberofitemsread=%d\n"
,numread);
printf
(
"Contentsofbuffer=%.25s\n"
,list);
fclose
(stream);
}
else
printf
(
"Filecouldnotbeopened\n"
);
}
标签:style class code c ext color
原文地址:http://www.cnblogs.com/alsofly/p/3725248.html