`
CreazyApple
  • 浏览: 61585 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics