C++ getline(istreamVar, strVar) return null string

J

Jerry

I'm new to c++, trying a simple test to read data form a txt file.
I compiled with gcc version 3.4.4 20050721 (Red Hat 3.4.4-2).
It didn't work as expected, getline() return with null string and
failed to read the left data.

Is there anything missing?

Thanks

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main( )
{ string orbfile, outfile, stemp, rinfile, teqcPltFile, rinexObsFile;
double year1, mon1, day1, hr1, min1, sec1;
double year2, mon2, day2, hr2, min2, sec2;
double cutoffAngle;

ifstream inp("cf2sky.inp");

inp >> year1 >> mon1 >> day1 >> hr1 >> min1 >> sec1;
inp >> year2 >> mon2 >> day2 >> hr2 >> min2 >> sec2;
inp >> orbfile;

getline(inp,stemp);

inp >> cutoffAngle;
inp >> rinexObsFile;
inp >> teqcPltFile;
inp.close();

cerr << year1 << mon1 << day1 << hr1 << min1 << sec1 << endl;
cerr << year2 << mon2 << day2 << hr2 << min2 << sec2 << endl;
cerr << orbfile << endl;
cerr << stemp << endl;
cerr << cutoffAngle << endl;
cerr << rinexObsFile << endl;
cerr << teqcPltFile << endl;
return 0;
}

**********************Here are output********************
2005812500
2005812700
/home/jerry/GAMIT/preproc/cf2sky/auto2240.05n

4.18086e-305


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


********************Here are input file*********************
2005 8 12 5 0 0
2005 8 12 7 0 0
/home/jerry/GAMIT/preproc/cf2sky/auto2240.05n
P1 Pseudorange Multipath at DEQN
10
/home/jerry/GAMIT/preproc/cf2sky/deqn2240.05o
/home/jerry/GAMIT/preproc/cf2sky/deqn2240.mp1
***************************************************************
 
V

Victor Bazarov

Jerry said:
I'm new to c++, trying a simple test to read data form a txt file.
I compiled with gcc version 3.4.4 20050721 (Red Hat 3.4.4-2).
It didn't work as expected, getline() return with null string and
failed to read the left data.

Is there anything missing?

After reading your 'orbfile', you need to tell it to '.ignore' the
rest of the same string. Only then read the next string. What you
have is the \n char is still stuck in the stream after reading your
'orbfile' string, and that's why the empty string is read.
Thanks

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main( )
{ string orbfile, outfile, stemp, rinfile, teqcPltFile, rinexObsFile;
double year1, mon1, day1, hr1, min1, sec1;
double year2, mon2, day2, hr2, min2, sec2;
double cutoffAngle;

ifstream inp("cf2sky.inp");

inp >> year1 >> mon1 >> day1 >> hr1 >> min1 >> sec1;
inp >> year2 >> mon2 >> day2 >> hr2 >> min2 >> sec2;
inp >> orbfile;

getline(inp,stemp);

inp >> cutoffAngle;
inp >> rinexObsFile;
inp >> teqcPltFile;
inp.close();

cerr << year1 << mon1 << day1 << hr1 << min1 << sec1 << endl;
cerr << year2 << mon2 << day2 << hr2 << min2 << sec2 << endl;
cerr << orbfile << endl;
cerr << stemp << endl;
cerr << cutoffAngle << endl;
cerr << rinexObsFile << endl;
cerr << teqcPltFile << endl;
return 0;
}

**********************Here are output********************
2005812500
2005812700
/home/jerry/GAMIT/preproc/cf2sky/auto2240.05n

4.18086e-305


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


********************Here are input file*********************
2005 8 12 5 0 0
2005 8 12 7 0 0
/home/jerry/GAMIT/preproc/cf2sky/auto2240.05n
P1 Pseudorange Multipath at DEQN
10
/home/jerry/GAMIT/preproc/cf2sky/deqn2240.05o
/home/jerry/GAMIT/preproc/cf2sky/deqn2240.mp1
***************************************************************

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top