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
************************************************** ************* 1 5207
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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()...
|
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...
|
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...
|
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...
|
by: bryant058 |
last post by:
#include<iostream>
#include<cstring>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
string s;
char *tokenptr;
int space;
|
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...
|
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...
|
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....
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| | |