a simple problem about using stl op text files

C

Chelong

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!
 
H

Hari

Chelong je napisao:
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
 
C

Chelong

Chelong said:
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!
 
Z

Zeppe

Chelong said:
Chelong said:
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
 
C

Chelong

Chelong said:
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-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 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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,211
Latest member
ChloeK3656

Latest Threads

Top