469,919 Members | 2,135 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,919 developers. It's quick & easy.

a simple problem about using stl op text files

hey,the follow is the text file content
========================================apple====p ear==
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!

May 14 '07 #1
5 1683
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-lit...t.html#faq-5.2

Feel free to try by yourself and if you have some problem post it here.

Regards,

Zeppe
May 14 '07 #2

Chelong je napisao:
hey,the follow is the text file content
========================================apple====p ear==
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

May 14 '07 #3
On 5月14日, 下午4时30分, Zeppe
<zeppe@.remove.all.this.long.comment.email.itwrote :
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-lit...t.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!

May 15 '07 #4
Chelong wrote:
On 5月14日, 下午4时30分, Zeppe
<zeppe@.remove.all.this.long.comment.email.itwrote :
>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-lit...t.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
May 15 '07 #5
On May 15, 4:56*pm, Zeppe
<zeppe@.remove.all.this.long.comment.email.itwrote :
Chelong wrote:
On 5鏈14鏃, 涓嬪崍4鏃30鍒, Zeppe
<zeppe@.remove.all.this.long.comment.email.itwrote :
Chelong wrote:
hey,the follow is thetextfilecontent
[SNIP]
waiting for ur help!
Hey!
If the problem issimpleas you state in the topic, why don't you solve
it by yourself? The solution is here, anyway:http://www.parashift.com/c++-faq-lit...t.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 moresimplemethod to modify it using stl.just so!

Sorry for insinuating, then :) Just custom.

As Hari suggested, you can read thefilewith an std::fstream. Actually,
everything depend on how many particular bad-format cases do you want to
handle, but if you want to retain itsimpleI 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 thefile(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 afile) 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- Hide quotedtext-

- Show quotedtext-
================================================== ===
Thank u very much!

i had solved the problem using vector and string, just like u say!
This is the first problem i solved using STL!

That's very nice!

May 16 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Winston Smith | last post: by
7 posts views Thread by dan | last post: by
5 posts views Thread by Rob Somers | last post: by
20 posts views Thread by Chris LaJoie | last post: by
14 posts views Thread by Xarky | last post: by
10 posts views Thread by serge calderara | last post: by
4 posts views Thread by bob lambert | last post: by
6 posts views Thread by Jim M | last post: by
reply views Thread by Andy Dingley | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.