Let's say there is a file called "info.txt", it looks like:
File1-0(71%) File2-0(63%) 177
File1-1(59%) File2-1(30%) 100
How to write C++ code to get the int number only? Example to get the int 71, int 63, and int 177 from the first line.
Thanks. : )
6 3810
There needs to be some consistency, otherwise it's not possible. For your case, there will always be three numbers. The first two numbers are between the characters '(' and '%'. I would search for each of those, then take the substring that's in between both of them. Then parse those strings to integers. For the last one, use a reverse find function to find the last space in the string, then everything after that will be the last integer. Parse that substring as an integer too.
There needs to be some consistency, otherwise it's not possible. For your case, there will always be three numbers. The first two numbers are between the characters '(' and '%'. I would search for each of those, then take the substring that's in between both of them. Then parse those strings to integers. For the last one, use a reverse find function to find the last space in the string, then everything after that will be the last integer. Parse that substring as an integer too.
Can explain in C++ code? I don't really understand how to convert what you said into coding.
Thanks.
For example, the following code parses lines of the form: - LastName 12343210 FirstName 9
Since there may be more than one tab in between LastName and ID, everything is pulled from the end after removing LastName from the string. There will always be exactly one tab in between those fields. That's why rfind(), reverse find, is used instead of find(). - string str;
-
ifstream file("toolsongs.txt");
-
-
if (file.is_open())
-
{
-
-
Song *temp;
-
string lname;
-
int ID;
-
string fname;
-
int age;
-
-
int pos;
-
-
do
-
{
-
getline(file,str);
-
-
pos = str.find('\t',0);
-
lname = str.substr(0,pos);
-
str = str.substr(pos+1,str.length());
-
-
pos = str.rfind('\t',str.length());
-
age = atoi(str.substr(pos+1,str.length()).c_str());
-
str = str.substr(0,pos);
-
-
pos = str.rfind('\t',str.length());
-
fname = str.substr(pos+1,str.length());
-
str = str.substr(0,pos);
-
-
pos = str.rfind('\t',str.length());
-
ID = atoi(str.substr(pos+1,str.length()).c_str());
-
-
temp = new Person(fname,ID,lname,age);
-
}
Hi can i add on a question...
What if the question requires us to read in from cin all lines of the report and write to cout the required statictic instead of reading from a file...
How do we modify the above codes so that instead of reading from a file, it reads in from cin...
For example, the following code parses lines of the form: - LastName 12343210 FirstName 9
Since there may be more than one tab in between LastName and ID, everything is pulled from the end after removing LastName from the string. There will always be exactly one tab in between those fields. That's why rfind(), reverse find, is used instead of find(). - string str;
-
ifstream file("toolsongs.txt");
-
-
if (file.is_open())
-
{
-
-
Song *temp;
-
string lname;
-
int ID;
-
string fname;
-
int age;
-
-
int pos;
-
-
do
-
{
-
getline(file,str);
-
-
pos = str.find('\t',0);
-
lname = str.substr(0,pos);
-
str = str.substr(pos+1,str.length());
-
-
pos = str.rfind('\t',str.length());
-
age = atoi(str.substr(pos+1,str.length()).c_str());
-
str = str.substr(0,pos);
-
-
pos = str.rfind('\t',str.length());
-
fname = str.substr(pos+1,str.length());
-
str = str.substr(0,pos);
-
-
pos = str.rfind('\t',str.length());
-
ID = atoi(str.substr(pos+1,str.length()).c_str());
-
-
temp = new Person(fname,ID,lname,age);
-
}
A current project I am working on is very similar to this. I have a question though, you have fname, lname declared as strings. Is it possible to declare them as char[some value]? And if so how does the extraction of the information change?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Annonce83 |
last post by:
Good evening
I will like to create a software on the grayline, I looking for information
for this subject (calculation, code etc...)
Thank your very much for your help.
My best regards,
...
|
by: Lionel |
last post by:
Hi all,
I would like having more informations on how we could exchange
informations and/or objects between PERL and JAVA.
We have a Java programs that open, maintain and close telnet...
|
by: Gabriele *Darkbard* Farina |
last post by:
Hi,
there is a Python library that makes me able to extract outline
informations from font files? I'd like to manage TrueType and FreeType
fonts ...
I searched for som wrappers, but I didn't...
|
by: marcelf3 |
last post by:
Hello,
I am a novice/moderate html programmer and I wonder how to tell the
browser from which start point and to which end point in the text will
each page be printed.
In other words, I have a...
|
by: marcelf3 |
last post by:
Hello,
I am a novice/moderate html programmer and I wonder how to tell the
browser from which start point and to which end point in the text will
each page be printed.
In other words, I have a...
|
by: Krish |
last post by:
I have requirement, that i get one big chunk of text file. This text file
will have has information, that on finding "****End of Information****", i
have to split them individual text file with our...
|
by: Stefan |
last post by:
Hi,
is there a way to get informations from tabelspaces of a db2
(datafiles) via java (jdbc)?
Thanx...
Stefan
|
by: Rolf Welskes |
last post by:
Hello,
Master-Content in asp.net is very powerful.
But:
I have the following:
One Master-Form.
30 Content-Forms which are the webpages used.
Each of this content-forms must have own...
|
by: durumdara |
last post by:
Hi !
WXP, Py2.4.3.
I want to get localized informations like month names, format
parameters, etc.
But nl_langinfo is not exists.
Have the Python a way to get these informations in...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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...
|
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: 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,...
|
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: 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...
| |