怎么读取文本文件的一行?

depotmen 2002-05-09 11:18:39
我用
ifstream infile("text.txt");
if (!infile) return;
else {
string name;
char tmp[MAX_PATH;
for (int i=0;;i++){
infile >>tmp[i];
cout <<tmp[i];
if (tmp[i] == '\0') {cout <<"\n";break;};}

这样能把它读完,但我发现好象不认文本文件里的换行~~~~
if (tmp[i]=="\n")....不会出现。
怎么能判断文本文件里的换行呢?
...全文
112 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
complayer 2002-05-09
  • 打赏
  • 举报
回复
istream& getline( char* pch, int nCount, char delim = '\n' );

istream& getline( unsigned char* puch, int nCount, char delim = '\n' );

istream& getline( signed char* psch, int nCount, char delim = '\n' );
支持UNIX文本文件的只有0x0A没有0x0D的文本文件
zhang1000 2002-05-09
  • 打赏
  • 举报
回复
#include <afx.h>
voohoo2000 2002-05-09
  • 打赏
  • 举报
回复
需要哪些头文件,我怎么总是string undeclared identifier
depotmen 2002-05-09
  • 打赏
  • 举报
回复
??按照我的思路不能判断文本的换行吗???
MFC我看不大懂呀~~~~~~~
白驼山宋兵甲 2002-05-09
  • 打赏
  • 举报
回复
console 一样可以用MFC 啊,
tianlinyi 2002-05-09
  • 打赏
  • 举报
回复
楼上的cstdiofile可以用的
depotmen 2002-05-09
  • 打赏
  • 举报
回复
??用MFC吗?需要什么头文件的吗?
PS:我是在CONSOLE下使用的~~~~~~~~~
白驼山宋兵甲 2002-05-09
  • 打赏
  • 举报
回复
CString csLine;
try
{
CStdioFile file("e:\\test.txt",CFile::modeRead);

while(file.ReadString(csLine))
AfxMessageBox(csLine);
}
catch(...)
{
AfxMessageBox("打开文件错");
}
白驼山宋兵甲 2002-05-09
  • 打赏
  • 举报
回复
CString csLine;
try
{
CStdioFile file("e:\\sn.txt",CFile::modeRead);

while(file.ReadString(csLine))
AfxMessageBox(csLine);
}
catch(...)
{
AfxMessageBox("打开文件错");
}
depotmen 2002-05-09
  • 打赏
  • 举报
回复
char buf[MAX_PATH];
ifstream infile("text.txt");
if (!infile) return;
else {
infile.getline(buf,MAX_PATH);
}


谢了!!!!!
kof99th 2002-05-09
  • 打赏
  • 举报
回复
上面的程序加上delete buf;
operaphantom 2002-05-09
  • 打赏
  • 举报
回复
那么麻烦做什么?
用fgets

char *fgets( char *string, int n, FILE *stream );
Parameters
string
Storage location for data
n
Maximum number of characters to read
stream
Pointer to FILE structure
kof99th 2002-05-09
  • 打赏
  • 举报
回复
忘了加delete buf了。加上先
kof99th 2002-05-09
  • 打赏
  • 举报
回复
char *buf=new char[MAX_LENGTH];
ifstream infile("text.txt");
if (!infile) return;
else {
infile.getline(buf,MAX_LENGTH);
}
depotmen 2002-05-09
  • 打赏
  • 举报
回复
tmd,还是不行,取不到空格,到底是怎么回事?
大家都嫌分少??
fangrk 2002-05-09
  • 打赏
  • 举报
回复
应该是ifstream
:(
fangrk 2002-05-09
  • 打赏
  • 举报
回复
是ifstream
:(
depotmen 2002-05-09
  • 打赏
  • 举报
回复
自己找到答案了~~~~~~~~~~~
depotmen 2002-05-09
  • 打赏
  • 举报
回复
????
'getline' : undeclared identifier
'ifile' : undeclared identifier
'InputFile' : undeclared identifier
fangrk 2002-05-09
  • 打赏
  • 举报
回复
string store;
getline(cin,store);
//
ifile InputFile("c:\\aaa.txt");
getline(InputFile,store);
加载更多回复(1)

69,377

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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