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

来源:互联网 发布:无主之地2mac汉化 编辑:程序博客网 时间:2024/06/14 10:23

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 0int 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]);}