Reading float values from a file??

This is a discussion on Reading float values from a file?? within the C++ Programming forums, part of the General Programming Boards category; Hi, been looking at the tutorials and it only tells you how to read in charchters, is there a way ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    Reading float values from a file??

    Hi, been looking at the tutorials and it only tells you how to read in charchters, is there a way to ready floats in from a file of numerical data, or if not a way to convert the char data into floating point after reading it in, thanks Neil

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Assuming a file called file.txt that has the following contents (as an example):
    Code:
    3.457
    This would be all you needed to read that value in a program.

    Code:
    #include <fstream>
    
    ...
    
    std::ifstream input("file.txt");
    float fValue;
    
    input >> fValue;
    That is for floating point values represented as text in a file. If you are talking about binary data then the process is going to be a bit different.
    Last edited by hk_mp5kpdw; 02-23-2005 at 07:50 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. reading a float from file
    By jamie in forum C++ Programming
    Replies: 1
    Last Post: 02-26-2003, 08:27 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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