I am trying to read multiple files which contain values under various Column headers (like pressure, temperature etc)...I using "infile" to read values and assign it to a structure. However whenver there is a bad character in the file or less number than the number of parameters, the output gives error..(like it gives the output as only 0 once the bad character is encountered...)...Can someone help me regarding avoiding these bad characters....I have tried infile.getline also, but i do not know how to read individual characters in each line....

Kindly have patience, i am a beginner.Thank you in advance....

Could u plz show us the format of the file? also when u are reading the file u declare the variable as char or some thing else?

The format is like this......I manage to skip the first line and then assign a structure for eg, a[i].time, a[i].z, a[i].p etc.

time    z   p   t   rh  dpt dir spd
1   17  998.4   27.7    77  23.4    0   0
2   23  997.8   26.3    64  19  -999    -999
3   23  997.8   26.3    64  19  -999    -999
4   23  997.8   26.3    64  19  -999    -999

But say if the format is

5   997.8   26.3    64  -999    -999

ie. 2 of the parameters are missing..
Then the whole output gives 0 from then on...the structure is like below

struct par
{
    int     time;
    double z,p,t,rh,dpt,dir,spd;

};

Thank you very much...

Edited 3 Years Ago by Nick Evan: Fixed formatting

What I would do is use a low-tech method: read the line into a string using getline() and step through it to count the spaces. If you don't have all the numbers you need, record the line number of the bad data, throw out the string and read another line.

Otherwise knowing whether something is of the proper nature for that element of the struct would be kind of tricky.

If the missing values are always the last in the line, then as jonsca suggests read teach line using fgets() then use sscanf() to read the line into the variables.

But you also mention "a bad character". Do you mean there are letters in the input that will mess things up? If so, your task is much harder.

Everybody will probably give me guff for saying this but if this isn't your assignment and you are trying to process a lot of real data try "preconditioning" it in Excel or the like.
It looks like maybe that's what someone had done with the -999 but I'm not sure of that. If that's the case already then you'll just have to deal with that type of situation in post-processing by sorting the outliers together and either treating them differently or ignoring them.

Yes, you are right, the file has been preprocessed by replacing bad characters with -999. But the thing is there are many files and i can't be doing that for everything....Actually this problem i solved it (also after going through various other topics in DaniWeb) by reading it as a string as adviced by you.

But i got a new problem with the original file, where the error characters are like "/////".....so wat i am trying to do is i am using the following code (part of the code)...

infile>>d_time>>d_z>>d_p>>d_t>>d_rh>>d_dpt>>d_dir>>d_spd;

where d_time and others are strings defined as char d_time[10]...etc

And then using atoi and atof to use it to assign the value to the actual variable like previously done for example

if(conditional statement)
a[i].time=atoi(d_time);

is my approach right? if so i do have another query regarding the conditional statement...Kindly have patience with me

Yes, I think since you do not know where the //// will be then you have limited choices. I think if you tried to designate what those variables should be and one of the numerical ones reads //// then the stream will go bad and the rest of the reads will fail.

what i would advice is you will need to get ur line in a string variable and tan concatenate it into parts while counting the number of spaces. from the number of spaces you would know how many column a line or row has. if it matches with 7 (num of colums your data has - 1) than you would consider the row or line else throw it and go to next line. if you are condering the line than concatenate the string at every space and test first should be an interger thats if its always gona be whole numbers and the rest u gona test for floating points if u get true than your data is valid adawise its not and u can skip that line once again. continue until u reach .eof()

Edited 6 Years Ago by kesh1000: n/a

This question has already been answered. Start a new discussion instead.