 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Chelong Guest
|
Posted: Mon May 14, 2007 9:11 am Post subject: a simple problem about using stl op text files |
|
|
hey,the follow is the text file content
========================================apple====pear==
one Lily 7 0 0 7 7
two Lily 20 20 6.6666 20 8
one Lily 0 10 2.85 4 0
two Lily 22 22 7.33326 2 5
two jenny 6 0 0 6 6
two jenny 12 0 0 12 12
three jenny 36 36 10.26 36 36
four jenny 30 30 6 30 30
four jenny 41 41 8.2 41 41
.........................................
.......................................and so on!
i want to get the result:
e.g:
Lily
one pears: 7+0
apples:7+4
two pears:8 + 5
apples:20 + 2
the jenny the same ,and some others
================================
how can i make it come true using stl?or another simple way?
waiting for ur help!
thx advance! |
|
| Back to top |
|
 |
Hari Guest
|
Posted: Mon May 14, 2007 9:11 am Post subject: Re: a simple problem about using stl op text files |
|
|
Chelong je napisao:
| Quote: | hey,the follow is the text file content
========================================apple====pear==
one Lily 7 0 0 7 7
two Lily 20 20 6.6666 20 8
one Lily 0 10 2.85 4 0
two Lily 22 22 7.33326 2 5
two jenny 6 0 0 6 6
two jenny 12 0 0 12 12
three jenny 36 36 10.26 36 36
four jenny 30 30 6 30 30
four jenny 41 41 8.2 41 41
........................................
......................................and so on!
i want to get the result:
e.g:
Lily
one pears: 7+0
apples:7+4
two pears:8 + 5
apples:20 + 2
the jenny the same ,and some others
================================
how can i make it come true using stl?or another simple way?
waiting for ur help!
thx advance!
|
1. use std::fstream for reading
2. use std::vector (std::list, or other container) to store result
3. display content using std::cout
Note: nobady will do your homework here. Please post specific
question.
Best,
zaharije Pasalic |
|
| Back to top |
|
 |
Zeppe Guest
|
Posted: Mon May 14, 2007 9:11 am Post subject: Re: a simple problem about using stl op text files |
|
|
Chelong wrote:
| Quote: | hey,the follow is the text file content
[SNIP]
waiting for ur help!
|
Hey!
If the problem is simple as you state in the topic, why don't you solve
it by yourself? The solution is here, anyway:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
Feel free to try by yourself and if you have some problem post it here.
Regards,
Zeppe |
|
| Back to top |
|
 |
Zeppe Guest
|
Posted: Tue May 15, 2007 9:10 am Post subject: Re: a simple problem about using stl op text files |
|
|
Chelong wrote:
| Quote: | On 5月14日, 下午4时30分, Zeppe
zeppe@.remove.all.this.long.comment.email.it> wrote:
Chelong wrote:
hey,the follow is the text file content
[SNIP]
waiting for ur help!
Hey!
If the problem is simple as you state in the topic, why don't you solve
it by yourself? The solution is here, anyway:http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
Feel free to try by yourself and if you have some problem post it here.
Regards,
Zeppe
=====================================================
thx for ur reminding!
actually,i had solved the problem by myself,and it is not a homework.
but i want a more simple method to modify it using stl.just so!
|
Sorry for insinuating, then Just custom.
As Hari suggested, you can read the file with an std::fstream. Actually,
everything depend on how many particular bad-format cases do you want to
handle, but if you want to retain it simple I suggest you to use an
exception-based approach.
Apart for the first line, that I would read with a separate approach
(basically removing the '=' symbols and retaining the string to identify
the categories), for the rest of the file (the data rows, I would use a
row-based approach.
Basically, I would declare a fstream and take the single lines with the
getline() method. Then, you can declare a std::istringstream (that is, a
stream based on a string instead that on a file) and then you tell it to
throw exceptions whenever an unexpected input is read. basically:
cin.exceptions( std::ios_base::badbit | std::ios_base::failbit );
Then, you can at least parse the single line without checking if every
input has been read correctly. Then you can put everything in a data
structure, but I haven't understood very well the format of the data, so
I can't give you more suggestions on this point.
Regrads,
Zeppe |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|