Thread: help with stl search/find, or something else- stumped!

  1. #1

    help with stl search/find, or something else- stumped!

    I know we're not supposed to ask for complete answers on this board, but I'm on vacation, and I'm trying to do a question, and I'm completely stumped:

    Here's the question (please just help me in the right direction):

    Please write a program that does the following:
    1. Requests a file name from the user.
    2. Opens the file, quitting if the file is not valid.
    3. Asks the user for the string.
    4. Reads in the file, line by line, and searches in each line to see if the string the user typed in occurs in the line. 
    ********  I'm not sure how to read in line by line???  I only know how to do this with istream_iterators************
    5. If the string is in the line, the whole line is written out to a file called "occs.txt", along with the number that the line occupied in the original file.  ********** I'm not sure how to cout the line number of the file either **********
    if there is a files called test.txt which contains:
    hello there
    why do birds
    suddenly there
    And the user request that file, and searches for the string "there", then occs .txt should read:
    Line 1: hello there
    Line 2: suddenly there
    solve this problem using the c++ standard libraries, not the c equivalents.  So use ifstream, and ofstream for the i/o, and string of course.  A careful search through string in the STL primer(book) may provide extra clues to simplify your task

    I'm truely stumped on this one. If anyone could please lead me in the right direction, I'd appreciate it.


  2. #2
    in the question above, it should say:
    Line 3: suddenly there, not line 2,


  3. #3
    Registered User
    Join Date
    Apr 2002
    Alright I'll throw something out here.

    You know you're going to use file streams for the File I/O obviously. You say you don't know how to get a line at a time though. So I assume you are familiar with File I/O though. Bascially you'll use something like

    string line;
    ifstream fin( "FileToOpen.txt" );
    getline( fin, line, '\n' );
    Basically you'll want to keep looping through doing this for every line. You'll want to have a counter also to keep track of what line you are on. As far as searching to see if a certain string of letters is in a given string, use find.

    string name = "This is a string";
    int index = name.find( "his" );
    This will return 1 because its 1 character from the beginning where "his" is in that string. If the string is NOT found it will return -1. Hope this helps a little bit. Good luck.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    thanks, got it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 05:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM