Microsoft Developer Network > 포럼 홈 > Visual C++ General > c++ i/o stream+string question
질문하기질문하기
 

답변됨c++ i/o stream+string question

  • 2009년 11월 8일 일요일 오전 3:31harry chan 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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??

답변

  • 2009년 11월 8일 일요일 오전 4:02Jijo RajMVP사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    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.
  • 2009년 11월 8일 일요일 오전 4:14WayneAKing 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    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

모든 응답

  • 2009년 11월 8일 일요일 오전 4:02Jijo RajMVP사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    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.
  • 2009년 11월 8일 일요일 오전 4:14WayneAKing 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    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
  • 2009년 11월 10일 화요일 오전 9:19Nancy ShaoMSFT, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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.