C系語法讀文件

 

1、C讀文件,讀一行方式

    FILE *file=fopen(filepath,"rb");//filepath文件名

    if (file)

    {

       char linebuf[1024];//讀unicode文件,改爲wchar_t linebuf[1024]

       CString strTemp;

       while (fgetws(linebuf,1024,file))

       {

           strTemp=wsline;

       }

    }

    fclose(file);


2、C++讀一行

//unicode 環境

#include   <fstream>  

#include   <iostream>

     std::wifstream wifs;

     wifs.open(m_strFilePath,std::ios_base::in);

     wchar_t buf[4096];

     int iCount=1;

     while(wifs.getline(buf,sizeof(buf)))

     {                   

      }


3、MFC讀一行

CStdioFile  file

file.Open(strFileName, CFile::modeReadWrite|CFile::modeCreate|CFile::modeNoTruncate);

CString strLine;

while(file.ReadString(strLine))

{

}

4、C#讀一行文件

     //讀取文件
      using (StreamReader sr = File.OpenText(path)) 
      {
        string s = "";
        while ((s = sr.ReadLine()) != null
        {
           Console.WriteLine(s);
        }
     }

 

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