Reading numbers from a file

This is a discussion on Reading numbers from a file within the C++ Programming forums, part of the General Programming Boards category; I know this is a stupid question, but how can I read numbers from a file and store them in ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    73

    Reading numbers from a file

    I know this is a stupid question, but how can I read numbers from a file and store them in an int, double, or float variable? I am quite familar with how characters can be read and stored in a string, but how can this be done with numbers?

    Thanks! ^_^

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    declare an fstream in ios::in mode or an ifstream, and associate either one with the desired file. Then just use the >> operator as you would with cin.


    fstream fin("myFile.txt", ios::in);
    fin >> num;

    ifstream fin("myFile.txt");
    fin >> num;

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    73
    Thank you so much! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem reading numbers in a txt file
    By nimamc in forum C Programming
    Replies: 3
    Last Post: 06-03-2009, 02:35 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  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

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