输入格式化文件 [英] input formatted file

查看:79
本文介绍了输入格式化文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个数据文件


========

4 1 1 1 1

5 1 1 1 1 1

2 1 1

========


第一列表示此行中的值,然后是

后跟值。我看过一些使用的代码


****************************** *

ifstream inf


while(inf> data1> data2)

********* **********************

读取数据,只是非常漂亮和简单的代码,任何人都可以给予
$ b我给我的一些建议吗?提前谢谢。

Jun

解决方案




不测试,但我的猜测将是这样的:


#include< vector>

#include< iostream>

使用命名空间std;


// ........

试试

{

unsigned long Cnt = 0;

while(inf> Cnt)

{

vector< unsigned longData;

for(unsigned long CntData = 0; CntData< Cnt; ++ CntData)

{

unsigned long Item = 0;

if(!(inf> Item))

{

throw" Unexpeceted end of file" ;;

}

Data.push_back(Item);

}

//用数据做点什么

}

}

catch(const char *错误)

{

cerr<<错误<<结束;

}

问候,Ron AF Greve

http://www.InformationSuperHighway.eu

" Jun" < ju ***** @ gmail.comwrote in message

news:81 ************************* ********* @ y5g2000h sf.googlegroups.com ...


hello,


I '有一个数据文件


========

4 1 1 1 1

5 1 1 1 1 1

2 1 1

========


第一列表示中有多少个值这一行,然后

后跟值。我看过一些使用的代码


****************************** *

ifstream inf


while(inf> data1> data2)

********* **********************

读取数据,只是非常漂亮和简单的代码,任何人都可以给予
$ b我给我的一些建议吗?提前谢谢。


Jun



" Jun" < ju ***** @ gmail.comwrote in message

news:81 ************************* ********* @ y5g2000h sf.googlegroups.com ...


hello,


I '有一个数据文件


========

4 1 1 1 1

5 1 1 1 1 1

2 1 1

========


第一列表示中有多少个值这一行,然后

后跟值。我看过一些使用的代码


****************************** *

ifstream inf


while(inf> data1> data2)

********* **********************

读取数据,只是非常漂亮和简单的代码,任何人都可以给予
$ b我给我的一些建议吗?先感谢您。



我要做的一件事是使用std :: getline(inf,somestring);

然后将它放入字符串流中。

std :: stringstream数据(somestring);

此时你可以检查值的数量是否正确。


int算一下;

if(Data> Count)

{

int Value;

int ValueCount;

while(数据>值)

{

ValueCount ++;

//带有价值的东西

}

if(ValueCount!= Count)

{

//在这里抛出错误。开头的数字不匹配

//数值。

}

}


如果你不使用字符串流而只是做

而(inf>数字)

你不能确定你是在读数据还是伯爵。如果你被认为

要读5个值,比如行

5 1 1 1 1

2 1 1


它将下一行作为一个值读取,将下一个1作为一个计数,然后

读取下一个作为一个值,如果你理解的话。 />

但是getline只会读取一行,如果你需要
,你可以验证计数。


< blockquote> 11月24日下午5:37,Jim Langston < tazmas ... @ rocketmail.comwrote:


" Jun" < junh ... @ gmail.com写信息


新闻:81 *********************** *********** @ y5g2000h sf.googlegroups.com ...


hello,


我有一个数据文件


========

4 1 1 1 1

5 1 1 1 1 1

2 1 1

========


第一列表示此行中的值,然后是

后跟值。我看过一些代码使用


********************** *********

ifstream inf


while(inf> data1> data2)

*******************************

来读取数据,只是非常漂亮和简单的代码,任何人都可以给我一些建议吗?先感谢您。



我要做的一件事是使用std :: getline(inf,somestring);

然后将它放入字符串流中。 />
std :: stringstream数据(somestring);

此时你可以检查值的数量是否正确。


int算一下;

if(Data> Count)

{

int Value;

int ValueCount;

while(数据>值)

{

ValueCount ++;

//带有价值的东西

}

if(ValueCount!= Count)

{

//在这里抛出错误。开头的数字不匹配

//数值。

}


}


如果你不使用字符串流而只是做

而(inf>数字)

你不能确定你是否''重新读取数据或计数。如果你被认为

要读5个值,比如行

5 1 1 1 1

2 1 1


它将下一行作为一个值读取,将下一个1作为一个计数,然后

读取下一个作为一个值,如果你理解的话。 />

但是getline只会读取一行,如果你需要
,你可以验证计数。



以下代码

================

std :: stringstream数据(somestring);

================


只是没有''工作


hello,

I''ve a data file

========
4 1 1 1 1
5 1 1 1 1 1
2 1 1
========

first column represents the how many values in this line, and then
followed by values. I''ve seen some codes using

*******************************
ifstream inf

while(inf >data1 >data2)
*******************************
to read data, just very beautiful and simple code, could anyone give
me some advices for my case? thank you in advance.
Jun

解决方案

Hi,

Not tested, but my guess would be something like:

#include <vector>
#include <iostream>
using namespace std;

//........
try
{
unsigned long Cnt = 0;
while( inf >Cnt )
{
vector<unsigned longData;
for( unsigned long CntData = 0; CntData < Cnt; ++CntData )
{
unsigned long Item = 0;
if( !( inf >Item ) )
{
throw "Unexpeceted end of file";
}
Data.push_back( Item );
}
// Do something with data
}
}
catch( const char *Error )
{
cerr << Error << endl;
}
Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"Jun" <ju*****@gmail.comwrote in message
news:81**********************************@y5g2000h sf.googlegroups.com...

hello,

I''ve a data file

========
4 1 1 1 1
5 1 1 1 1 1
2 1 1
========

first column represents the how many values in this line, and then
followed by values. I''ve seen some codes using

*******************************
ifstream inf

while(inf >data1 >data2)
*******************************
to read data, just very beautiful and simple code, could anyone give
me some advices for my case? thank you in advance.
Jun



"Jun" <ju*****@gmail.comwrote in message
news:81**********************************@y5g2000h sf.googlegroups.com...

hello,

I''ve a data file

========
4 1 1 1 1
5 1 1 1 1 1
2 1 1
========

first column represents the how many values in this line, and then
followed by values. I''ve seen some codes using

*******************************
ifstream inf

while(inf >data1 >data2)
*******************************
to read data, just very beautiful and simple code, could anyone give
me some advices for my case? thank you in advance.

One thing I would do is use std::getline( inf, somestring );
then put it into a stringstream.
std::stringstream Data( somestring );
At this point you can check if the number of values are correct.

int Count;
if ( Data >Count )
{
int Value;
int ValueCount;
while ( Data >Value )
{
ValueCount++;
// dosomething with Value
}
if ( ValueCount != Count )
{
// throw error here. Number at beginning does not match
// number of values.
}
}

If you don''t use a stringstream but just do
while ( inf >Number )
you can''t be sure if you''re reading data or the count. If you were supposed
to be reading 5 values, such as the lines
5 1 1 1 1
2 1 1

It would read the next lines 2 as a value, grab the next 1 as a count, then
read the next 1 as a value, if you understand.

but getline will only read one line, which you can validate the count if you
need to.


On Nov 24, 5:37 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:

"Jun" <junh...@gmail.comwrote in message

news:81**********************************@y5g2000h sf.googlegroups.com...

hello,

I''ve a data file

========
4 1 1 1 1
5 1 1 1 1 1
2 1 1
========

first column represents the how many values in this line, and then
followed by values. I''ve seen some codes using

*******************************
ifstream inf

while(inf >data1 >data2)
*******************************
to read data, just very beautiful and simple code, could anyone give
me some advices for my case? thank you in advance.


One thing I would do is use std::getline( inf, somestring );
then put it into a stringstream.
std::stringstream Data( somestring );
At this point you can check if the number of values are correct.

int Count;
if ( Data >Count )
{
int Value;
int ValueCount;
while ( Data >Value )
{
ValueCount++;
// dosomething with Value
}
if ( ValueCount != Count )
{
// throw error here. Number at beginning does not match
// number of values.
}

}

If you don''t use a stringstream but just do
while ( inf >Number )
you can''t be sure if you''re reading data or the count. If you were supposed
to be reading 5 values, such as the lines
5 1 1 1 1
2 1 1

It would read the next lines 2 as a value, grab the next 1 as a count, then
read the next 1 as a value, if you understand.

but getline will only read one line, which you can validate the count if you
need to.

the following code
================
std::stringstream Data( somestring );
================

just doesn''t work


这篇关于输入格式化文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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