473,782 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4961
On 26 Oct 2006 23:13:08 -0700 in comp.lang.c++,
co***********@g mail.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***********@g mail.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::stringstre am 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_er rors 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::stringstre am

template < typename T >
T stream_cast(con st std::string& r_s)
{
std::stringstre am 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***********@g mail.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(getlin e(getline(getli ne(stream, 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
14894
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 allows me to do that? It should avoid us the work to write a COBOL program that open the COBOL
18
4893
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) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
4
4463
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 level, and provide some additional data validation, I'm trying to do it with code instead of an update query. Unfortunately, it's not letting me update the data: I'm getting an error saying the data is read only. The same query statement, when...
5
2929
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 shift 24 bits storing in result then printing the result. I thing a while or for loop is needed but I'm not quite sure how to go about it. How do I step through each character in this case and store it for use and passing to another function. ...
5
5120
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 Partition we are using ReadFile , WriteFile and setFilePointerEx to read and write the sectors and we are reading/writing 102400 sectors together, even we have reduced the sectors still it is not able to read or write, our program is working fine...
7
1549
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 is no header file to include, but I can't seem to figure out how to output the data to the file I create when the main is in a different. Will someone give me an example or show me what I am doing wrong? #include <iostream> #include...
3
6249
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 that multiple worksheet data in different table.How can i do it.Below is my xml file. <?xml version="1.0"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" ...
3
5402
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 that multiple worksheet data in different table.How can i do it.Below is my xml file. <?xml version="1.0"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"...
0
9639
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10308
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10076
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8964
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5375
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4040
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
3
2870
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.