[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

istream of a dirty file

thomas

10/3/2008 7:43:00 AM

----------code----------
istream is(filename);

int i1,i2; string s3;
while(is>>i1>>i2>>s3){
...
}
--------code--------

The file "filename" should be formated as "int int string" for each
line,
but the last line may have only "int int" or even "int".
So the program encounter error when reading the last line.

It's supposed that I can remove the last line if it's mal-formated.
But the file is huge, opening it is time consuming.

Is there a convenient way to tell "istream" that " this line is not
formated as expected, you can stop here"?
1 Answer

LR

10/3/2008 2:02:00 PM

0

thomas wrote:

> The file "filename" should be formated as "int int string" for each
> line,
> but the last line may have only "int int" or even "int".
> So the program encounter error when reading the last line.
>
> It's supposed that I can remove the last line if it's mal-formated.
> But the file is huge, opening it is time consuming.
>
> Is there a convenient way to tell "istream" that " this line is not
> formated as expected, you can stop here"?

I haven't tried this snippet, but something like this might work

std::ifstream input("yourfile");
std::string line;
while(std::getline(input,line)) {
std::istringstream inline(line);
int i1, i2;
std::string s3;
if(!(inline >> i1 >> i2 >> s3)) {
// bad line
break;
}
// continue processing line
}

LR