473,397 Members | 2,084 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,397 software developers and data experts.

How to get individual informations from a file?

7
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. : )
Sep 4 '06 #1
6 3810
D_C
293 100+
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.
Sep 4 '06 #2
GAOZH
7
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.
Sep 5 '06 #3
D_C
293 100+
For example, the following code parses lines of the form:
Expand|Select|Wrap|Line Numbers
  1. 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().
Expand|Select|Wrap|Line Numbers
  1.   string str;
  2.   ifstream file("toolsongs.txt");
  3.  
  4.   if (file.is_open())
  5.   {
  6.  
  7.     Song *temp;
  8.     string lname;
  9.     int ID;
  10.     string fname;
  11.     int age;
  12.  
  13.     int pos;
  14.  
  15.     do
  16.     {
  17.       getline(file,str);
  18.  
  19.       pos = str.find('\t',0);
  20.       lname = str.substr(0,pos);
  21.       str = str.substr(pos+1,str.length());
  22.  
  23.       pos = str.rfind('\t',str.length());
  24.       age = atoi(str.substr(pos+1,str.length()).c_str());
  25.       str = str.substr(0,pos);
  26.  
  27.       pos = str.rfind('\t',str.length());
  28.       fname = str.substr(pos+1,str.length());
  29.       str = str.substr(0,pos);
  30.  
  31.       pos = str.rfind('\t',str.length());
  32.       ID = atoi(str.substr(pos+1,str.length()).c_str());
  33.  
  34.       temp = new Person(fname,ID,lname,age);
  35. }
Sep 5 '06 #4
Nhd
24
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...
Sep 5 '06 #5
dav3
94
For example, the following code parses lines of the form:
Expand|Select|Wrap|Line Numbers
  1. 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().
Expand|Select|Wrap|Line Numbers
  1.   string str;
  2.   ifstream file("toolsongs.txt");
  3.  
  4.   if (file.is_open())
  5.   {
  6.  
  7.     Song *temp;
  8.     string lname;
  9.     int ID;
  10.     string fname;
  11.     int age;
  12.  
  13.     int pos;
  14.  
  15.     do
  16.     {
  17.       getline(file,str);
  18.  
  19.       pos = str.find('\t',0);
  20.       lname = str.substr(0,pos);
  21.       str = str.substr(pos+1,str.length());
  22.  
  23.       pos = str.rfind('\t',str.length());
  24.       age = atoi(str.substr(pos+1,str.length()).c_str());
  25.       str = str.substr(0,pos);
  26.  
  27.       pos = str.rfind('\t',str.length());
  28.       fname = str.substr(pos+1,str.length());
  29.       str = str.substr(0,pos);
  30.  
  31.       pos = str.rfind('\t',str.length());
  32.       ID = atoi(str.substr(pos+1,str.length()).c_str());
  33.  
  34.       temp = new Person(fname,ID,lname,age);
  35. }
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?
Jan 28 '07 #6
horace1
1,510 Expert 1GB
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. : )
you could use a string tokenizer, e.g.
http://www.cplusplus.com/reference/c...ng/strtok.html
http://www.partow.net/programming/st...zer/index.html
http://www.codeproject.com/cpp/stringtok.asp

to extract tokens from your input string and then convert the appropriate strings into numeric data
Jan 28 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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, ...
2
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...
1
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...
4
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...
0
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...
1
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...
4
by: Stefan | last post by:
Hi, is there a way to get informations from tabelspaces of a db2 (datafiles) via java (jdbc)? Thanx... Stefan
7
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
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
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,...
0
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,...
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...
0
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...

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.