473,549 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ getline(istream Var, 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,ste mp);

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 5219
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,ste mp);

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2982
by: gogomei | last post by:
I have a text file like following and need to read out the names of each person. Since the only accurate info is there is a null line before starting a new name, I have written following code to take out the names, but it doesn't work. Looks like "strcmp(tmp,"\n")==0" is never reached. How can I do? Thank you!
4
3679
by: Joe | last post by:
Hello - I wrote a program that uses ifstream to open an ASCII file and getline() to read in the lines. The problem is when I try to open the same file again later in the code. I used close() to close the file but the next open() fails. If I comment out the getline() then I can open it a second time without a problem. Here are parts of...
5
7734
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program that shows how to use the 'getline' function. It said: "The getline function allows you to specify how many bytes you will get from the users input....
14
3864
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a ..txt file that I want to read into a multi-dimensional string array. Each line of the file needs to be read into the array. OK..sounds easy...
2
28692
by: manwanirg | last post by:
the function getline is a public member of istream and cin.getline can be used. Since ifstream is publicily derived from istream, getline shall be available in ifstream as well. However,on solaris ver 2.7, getline is not recognised. e.g ifstream f("a.cpp"); string s; f.getline(s,100); why doesn't complier recognise...
6
2371
by: bryant058 | last post by:
#include<iostream> #include<cstring> #include<string> #include<iomanip> using namespace std; int main() { string s; char *tokenptr; int space;
4
3951
by: Kelly B | last post by:
This is a simple string matching code from K&R2 (modified to match a user entered string from a text file) I tried to code an alternative strindex function(commented below) but it does not work ,i don't seem to understand where exactly am i going wrong Secondly using gets() can be a dangerous option if the user enters more characters than...
4
2567
by: nothix9 | last post by:
Hello guys, Ive been working this for 2 days, I am required to position a string using drawstring but the problem is its dynamic and changes depending on the database or source. A quick fix I thought was to position it at the end of the report but its required at the middle of other entries. Can you give me tips and different approach how to...
11
4240
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr. Yick. All I could come up with is: #include <string> int main() { std::string text;
0
7527
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7459
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7726
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7967
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7485
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5377
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1953
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.