转载:如何一行一行地读取文本文件中的内容呢

来源:互联网 发布:江苏有线数据公司 编辑:程序博客网 时间:2024/04/26 20:03

如何一行一行地读取文本文件呢?

楼主vcfresh(fresh)2001-06-18 11:48:00 在 VC/MFC / 基础类 提问

    如何一行一行地读取文本文件中的内容呢?  
  请给出相关代码? 问题点数:20、回复次数:15Top

<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><iframe width="728" scrolling="no" height="90" frameborder="0" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3814784377435186&amp;dt=1179219245625&amp;lmt=1164894707&amp;prev_fmts=336x280_as&amp;format=728x90_as&amp;output=html&amp;channel=0427961622&amp;pv_ch=0427961622%2B&amp;url=http%3A%2F%2Fwww.80diy.com%2Fhome%2F20010618%2F11%2F162395.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=333333&amp;color_url=999999&amp;color_border=FFFFFF&amp;ad_type=text&amp;ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fcomplete%3D1%26hl%3Dzh-CN%26newwindow%3D1%26q%3DCString%2B%25E8%25AF%25BB%25E5%258F%2596%25E6%2596%2587%25E6%259C%25AC%25E6%2596%2587%25E4%25BB%25B6%26btnG%3DGoogle%2B%25E6%2590%259C%25E7%25B4%25A2%26meta%3D&amp;cc=100&amp;flash=9&amp;u_h=1024&amp;u_w=1280&amp;u_ah=957&amp;u_aw=1280&amp;u_cd=32&amp;u_tz=480&amp;u_his=1&amp;u_java=true&amp;u_nplug=27&amp;u_nmime=148" name="google_ads_frame"></iframe>

1 楼maxsuperkiller(狸猫)回复于 2001-06-18 11:57:00 得分 0

CFile   file;  
  char   buffer[102];  
  file.Open("文件名",CFile::Text);  
  do  
  {  
          file.Read(buffer,100);  
          strcat(buffer,"/n");  
  }while(file.IsEof());Top

2 楼vcfresh(fresh)回复于 2001-06-18 12:06:00 得分 0

上面的老兄代码有错,即使用strstr也不能用strcat吧  
  还有如果一行的buffer超过100,那也是错误的  
   
  如何逐行读入数据,有没有比较好的算法呢?Top

3 楼ahphone(阿丰--重出江湖,陌生的容颜)回复于 2001-06-18 12:16:00 得分 0

CStdioFile   Class   Members  
  ReadString   Reads   a   single   line   of   text.    
  WriteString   Writes   a   single   line   of   text.    
  Top

4 楼attackboy(我像是飘在城市的一片尘埃)回复于 2001-06-18 12:39:00 得分 0

u   can   use   the   api   function   for   reading   or   writting   just   like   an   ini   fileTop

5 楼waxmxinyu(飞鱼)回复于 2001-06-18 12:50:00 得分 0

用CStdioFile吧,  
  BOOL   CStdioFile::ReadString(CString&   rString)  
  挺好用的。  
  Top

6 楼waxmxinyu(飞鱼)回复于 2001-06-18 12:52:00 得分 5

CString   str;  
  CStdioFile   file;  
  file.Open("***.***",CFile::modeRead);  
  file.ReadString(str);Top

7 楼duststar(bug)回复于 2001-06-18 12:55:00 得分 5

#define   MAX   999  
  fstream   iof;  
  char   bb[MAX];  
  iof.getline(bb,MAX);  
  不需要用vc类裤:)Top

8 楼attackboy(我像是飘在城市的一片尘埃)回复于 2001-06-18 13:06:00 得分 0

请注意:是一行一行的,而不是一行的读。如果要读一行,由于确定不了长度,仅仅需要一个CSTING转换就可以办到!  
  Top

9 楼wangyi03(西湖上划船的)回复于 2001-06-18 13:20:00 得分 0

用while一个一个字符的读,直到读到回车换行Top

10 楼SmartHeart(女孩其实很傻,不知道谁真的爱她!)回复于 2001-06-18 13:25:00 得分 0

fgets!!!!!Top

11 楼vcfresh(fresh)回复于 2001-06-18 14:30:00 得分 0

CString   str;  
  CStdioFile   file;  
  file.Open("***.***",CFile::modeRead);  
  file.ReadString(str);    
   
  用CStdioFile很好用,但如何判断已经读到文件末尾了呢?Top

12 楼vcfresh(fresh)回复于 2001-06-18 15:01:00 得分 0

??Top

13 楼111222(www.111222.cn)回复于 2001-06-18 15:05:00 得分 10

CString   str1,str2;  
  CStdioFile   file;  
  file.Open("***.***",CFile::modeRead);  
  while(file.ReadString(str2))  
  {  
          str1   =   str1   +"/r/n"+   str2;  
  }  
  file.Close();  
  AfxMessageBox(str1);//显示出来  
   
  给分吧!!!!  
   
  Top

14 楼personnel(无忌)回复于 2001-06-18 15:07:00 得分 0

用feof()   判断文件结束。Top

15 楼aceplus(飞狼0723)回复于 2001-06-18 15:08:00 得分 0

//   .h  
   
  #include   "afxtempl.h"  
   
  class   CTextDoc   :   public   CDocument  
  {  
  ...  
  public:  
        ...  
        virtual   void   Serialize(CArchive&   ar);       //   overridden   for   document   i/o  
        ...  
  public:  
      //...  
      CList   <CString,CString&>   m_text;  
      ...  
  }  
   
   
  //   .cpp  
  void   CTextDoc::Serialize(CArchive&   ar)  
  {  
  CString   str;  
  if   (ar.IsStoring())  
  {  
  //   TODO:   add   storing   code   here  
  POSITION   pos=m_text.GetHeadPosition();  
  while(pos!=NULL)  
  {  
  str=m_text.GetNext(pos);  
  ar.WriteString(str+CString("/r/n"));  
  }  
  }  
  else  
  {  
  //   TODO:   add   loading   code   here  
  while(ar.ReadString(str))  
  {  
  //   将文本读入m_text  
  m_text.AddTail(str);  
  }  
  }  
  }  
   
  呵呵,我也来充一把大虾!!  
   
  Top

你可能对以下的问题也比较感兴趣....

  • VC++中如何对文本文件一行一行的读取
  • 如何指定读取文本文件的某一行
  • 如何用CFile读取文本文件的一行
  • 如何用CFile读取文本文件的一行
  • 如何读取文本文件中的每一行?
  • 如何一行行的读取文本文件的内容?
  • 文本文件,如何读取、删除、写入某一行
  • 白送分!!!如何一次读取文本文件一行内容?
  • 请问如何把文本文件的一行作为一个string来读取?
  • 如何读取文本文件?
 
原创粉丝点击