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

This is a discussion on help with stl search/find, or something else- stumped! within the C++ Programming forums, part of the General Programming Boards category; I know we're not supposed to ask for complete answers on this board, but I'm on vacation, and I'm trying ...

  1. #1
    Terranc
    Guest

    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):

    Code:
    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 **********
    
    Example:
    
    if there is a files called test.txt which contains:
    
    hello there
    why do birds
    suddenly there
    appear?
    
    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
    
    note:
    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.

    thanks,
    Terrance

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

    thanks

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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

    Code:
    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.

    Code:
    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
    Terranc
    Guest
    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-24-2004, 12:22 AM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 03:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21