Thread: reading the nth item from a txt file

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    reading the nth item from a txt file

    if the items in the txt file are separated by space, or tab, how is it possible to read the nth item?

    lets say the txt is as follows:
    Code:
    2 3 4 5 6 7 8 9 0
    1 2 11 4
    55 6 1 9 0 35 4
    2 2 1
    and i want to only read the 3rd item on each row.

    the other questions is, how would you only count the number of items on the first row of the txt file? in the above case, it should be 9 since the 1st line has 9 items.

    thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Read in a line into a string, then parse the line. Since those are integers, the easiest way to parse a line would probably be to create a stringstream with the string and then use a loop to read each integer from the stringstream. Use a counter to find the third one and to identify the total number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM