How to get individual informations from a file? | Newbie | | Join Date: Aug 2006
Posts: 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. : )
| | Needs Regular Fix | | Join Date: Jun 2006
Posts: 294
| | | re: How to get individual informations from a file?
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.
| | Newbie | | Join Date: Aug 2006
Posts: 7
| | | re: How to get individual informations from a file? Quote:
Originally Posted by D_C 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.
| | Needs Regular Fix | | Join Date: Jun 2006
Posts: 294
| | | re: How to get individual informations from a file?
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);
-
}
| | Newbie | | Join Date: Aug 2006
Posts: 24
| | | re: How to get individual informations from a file?
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...
| | Member | | Join Date: Nov 2006
Posts: 94
| | | re: How to get individual informations from a file? Quote:
Originally Posted by D_C 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?
|  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 229,155 network members.
|