c++怎样分行的读取txt文本里面的内容

yujunraining 2010-01-12 12:18:03
如题
...全文
1241 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mr_Right 2011-10-10
  • 打赏
  • 举报
回复
学习……
zzcmx2008 2010-01-31
  • 打赏
  • 举报
回复
fgets()
cq_gongyoulong 2010-01-30
  • 打赏
  • 举报
回复
用fgets读fopen打开的文件指针,fgets读取遇到回车换行结束~接着读取下一行
StephenGaoYu 2010-01-30
  • 打赏
  • 举报
回复
学习
yzx714 2010-01-30
  • 打赏
  • 举报
回复
C++当然是getline最方便
ruite1227 2010-01-30
  • 打赏
  • 举报
回复
学习了
wyjjsn 2010-01-30
  • 打赏
  • 举报
回复
用文件流的write
tycoonliu 2010-01-30
  • 打赏
  • 举报
回复
学习,我也是菜鸟一个
驴被脑袋踢了 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 milaguo 的回复:]
8楼的胸前的 勋章丫!!!  口水
[/Quote]

同感!
yujunraining 2010-01-29
  • 打赏
  • 举报
回复
为什么这个getline可以换行读取啊 那个循环不是只是循环四次的么?[Quote=引用 18 楼 djh512 的回复:]
C/C++ code
#include<iostream>
#include<fstream>
#include<string>usingnamespace std;int main()
{int i;
ofstream ofile("text.txt",ios_base::out);for(i=0;i<4;i++)
{
ofile<<"Zhang"<<i<<end?-
[/Quote]
chengsiping 2010-01-16
  • 打赏
  • 举报
回复
逛逛 加点分
djh512 2010-01-16
  • 打赏
  • 举报
回复

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
int i;
ofstream ofile("text.txt",ios_base::out);
for(i=0;i<4;i++)
{
ofile<<"Zhang"<<i<<endl;
}
ofile.close();

string str;
char line[101];
ifstream ifile("text.txt",ios_base::in);
for(i=0;i<4;i++)
{
ifile.getline(line,100);
cout<<line<<endl;
}
ifile.close();

return 0;
}

yangch_nhcmo 2010-01-16
  • 打赏
  • 举报
回复
fgets();
jinjinClouded 2010-01-16
  • 打赏
  • 举报
回复
getline 能解决
chaiyanlin 2010-01-15
  • 打赏
  • 举报
回复
LZ的问题太抽象了,你这样问得话大家的回答都是getline啊,
一般读取文档内容是第一个要做的就是详细了解文档内容的格式。。。。
ok如果你确定你的内容都要存成char的话那就不废话了,
你说有多余的空格什么的。。。
首先你确定你的数组的size够大,如果size不够导致溢出的话就会出现你那种情况。
其次照常理来说getline是默认的读到换行符就停止,等再次使用getline绝对是从下一行读起的,
如果你确定了数组size够大的话,你可以这样;
in1.getline(c1,30);//读第一行;
in1.getline(c2,30);//你不是说有多余的空格吗,这一行就用它去掉多余字符;
in1.getline(c3,30);//读第二行;
然后cout看看结果
romeprince 2010-01-15
  • 打赏
  • 举报
回复
getline.....
milaguo 2010-01-15
  • 打赏
  • 举报
回复
8楼的胸前的 勋章丫!!! 口水
elegant87 2010-01-15
  • 打赏
  • 举报
回复

//正好前两天写过关于文件的程序,你看看吧
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

typedef struct{
char name[8];
char number[8];
char sex[4];
}DataType;

int main()
{
DataType one;
cout<<"Enter the information."<<endl;
cout<<"Name"<<setw(10)<<"Number"<<setw(10)<<"Sex"<<endl;
cin>>one.name>>one.number>>one.sex;
cout<<"Display the information."<<endl;
cout<<"Name"<<setw(10)<<"Number"<<setw(10)<<"Sex"<<endl;
cout<<one.name<<setw(10)<<one.number<<setw(10)<<one.sex<<endl;
cout<<"Save the information to file."<<endl;
ofstream outfile;
outfile.open("test.txt",ios::out);
if(!outfile.is_open())
{
cout<<"Can not open the file."<<endl;
exit(0);
}
else
{
outfile.clear();
outfile<<"Name"<<setw(10)<<"Number"<<setw(10)<<"Sex"<<endl;
outfile<<one.number<<setw(10)<<one.name<<setw(10)<<one.sex<<endl;
}
outfile.close();

cout<<"Read the information form the fiel"<<endl;
ifstream readfile;
readfile.open("test.txt",ios::in);
if(!readfile.is_open())
{
cout<<"Can not open the file"<<endl;
exit(0);
}
else
{
string line; //每次从文件读一行
while(!readfile.eof())
{
getline(readfile,line);
cout<<line<<endl;
}
}
readfile.close();
system("pause");
return 0;
}
wpt_007 2010-01-13
  • 打赏
  • 举报
回复
学习了
tbg1986 2010-01-12
  • 打赏
  • 举报
回复
这样子不知道可以不?
FILE * fp = fopen("111111.txt","r");
if(fp != NULL)
{
fread(tmc_version,sizeof(char),TMC_VERSION_LENGTH,fp);
fclose(fp);
}
Or
////////////////////////////////////////////////
char ch;
FILE *fp = fopen("2222222222222222.TXT","r");
if(fp == NULL)
{
// MessageBox(NULL,_T("Open 2222222222222222 .TXT fail"),_T("FAIL"),MB_OK+MB_TOPMOST);
return '?';
}
else
{
fscanf(fp,"%c",&ch);
fclose(fp);
return ch;
}
加载更多回复(11)

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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