471,058 Members | 1,114 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,058 software developers and data experts.

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

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
1 4935
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.

Similar topics

4 posts views Thread by Joe | last post: by
5 posts views Thread by vknid | last post: by
14 posts views Thread by KL | last post: by
2 posts views Thread by manwanirg | last post: by
6 posts views Thread by bryant058 | last post: by
4 posts views Thread by Kelly B | last post: by
11 posts views Thread by Christopher Pisz | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.