473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Read file of multiple data types.

I have a file where I know the lines go as follows:

string
long
string
int
int
string
double

it then repeats this sequence multiple times. I can use getline() to
get the data, but I need to be able to use the ints, doubles, and longs
without them being strings or char[]. The strings also have an unknown
amount of spaces in each one, so i can't use >to get the data. At
least not that I can get to work. Is there a way to get the different
data into variables of the correct types? If there is a way to convert
them after I use getline() that would work too.

Oct 27 '06 #1
3 4922
On 26 Oct 2006 23:13:08 -0700 in comp.lang.c++,
co***********@gmail.com wrote,
>I can use getline() to
get the data, but I need to be able to use the ints, doubles, and longs
strtod()
strtol()

Oct 27 '06 #2
co***********@gmail.com wrote:
I have a file where I know the lines go as follows:

string
long
string
int
int
string
double

it then repeats this sequence multiple times. I can use getline() to
get the data, but I need to be able to use the ints, doubles, and longs
without them being strings or char[]. The strings also have an unknown
amount of spaces in each one, so i can't use >to get the data. At
least not that I can get to work. Is there a way to get the different
data into variables of the correct types? If there is a way to convert
them after I use getline() that would work too.
Write a simple struct with a corresponding member for each of the above
variable types.
I'ld suggest std::string instead of char arrays and leave the numeric
types as they are.
Use a default and a parametized ctor. An overloaded op<< is nice to
have.
Prove that you create a Record in main(). Don't use new.

Then declare a std::vector< Record where Record represents your new
class.
A good option is to wrap the std::vector in a Container class along
with the filename.
Prove that you can create the Container and
container.push_back(therecord);
An overloaded op<< is nice here too. Display the stored records.
Then code a member function void Container::read() to parse the Records
from your data file (std::ifstream).

A std::stringstream can help you parse a std::string buffer when
std::getlining the numerics.
You need an error checking mechanism, i'ld suggest throwing
std::runtime_errors when checking state of std::ifstream in read(). Use
try-catch block in main() to capture these.

You can use a templated function like so to convert buffer to numbers:

#include <string>
#include <sstream// for std::stringstream

template < typename T >
T stream_cast(const std::string& r_s)
{
std::stringstream ss;
ss << r_s;
T result;
ss >result;
return result;
}

int main()
{
std::string s("1.1");
double d = stream_cast< double >(s.data());
std::cout << "d = " << d << std::endl;
}

Sounds daunting but its not that bad.
Got my container working in like 20 minutes.
If any problems, show your code.
The guys in here are a pain in the ass, but they know their stuff, lol.

Oct 27 '06 #3

co***********@gmail.com wrote:
I have a file where I know the lines go as follows:

string
long
string
int
int
string
double

it then repeats this sequence multiple times. I can use getline() to
get the data, but I need to be able to use the ints, doubles, and longs
without them being strings or char[]. The strings also have an unknown
amount of spaces in each one, so i can't use >to get the data. At
least not that I can get to work. Is there a way to get the different
data into variables of the correct types? If there is a way to convert
them after I use getline() that would work too.
You could use getline for the strings, and operator >for everything
else. The only gotcha there is that operator >won't extract the
newline after the long, ints, or double. So when the next thing you
need to read is a string, the getline will only extract the newline,
rather than the string you were aiming for. This is fixed by putting
in a dummy getline just to grab the newline.

getline(stream, s1);
stream >l1; getline(stream, eol); // get rid of the newline because
we are reading a string next
getline(stream, s2);
stream >i1; // no need for getline here because we aren't reading a
string next.
stream >i2; getline(stream, eol); // get rid of the newline because
we are reading a string next

If you wanted to be very creative, you could exploit the fact that
getline returns a stream to read an entire record in one line:
getline(getline(getline(getline(getline(getline(st ream, s1) >l1,
eol), s2) >i1 >i2, eol), s3) >d1, eol) ;

(Note: You could, but you probably shouldn't).

--
Alan Johnson

Oct 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Batista, Facundo | last post by:
People: I'm trying to convert my father from using COBOL to Python, :) One difficult thing we stuck into is how to read, from python, files written with COBOL. Do you know a module that...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
4
by: google | last post by:
I am trying to implement form that allows a user to update data in multiple records in a table based on criteria they enter in the form. Because I want to allow some user interaction on a record...
5
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right...
5
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or...
7
by: B. Williams | last post by:
I wrote a program that would simply output data to the screen, but now I am trying to have the data saved to a file. I don't have a problem creating the file or even outputting data to it when there...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...

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.