您的位置:首页 > 运维架构

fopen的巧妙用法--清空一个已经存在的文件,但不删除这个文件 fgets取文件一行

2012-07-28 10:28 239 查看
1、在打开文件的同时删除文件的内容。

FILE* fp;
fp = fopen("c:\test12.txt","w");
if(fp==NULL)
return 0;
fclose(fp);


有时候需要包含

#include <io.h>

"w" Opens an empty file for writing. If the given file exists, its contents are destroyed.

#include<stdio.h>
#include<stdlib.h>
#define true  1
#define false 0

int main (int argc ,char *argv[])
{
int line;
char buffer[80];
FILE *fp = fopen(argv[1], "r");//读取文件

while(fgets(buffer, sizeof(buffer), fp)){//取一行.客官请多多注意fgets的特殊用法!
line++;
}
line--;

printf("There are %d lines in the file %s \n",line,argv[1]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐