Reading data from a file..

This is a discussion on Reading data from a file.. within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. I am attempting to read data from a file but am having some troubles. My data file is ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    Reading data from a file..

    Hello everyone.

    I am attempting to read data from a file but am having some troubles.

    My data file is labeled LAB5.DAT
    and its info is the following, exactly:

    10A111AX145
    STEEL HAMMER
    10.95

    My goal here is to read in the first two letters/numbers, then the next one by itself, then the following 8.

    IE:

    Part number- 10
    Division - A
    Bin # - 111AX145

    Then read in the description, then the price.
    I have successfully written the program to read in the first line of data into the correct variables but cannot figure out how to read in the dscription or price, because they are on seperate lines. Here is my program so far...

    Code:
    int main()
    {
    
    ifstream LAB5;
    
    LAB5.open ("LAB5.DAT");
    
    int department;   
    char part_number[9];
    char bin;
    char description[14];     
    float unit_price;
    
    LAB5 >> setw(3) >> department;
    LAB5 >> setw(2) >> bin;
    LAB5 >> setw(9) >> part_number;
    LAB5.ignore(200);
    LAB5.getline(description, 14); 
    LAB5 >> setw(5) >> unit_price;
    
    cout << "department = " << department << endl;
    cout << "bin = " << bin << endl;
    cout << "part_number = " << part_number << endl;
    cout << "description = " << description << endl;
    cout << "unit price = $" << unit_price << endl;
    LAB5.close();
    
            return 0;
    }
    
    When I compile and execute the program, this is how it reads:
    
    department = 10
    bin = A
    part_number = 111AX145
    description = 
    unit price = $0

    *Please help. Not sure what im doing wrong!*

    -Mike

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You need to read in the newline.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    Yeah.. not sure how to do that. Thats the problem =)

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    LAB5.ignore(200);
    ->
    Code:
    LAB5.ignore(200, '\n');
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    Thank you sir.

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. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. reading data from a file
    By brianptodd in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2003, 05:50 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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