Thread: is there an easier or another way to insert things from a txt file

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    is there an easier or another way to insert things from a txt file

    ok i have text file it has something like this

    item1 5.00 7 no
    item2 6.00 9 yes
    item3 6.00 8 no
    item4 5.00 7 no


    and the list goes on


    so i have a struct
    Code:
    #include <iostream>
    #include <fstream>
    
    
    const int CAPACITY=15
    
    struct Database
    {
              string item;
              double cost;
              int amount;
              string availability;
    
    
    
    }; 
    
     Database list[CAPACITY];
    
    void Get_Info(){
    
    string filename;
        cout << "Enter the filename.txt ";
        cin >> filename;
    
    
        ifstream in;
        in.open(filename.c_str());
    
    for (int i= 0; i < CAPACITY; index++)
         {
    
    
                string item;
                double cost;
                int amount;
                string availability;
    
    
                getline(in, list[i].item);
                item = list[i].number;
                in >> list[i].number;
                in.get();
                cost = list[i].cost;
                in >> list[i].cost;
    
    
                amount= list[i].amount;
                in >> list[i].amount;
            
                availability= list[i].availability;
                in >> list[i].availability;
    
    
            }
    
    
    }
    
    int main(){
    
         
    
    
    }
    my code worked on a previous assignment but can someone explain to me what getline is doing is it getting the 1st item which is a string so i have to call getline and is there a simpler or easier way

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if the first line of your file is
    item1 5.00 7 no

    Then your first call to getline() is going to put all of that into the first list[i].item string.

    If all your items are single words, like "apples", "pears" etc, then simple >> operators will work just fine.

    But if you have complicated lines like
    Seedless Grapes 5.00 7 no
    then you pretty much have to use getline to read the whole line into a string, then try and pick the data out of it using whatever string operators you can.
    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.

  3. #3
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    You could potentially use a sign, such as ";" to denote the end of your line, and tell it to read until it reaches that sign, to get the full name no matter how long or complex it is, then it's all simple, a float an int and a single word.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can I insert an image to my C file?
    By Hritik in forum C Programming
    Replies: 18
    Last Post: 12-29-2012, 03:30 AM
  2. How to Insert a new line in a file?
    By my7dream2006 in forum C Programming
    Replies: 2
    Last Post: 02-17-2009, 04:18 AM
  3. File IO, insert mode?
    By OnionKnight in forum C Programming
    Replies: 3
    Last Post: 08-17-2005, 02:39 PM
  4. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  5. Replies: 1
    Last Post: 09-17-2001, 05:46 AM