VC中怎麼讀取文件

[轉]http://www.cnblogs.com/carekee/articles/1687318.html

一、

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

發佈了21 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章