创建+写入
#include <iohead.h>
int main(int argc, const char *argv[])
{
FILE*p1=fopen("./aaa","w");
if(p1==NULL)
{
ERRLOG("fopen");
}
printf("创建成功!\n");
char str[]="hello";
if(EOF==fputs(str,p1))
{
printf("fputs error\n");
return -1;
}
if(EOF==fclose(p1))
{
ERRLOG("fclose");
}
return 0;
}
读取+创建
#include <iohead.h>
int main(int argc, const char *argv[])
{
FILE*p1=fopen("./aaa","r");
if(p1==NULL)
{
ERRLOG("fopen");
}
printf("创建成功!\n");
char str[128]={0};
while(1)
{
if(NULL==fgets(str,sizeof(str),p1))
{
printf("fgets error\n");
break;
}
}
FILE*p2=fopen("./bbb","w");
if(p2==NULL)
{
ERRLOG("fopen");
}
printf("yes\n");
if(EOF==fputs(str,p2))
{
printf("fputs error");
return -1;
}
if(EOF==fclose(p2))
{
ERRLOG("fclose");
}
if(EOF==fclose(p1))
{
ERRLOG("fclose");
}
return 0;
}
计算多少行
#include <iohead.h>
int main(int argc, const char *argv[])
{
FILE*p1=fopen("./4_fgets.c","r");
if(p1==NULL)
{
ERRLOG("fopen");
}
printf("创建成功!\n");
char str[128]={0};
int count=0;
while(1)
{
if(NULL==fgets(str,sizeof(str),p1))
{
printf("fputs error\n");
break;
}
count++;
}
printf("%d\n",count);
if(EOF==fclose(p1))
{
ERRLOG("fclose");
}
return 0;
}
135

被折叠的 条评论
为什么被折叠?



