vc 各种读取txt文件的方法


一、

  CStdioFile

二、

  FILE* f = fopen("file name", "mode");
  char buff[size];
  fread(buff, size, 1, f);
  fclose(f);

三、

  //用MFC读文件
  CFile file("yourfile.txt",CFile::modeRead);

  char *pBuf;
  int iLen=file.GetLength();
  pBuf =new char[iLen 1];
  file.Read(pBuf,iLen);
  pBuf[iLen]=0;

  file.Close();
  MessageBox(pBuf);

四、

  //用C SDK 读文件

  HANDLE hFile;
  hFile=CreateFile("2.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  char ch[100];
  DWORD dwReads;
  ReadFile(hFile,ch,100,&dwReads,NULL);
  CloseHandle(hFile);
  ch[dwReads]=0;
  MessageBox(ch);*
五、

  用C读文件
  FILE *pFile=fopen("1.txt","rb");
  char *pBuf;
  fseek(pFile,0,SEEK_END);//移动文件指针到文件末尾
  int len=ftell(pFile);//获取当前文件指针在文件中的偏移量,Gets the current position of a file pointer.offset
  pBuf=new char[len];
  rewind(pFile);//将指针移动到文件头,Repositions the file pointer to the beginning of a file
  //也可以用fseek(pFile,0,SEEK_SET);
  fread(pBuf,1,len,pFile);
  pBuf[len]=0;
  fclose(pFile);

  MessageBox(pBuf);


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值