标签:style http io ar color os 使用 sp for
#include<stdio.h>
#defineF_PATH"d:\\myfile\\file.dat"
intmain(
void
)
{
FILE
*fp=NULL;
//需要注意
fp=
fopen
(F_PATH,
"r"
);
if
(NULL==fp)
{
return
-1;
//要返回错误代码
}
fclose
(fp);
fp=NULL;
//需要指向空,否则会指向原打开文件地址
return0;
}
#include<stdio.h>
#include<stdlib.h>//为了使用exit()
intmain()
{
charch;
FILE
*fp=NULL;
charfname[50];
//用于存放文件名
printf
(
"输入文件名:"
);
scanf
(
"%s"
,fname);
fp=
fopen
(fname,
"r"
);
//只供读取
if
(fp==NULL)
//如果失败了
{
printf
(
"错误!"
);
exit
(1);
//中止程序
}
while
((ch=
getc
(fp))!=EOF)
putchar
(ch);
fclose
(fp);
//关闭文件
return0;
}
#include<stdio.h>
FILE
*stream,*stream2;
intmain(
void
)
{
intnumclosed;
//Openforread(willfailiffile"crt_fopen.c"doesnotexist)
if
((stream=
fopen
(
"crt_fopen.c"
,
"r"
))==NULL)
//C4996
//Note:fopenisdeprecated;considerusingfopen_sinstead
printf
(
"Thefile‘crt_fopen.c‘wasnotopened\n"
);
else
printf
(
"Thefile‘crt_fopen.c‘wasopened\n"
);
//Openforwrite
if
((stream2=
fopen
(
"data2"
,
"w+"
))==NULL)
//C4996
printf
(
"Thefile‘data2‘wasnotopened\n"
);
else
printf
(
"Thefile‘data2‘wasopened\n"
);
//ClosestreamifitisnotNULL
if
(stream)
{
if
(
fclose
(stream))
{
printf
(
"Thefile‘crt_fopen.c‘wasnotclosed\n"
);
}
}
//Allotherfilesareclosed:
numclosed=_fcloseall();
printf
(
"Numberoffilesclosedby_fcloseall:%u\n"
,numclosed);
}
标签:style http io ar color os 使用 sp for
原文地址:http://www.cnblogs.com/sky-of-chuanqingchen/p/4123163.html