Sign-up....

如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

如上,想在读取文件的时候 对>>运算符 只是对'\n' 做反应

[41 byte] By [msdn] at [2007-8-14 12:57:44]
# 1 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

string str;

getline(cin, str, '\n');

将连空格一起读出一行。不知道你是不是这个意思?

whyglinux at 2004-9-20 12:24:07 >
# 2 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

倘若我的文件格式是如此的

A\n 为字符串

a\tb 两个整数

B\n 为字符串

c\td 两个整数

该如何?

ghiewa at 2004-9-20 12:31:20 >
# 3 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

string str;

getline(cin, str, '\n');

他会读一完整的行,遇到回车结束。

比如有一行:

hello, 123, xyz, 程序

那么读后,str内容为:"hello, 123, xyz, 程序"

pleasehelpme at 2004-9-20 13:11:08 >
# 4 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

>> 倘若我的文件格式是如此的

>> A\n 为字符串

>> a\tb 两个整数

>> B\n 为字符串

>> c\td 两个整数

>> 该如何?

string str;

int a, b;

getline(filestream, str, '\n');

filestream >> a >> b;

由于下面的记录格式一样,重复上面两句即可。在读整数的时候会自动忽略空白字符(包括空格、制表、回车等)。

whyglinux at 2004-9-20 13:24:39 >
# 5 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

用cin读取 的好象系统就可以自动过滤空格吧

daylove at 2004-9-20 14:59:59 >
# 6 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

cin>>

是读到空格是就认为输入结束

hcj2002 at 2004-9-21 12:50:54 >
# 7 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

to whyglinux(山青水秀):

>> a\tb 两个整数

>> A\n 为字符串

>> c\td 两个整数

>> B\n 为字符串

string str;

int a, b;

filestream >> a >> b;

getline(filestream, str, '\n');

通不过

ghiewa at 2004-9-22 7:41:03 >
# 8 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

呵呵,UP!

101monster at 2004-9-22 7:48:25 >
# 9 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

自己 up了

ghiewa at 2004-9-22 12:22:35 >
# 10 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

为什么通不过,有什么错误现象?

whyglinux at 2004-9-22 14:01:07 >
# 11 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

up

ccj200308053 at 2004-9-22 14:42:09 >
# 12 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

请教:如果我要读一个2进制的文件,我想把文件的内容读到一个 char* 或者char[],应该怎么办?fstream碰到空格,换行什么的就认为文件结束了,我想把2进制的内容读出来,不用fstream出来换行什么的。

Magic_CY at 2004-9-23 13:55:15 >
# 13 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

ifstream file;

file.open("aa.dat",ios::binary);

char ch[1000];

for(int i = 0;i < 1000;i ++)

{

file>>ch[i];

}

但是在vc7里调试,发现一些特殊字符,比如:0x0a ,0x0d等被忽略了,就是说,文件的内容是:0x46,0x0d,0x00,0x72。。。。。。读出来变成:0x46,0x00,0x72。。。。。。,有些字符被忽略了。

Magic_CY at 2004-9-23 14:24:46 >
# 14 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

用whyglinux(山青水秀):的方法应该是可以的。

pacman2000 at 2004-9-23 14:28:30 >
# 15 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

>> a\tb 两个整数

>> A\n 为字符串

>> c\td 两个整数

>> B\n 为字符串

string str;

int a, b;

filestream >> a >> b;

getline(filestream, str, '\n');

不行得,读完b后 其后得\n存在 会在执行

getline(filestream, str, '\n'); 得不到A字符窜得 而是个“空”

ghiewa at 2004-10-1 20:34:27 >
# 16 Re: 如何设置让fstream在读取顺序文件时候忽略空格,std::ws有用吗?

说答案了 给分

string str;

int a,b;

filestream>>a>>b;

filestream.ignore();

getline(filestream,str,'\n');

ghiewa at 2004-10-7 20:22:18 >

C/C++

All Classified