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

vcfresh 2001-06-18 11:48:00
如何一行一行地读取文本文件中的内容呢?
请给出相关代码?
...全文
454 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
duststar 2001-06-18
  • 打赏
  • 举报
回复
#define MAX 999
fstream iof;
char bb[MAX];
iof.getline(bb,MAX);
不需要用vc类裤:)
waxmxinyu 2001-06-18
  • 打赏
  • 举报
回复
CString str;
CStdioFile file;
file.Open("***.***",CFile::modeRead);
file.ReadString(str);
waxmxinyu 2001-06-18
  • 打赏
  • 举报
回复
用CStdioFile吧,
BOOL CStdioFile::ReadString(CString& rString)
挺好用的。
attackboy 2001-06-18
  • 打赏
  • 举报
回复
u can use the api function for reading or writting just like an ini file
ahphone 2001-06-18
  • 打赏
  • 举报
回复
CStdioFile Class Members
ReadString Reads a single line of text.
WriteString Writes a single line of text.
vcfresh 2001-06-18
  • 打赏
  • 举报
回复
上面的老兄代码有错,即使用strstr也不能用strcat吧
还有如果一行的buffer超过100,那也是错误的

如何逐行读入数据,有没有比较好的算法呢?
maxsuperkiller 2001-06-18
  • 打赏
  • 举报
回复
CFile file;
char buffer[102];
file.Open("文件名",CFile::Text);
do
{
file.Read(buffer,100);
strcat(buffer,"\n");
}while(file.IsEof());
aceplus 2001-06-18
  • 打赏
  • 举报
回复
// .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);
}
}
}

呵呵,我也来充一把大虾!!

personnel 2001-06-18
  • 打赏
  • 举报
回复
用feof() 判断文件结束。
111222 2001-06-18
  • 打赏
  • 举报
回复
CString str1,str2;
CStdioFile file;
file.Open("***.***",CFile::modeRead);
while(file.ReadString(str2))
{
str1 = str1 +"\r\n"+ str2;
}
file.Close();
AfxMessageBox(str1);//显示出来

给分吧!!!!

vcfresh 2001-06-18
  • 打赏
  • 举报
回复
??
vcfresh 2001-06-18
  • 打赏
  • 举报
回复
CString str;
CStdioFile file;
file.Open("***.***",CFile::modeRead);
file.ReadString(str);

用CStdioFile很好用,但如何判断已经读到文件末尾了呢?
SmartHeart 2001-06-18
  • 打赏
  • 举报
回复
fgets!!!!!
Android 2001-06-18
  • 打赏
  • 举报
回复
用while一个一个字符的读,直到读到回车换行
attackboy 2001-06-18
  • 打赏
  • 举报
回复
请注意:是一行一行的,而不是一行的读。如果要读一行,由于确定不了长度,仅仅需要一个CSTING转换就可以办到!

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧