C语言中怎么判断txt(文本文档)中是否存在内容

C语言中怎么判断txt(文本文档)中是否存在内容,第1张

#include <stdio.h>

fopen 打开一个文件

fseek 用SEEK_END移动到文件结尾

ftell取得文件流当前读写位置。

即取文件结尾的位置,判断一下值即可。

这3个函数可以搞定,具体用法自己查下就行了。

MFC里提供了一些其他的文件处理类,但上面那些是标准库的,通用。

#include<fstream>    // 文件流 

#include<iostream>   // 标准流 

#include<string>     // 字符串 

using namespace std // 使用std名字空间 

int main() 

     fstream _file

     _file.open("c://123.txt",ios::in)

     if(!_file)

     {

     ofstream inFile 

inFile.open("c://123.txt")   // 打开文件 

if(inFile.is_open())

   inFile << "0 0 0 0 0 " <<endl // 字符串写入文件 

inFile.close()  // 关闭文件 

  cout<<"c://123.txt"<<"文件写入完成!\n"

      }

      else

      {

          cout<<"c://123.txt"<<"文件存在!\n"

      }

      return 0

}

这样的结果满意吗,请采纳

#include <stdio.h>

int main()

{

    int i = 1

    FILE *file = NULL

    char name[FILENAME_MAX]

    do

        sprintf(name, "%d.txt", i++)

    while((file = fopen(name, "r")) == NULL)

    fclose(file)

    return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11934821.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存