Visual C++ Developer Center >
Visual C++ Forums
>
Visual C++ General
>
c++ i/o stream+string question
c++ i/o stream+string question
- a.txt contain the following:
student_name,student_no,marks
Chan Tai Man,20091001,49
Wong Siu Keung,20091014,80
Chau Cheong,20091012,58
Cheung Man Kit,2008122334,77
Han Keung,2008345778,45
below is the program:
File name: a.txt
Field number or name: student_name
Search string: ha
* begin output
Chan Tai Man,20091001,49
Chau Cheong,20091012,58
* end output
Field number or name: student_no
Search string: 4
* begin output
Wong Siu Keung,20091014,80
Cheung Man Kit,2008122334,77
Han Keung,2008345778,45
* end output
can anyone help me to write this difficult program??
Answers
- Hello Harry,
1) Read txt file into vector<string>
At first, you should read the text file, line by line and keep it as a vector<string>
Refer this article - Reading a text file into an STL vector of strings .
2) Read search string from console
Refer this article - Read string from console .
3) Iterate the vector
Iterate through each item in vector of strings.
This article will be helpful - Accessing Elements of C++ Vector .
4) Search for string
While iterating through vector, for each string check whether the substring is present or not.
You can use string::find() to check that. If yes, then print it to console using cout.
Hope this solves your problem. Feel free to contact to ask more doubts if you have.
Good Luck!
Best Regards,
Jijo.
http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.- Proposed As Answer byNancy ShaoMSFT, ModeratorTuesday, November 10, 2009 9:19 AM
- Marked As Answer byNancy ShaoMSFT, ModeratorFriday, November 13, 2009 8:31 AM
- Quote>can anyone help me to write this difficult program?
Many of us can. I fact, we could probably give you the entire program
faster than we can coax you through it. But that won't help you learn
how to program. It may also be cheating, if this is a course assignment
as it appears to be.
Put together some code which does as much as you can accomplish on your
own. When you get stuck with one aspect of the problem, post your code
and describe which part you are having problems with.
Assignments usually build on functions and methods already covered in
the course. Since we don't know what you have covered, we can't suggest
code which uses that material. You will have to be very specific. Some
examples:
Are you to use C-style strings (nul-terminated char arrays) or a C++ string class?
Have you covered vectors or has your course been using "raw" char arrays?
When you covered file I/O did you use C functions (fopen, etc.) or C++ iostreams?
Have you covered methods of parsing file data? If so, which methods?
e.g. - strtok(), scanf(), sscanf(), C++ string class functions, etc.
What method(s) have you been using for console input and output?
Note that submitting a program which uses C/C++ language features not covered
to date in your course will be a sure tip off to your instructor/professor
that someone else wrote the code. Expect to get a failing grade.
- Wayne- Proposed As Answer byNancy ShaoMSFT, ModeratorTuesday, November 10, 2009 9:19 AM
- Marked As Answer byNancy ShaoMSFT, ModeratorFriday, November 13, 2009 8:31 AM
All Replies
- Hello Harry,
1) Read txt file into vector<string>
At first, you should read the text file, line by line and keep it as a vector<string>
Refer this article - Reading a text file into an STL vector of strings .
2) Read search string from console
Refer this article - Read string from console .
3) Iterate the vector
Iterate through each item in vector of strings.
This article will be helpful - Accessing Elements of C++ Vector .
4) Search for string
While iterating through vector, for each string check whether the substring is present or not.
You can use string::find() to check that. If yes, then print it to console using cout.
Hope this solves your problem. Feel free to contact to ask more doubts if you have.
Good Luck!
Best Regards,
Jijo.
http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.- Proposed As Answer byNancy ShaoMSFT, ModeratorTuesday, November 10, 2009 9:19 AM
- Marked As Answer byNancy ShaoMSFT, ModeratorFriday, November 13, 2009 8:31 AM
- Quote>can anyone help me to write this difficult program?
Many of us can. I fact, we could probably give you the entire program
faster than we can coax you through it. But that won't help you learn
how to program. It may also be cheating, if this is a course assignment
as it appears to be.
Put together some code which does as much as you can accomplish on your
own. When you get stuck with one aspect of the problem, post your code
and describe which part you are having problems with.
Assignments usually build on functions and methods already covered in
the course. Since we don't know what you have covered, we can't suggest
code which uses that material. You will have to be very specific. Some
examples:
Are you to use C-style strings (nul-terminated char arrays) or a C++ string class?
Have you covered vectors or has your course been using "raw" char arrays?
When you covered file I/O did you use C functions (fopen, etc.) or C++ iostreams?
Have you covered methods of parsing file data? If so, which methods?
e.g. - strtok(), scanf(), sscanf(), C++ string class functions, etc.
What method(s) have you been using for console input and output?
Note that submitting a program which uses C/C++ language features not covered
to date in your course will be a sure tip off to your instructor/professor
that someone else wrote the code. Expect to get a failing grade.
- Wayne- Proposed As Answer byNancy ShaoMSFT, ModeratorTuesday, November 10, 2009 9:19 AM
- Marked As Answer byNancy ShaoMSFT, ModeratorFriday, November 13, 2009 8:31 AM
- Hi Harry,
Wayne and Jijo have given you great suggestion. There are a lot of ways to achieve your objective, you can use the way you like. And Wayne is right, it is easy to give you code than explaination. But you can learn a lot form the later way.
Best Regards,
Nancy
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


