Reading data errors

Dec 12, 2010 at 8:59pm
closed account (ohRGz8AR)
Hi guys, I have used this site for over a year now, it has helped me so much!

but i cant seem to find any information on this topic:

I am reading data from a text file and storing it as interger variables, I want to make my program "more defensive" and check to make sure all required data is present before computing anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream stream1;
stream1.open("K:\\test.txt");\\Desktop\\stocks.txt

if(!stream1)
{
 cout<<"ERROR: File Not Found"<<endl;

}

while (stream1.good() )
{

//read file
		 stream1 >> a >> b >> c >> d >> e;


I problem I am having is making the program identify missing data for example if variable a is missing, the program just takes b, knocking everything out of sync.

All i have come up with so far is individual constraints for each variable, but these also fail when a "fitting" variable replaces the missing one.

Thanks in advance :)

Dec 12, 2010 at 9:19pm
how does a missing variable look?
is there an empty line or a longer gap or what?
Dec 12, 2010 at 9:22pm
closed account (ohRGz8AR)
1
2
3
4
5
7.75	08 09 2006	113.164254
8	10 06 2003	109.384699
8	25 09 2009	120.489669
8	27 09 2013	128.815470
	07 12 2015	136.130273


The last line demonstrates missing data (the above is the format of the data) but any element could be missing.

I also would like to skip over any completly blank lines.

Thanks
Last edited on Dec 12, 2010 at 9:25pm
Dec 12, 2010 at 9:36pm
It looks like you could use the whitespace as hinting. All the data is numeric, so short of custom-making a parser to parse the "right" kind of number data that would be the only approach.

Require tabs in-between column 1-2 and 4-5. You could do it something like this:

1
2
3
4
5
6
7
getfirstcolumn();
if (getcharacter() != '\t')
    //problem???
getdatecolumn();
if (getcharacter() != '\t')
    //u mad parser???
getthirdcolumn();
Dec 12, 2010 at 9:44pm
Use sscanf for formatted input.
Dec 12, 2010 at 10:23pm
closed account (ohRGz8AR)
1
2
3
4
5
6
7
getfirstcolumn();
if (getcharacter() != '\t')
    //problem???
getdatecolumn();
if (getcharacter() != '\t')
    //u mad parser???
getthirdcolumn();


So if i am understanding this correctly the first statment checks for a element that isnt a tab?

So I could run checks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (1=1)
{
stream1 >> a >> b >> c >> d >> e;
if ( a == '\t')
{
cout << "ERROR MISSING a!" << endl;
}
else if (b == '\t')
{
cout << "ERROR MISSING b!" << endl;
else
{
     //run rest of porgam here
}


I still dont think this will accomplish what I need?

I was just thinking could I not do this:

stream1 >> a >> '\t" >> b >> ' ' >> c >> ' ' >> d >> >> '\t" << e;

Or am i completly going wrong here?
Last edited on Dec 12, 2010 at 10:28pm
Topic archived. No new replies allowed.