文件流中怎样读入空格

pintime 2009-01-20 09:16:36
比如文件:

2008-12-12 12:01 I Love You.

/* "2008-12-12 12:01" 与"I Love You." 之间用'\t'分隔 */


in >> data >> char;

怎样 data <----- "2008-12-12 12:01"

char <----- "I Love You."

忽略掉空格。
...全文
235 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyisnail 2009-01-20
  • 打赏
  • 举报
回复

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

int main()
{
char data[128] = {0};
char str[128] = {0};

ifstream in("in.txt",ios::in);

while(in)
{
in.getline(data, 128, '\t');
cout<<data<<endl;
in.getline(str, 128, '\t');
cout<<str<<endl;
}

return 0;
}


文件内容:

2008-12-12 12:01 I Love You.
2009-01-20 11:11 I Need You.


运行结果:

2008-12-12 12:01
I Love You.
2009-01-20 11:11
I Need You.
waizqfor 2009-01-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jiangheng2008 的回复:]
C/C++ code
string str;
in >> str;
[/Quote]
MS 直接定义成字符串 直接读就可以了 想显示的时候 判断'\t'分割就OK了
xuruichen 2009-01-20
  • 打赏
  • 举报
回复
getline函数应该可以吧
zjfhgdx 2009-01-20
  • 打赏
  • 举报
回复
getline
zhaojia268115 2009-01-20
  • 打赏
  • 举报
回复
直接读!!!!
ljmscsq 2009-01-20
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int find(string line)
{
int n=0;
while(line[n]!='\t')
{
n++;
}
return n;
}
int main()
{
ifstream input("aa.txt");
string data;
string str;

string line;
while(getline(input,line,'\n')) //按行读文件
{
int pos=find(line); //找到\t的位置
data=line.substr(0,pos); //提取\t前子串
str=line.substr(pos+1,line.length());//提取\t后子串

cout<<data<<endl;
cout<<str<<endl;
}
return 0;
}

jiangheng2008 2009-01-20
  • 打赏
  • 举报
回复

string str;
in >> str;
nullah 2009-01-20
  • 打赏
  • 举报
回复
貌似不行

不过可以安空格分割每行的内容
然后在组合好了
icesky_ff 2009-01-20
  • 打赏
  • 举报
回复
好像不能直接读入空格吧
你可以找个替换字符先放那,等你读出来的时候再替换回来
nullah 2009-01-20
  • 打赏
  • 举报
回复
汗...
没看到楼主说的间隔是\t

65,210

社区成员

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

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