By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,847 Members | 2,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,847 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
************************************************** *************

Jul 7 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Jerry wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 7 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.