Reading info from a text document.

This is a discussion on Reading info from a text document. within the C++ Programming forums, part of the General Programming Boards category; Hi all, I want to be able to read info from a text document, and have the user select it ...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262

    Reading info from a text document.

    Hi all, I want to be able to read info from a text document, and have the user select it for the program I'm writing. The text document will be full of names, they will be numbered and each on a new line. If the user wants to get one I'd like it so that they'd only have to put in the number of the person they want to select, and that will be stored as a variable. does anyone know how to do that?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    I'd use the <fstream> library as I use it all the time.
    Just declare 2 variables to assist reading in the numbers and the name, so an int and a char[].

    Then keep looping the variables and compare them until you find it.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Assuming you are using C++, I would use a string instead of a char[]. You could read in each line and store it in some sort of container. One possibility is a vector, which would work well if the numbers start at 0 and are consecutive. You could also use a map, which automatically sorts the data based on the key (the number).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text from a file
    By gustavson in forum C Programming
    Replies: 1
    Last Post: 10-26-2007, 02:18 AM
  2. Reading text files on the internet
    By Mavix in forum C# Programming
    Replies: 8
    Last Post: 06-20-2007, 06:38 AM
  3. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. problems reading in from text
    By requiem in forum C++ Programming
    Replies: 5
    Last Post: 04-25-2003, 01:13 AM

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