File I/O

This is a discussion on File I/O within the C++ Programming forums, part of the General Programming Boards category; I'm knew to file I/O and was wondering if I could get help for my scenario: I have a bunch ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    4

    Question File I/O

    I'm knew to file I/O and was wondering if I could get help for my scenario:
    I have a bunch of html files, and I want my program to open each one, and find the number that comes after "Number of hits:" in line 67 (for each one). I would like for an array like this in the end:
    [FILENAME, number-of-hits, FILENAME2, number-of-hits, ....]

    All replies appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What do you need help on?

    Kuphryn

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    4
    How can I go to a specific line in the file, search for the string "Number-of-hits:" in that line, and then take the number that comes after "Number-of-hits" and store that into an array, for each file.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>How can I go to a specific line in the file
    You could call getline() until you get to the line you're looking for. Alternately, you could read a single character at a time, until you reach a '\n', which would signal a new line.

    >>search for the string "Number-of-hits:" in that line
    Store the line in a std::string, and call find().

    >>take the number that comes after "Number-of-hits"
    If the first part of the line is "Number-of-hits", call substr() using the length of "Number-of-hits" as the offset. That'll get you the number as a string. Then you can call atoi(sub.c_str()), or use a stringstream, or sscanf, or anything else you choose to convert the string to an integer.

    >>and store that into an array, for each file.
    Store it like you would in any other array...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

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