如何读取文件? [英] how to read a file?

查看:62
本文介绍了如何读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,每行有四个字段:

string,int,int,float。

例如:


年龄3 5 3.5

avr 6 7 3.6

....

....

我怎么能读到这样的文件:

每行
做{

str =获取字符串(或单词)

int1 =获取第一个int

int2 =获取第二个int

f = get float

... ..

详细说明str,int1,int2和f

.....

}


先谢谢你,马里奥。

解决方案



" _mario.lat" <毫安******* @ libero.it>在消息中写道

新闻:Vd ********************* @ twister1.libero.it ...

我有一个文件,每行有四个字段:
string,int,int,float。
例如:

年龄3 5 3.5
avr 6 7 3.6
...


我怎么能读到这样的文件:

每一行都是{
str =获取字符串(或单词)
int1 =获取第一个int
int2 =获取第二个int
f = get float
....



在您尝试过问题的地方发布一些代码。然后人们会更高兴

来帮助你。说真的,如果你搜索 http://groups.google.com ,你就是

会发现问题已被多次回答。


祝您好运。


-Sharad




" _mario.lat" <毫安******* @ libero.it>在消息中写道

新闻:Vd ********************* @ twister1.libero.it ...

我有一个文件,每行有四个字段:
string,int,int,float。
例如:

年龄3 5 3.5
avr 6 7 3.6
...


我怎么能读到这样的文件:

每一行都是{
str =获取字符串(或单词)
int1 =获取第一个int
int2 =获取第二个int
f = get float
.... 详细说明str,int1,int2和f
....


先谢谢你,马里奥。




喜欢这个


ifstream文件(" some_file");

string str;

int int1,int2;

浮动f;

while(文件>> str>> int1>> int2>> f)

{

...

}


但是上面的代码中没有太多错误检查。如果你想检查

输入那么事情会变得更复杂。


john


_mario .lat写道:

我有一个文件,每行有四个字段:
string,int,int,float。
例如:

年龄3 5 3.5
avr 6 7 3.6
...


我怎么能读到这样的文件:<每行做一次{
str =得到字符串(或单词)
int1 =得到第一个int
int2 =得到第二个int
f =得到漂浮
....
详细说明str,int1,int2和f
....


提前谢谢你,马里奥。




std :: string line;

while(std :: getline(thefile,line))

{

std :: string str;

int int1,int2;

float f;

std: :istringstream s(line);


if(!(s>> str>> int1>> int2>> f))

{

//破李ne $ / $
}

}


if(!thefile.eof())

{< br $>
//错误

}


I have a file whith four field in each line:
string, int, int, float.
For example:

age 3 5 3.5
avr 6 7 3.6
....
....

how can I read this file doing something like that:

for each line do{
str=get the string (or word)
int1=get the first int
int2=get the second int
f=get float
.....
elaborate str,int1,int2 and f
.....
}

Thank you in advance, Mario.

解决方案


"_mario.lat" <ma*******@libero.it> wrote in message
news:Vd*********************@twister1.libero.it...

I have a file whith four field in each line:
string, int, int, float.
For example:

age 3 5 3.5
avr 6 7 3.6
...
...

how can I read this file doing something like that:

for each line do{
str=get the string (or word)
int1=get the first int
int2=get the second int
f=get float
....



Post some code where you have tried the problem. Then people will be happier
to help you here. Seriously, if you search http://groups.google.com , you
will find the problem been answered several times.

Best of luck.

-Sharad



"_mario.lat" <ma*******@libero.it> wrote in message
news:Vd*********************@twister1.libero.it...

I have a file whith four field in each line:
string, int, int, float.
For example:

age 3 5 3.5
avr 6 7 3.6
...
...

how can I read this file doing something like that:

for each line do{
str=get the string (or word)
int1=get the first int
int2=get the second int
f=get float
....
elaborate str,int1,int2 and f
....
}

Thank you in advance, Mario.



Like this

ifstream file("some_file");
string str;
int int1, int2;
float f;
while (file >> str >> int1 >> int2 >> f)
{
...
}

But there is not much error checking in the above code. If you want to check
the input then things get more complicated.

john


_mario.lat wrote:

I have a file whith four field in each line:
string, int, int, float.
For example:

age 3 5 3.5
avr 6 7 3.6
...
...

how can I read this file doing something like that:

for each line do{
str=get the string (or word)
int1=get the first int
int2=get the second int
f=get float
....
elaborate str,int1,int2 and f
....
}

Thank you in advance, Mario.



std::string line;
while (std::getline(thefile, line))
{
std::string str;
int int1, int2;
float f;
std::istringstream s(line);

if (! (s >> str >> int1 >> int2 >> f))
{
//broken line
}
}

if (!thefile.eof())
{
//error
}


这篇关于如何读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆