Read data from my .txt?

This is a discussion on Read data from my .txt? within the C++ Programming forums, part of the General Programming Boards category; I wanna make a program that can easily read data from my document! If I have a .txt with these ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Denmark
    Posts
    16

    Read data from my .txt?

    I wanna make a program that can easily read data from my document!

    If I have a .txt with these values

    Code:
    ABC 1 2 3
    DEF 4 5 6
    GHI 7 8 9
    I want to use it as a database...

    But how do I make a function that reads data from a specific line and pulls out the numbers behind and gives them all names like

    Name = "DEF";
    Value1 = 4;
    Value2 = 5;
    Value3 = 6;

    The thign I need is that that I can type in, I want the data from line 10 and then the program takes the data and puts in into my variables

    I want to use fstream btw... Iam just not very good at streams atm...
    Last edited by Mech0z; 05-29-2006 at 09:14 AM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Basically:
    Code:
    struct CEntry
    {
      std::string Name;
      int Value1;
      int Value2;
      int Value3;
    };
    
    std::vector<CEntry> EntryList;
    std::ifstream File("MyData.txt");
    
    while(!File.eof())
    {
      CEntry Entry;
      File >> Entry.Name >> Entry.Value1 >> Entry.Value2 >> Entry.Value3;
      EntryList.push_back(Entry);
    }
    ...in its simplest form. May need some tweaking/error testing though.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Denmark
    Posts
    16
    So you are storing the data in a vector? Which I can then take the data from?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > while(!File.eof())
    Ahem - eof in a control loop FAQ ?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. data read zeros?
    By Question in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2003, 12:15 PM
  4. read data in files
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-18-2002, 11:45 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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