read values from a txt file

This is a discussion on read values from a txt file within the C++ Programming forums, part of the General Programming Boards category; i know this a very baxic question but i don't have anyone to ask so please take pity on me! ...

  1. #1
    Unregistered
    Guest

    Question read values from a txt file

    i know this a very baxic question but i don't have anyone to ask so please take pity on me!

    i need to read in a list of numbers from a text file and save them to various variables. my problem isn't in reading the actual numbers in but that i don't know how to make an array of individual numbers represented by characters as one number.
    for exp:

    char b[]={ 1 , . , 5 , 3 }

    but how do i get

    double n=1.53

    ???

    or am i going about this all wrong?

  2. #2
    Unregistered
    Guest
    Well, I hope I understand your question...it looks to me like you want to read in numbers as represented in a text file and assign them to variables of type double instead of string or char*. The best way I knwo of to do this is to use a file stream, which you can then read from just like cin:

    Code:
    #include <iostream>
    #include <fstream>
    
    int main() {
        ifstream inFile("numbers.txt");
        double a, b, c;
        inFile >> a >> b >> c;
    }
    If I am misinterpreting your question, please post again with more details.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    char b[]={ 1 , . , 5 , 3 }

    but how do i get

    double n=1.53;
    Okay, I know of no standard function to convert strings to doubles. If you know the values won't be floating point, you can use atoi() to convert the string to a number. For example:

    char b[] = {"1234"};
    int a = atoi(b);

    Edit:: There's also atof() which converts a string into a floating point value. While you won't be getting the extra precision of a double, you can still extract the value from the string. Example:

    char b[] = {"1.345"}
    float a = atof(b);
    Last edited by Eibro; 08-30-2002 at 12:31 PM.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You can use stringstreams to convert from any type to anytype as long as the stream output from one type is readable as stream input from the other type.

    Code:
    #include <sstream>
    #include <iostream>
    
    // generalized notion of stream_casting
    template <class outType, class inType>
    outType stream_cast(const inType& in)
    {
    	outType out;
    	std::ostringstream outStream;
    	outStream << in;
    	std::istringstream inStream(outStream.str());
    	inStream >> out;
    	return out;
    }
    
    
    int main() {
    	// specific example of stream casting
    	char numAsChar[]="1.532";
    	double numAsDouble;
    	std::ostringstream out;
    
    	out << numAsChar;
    	std::istringstream in(out.str());
    
    	in >> numAsDouble;
    
    	std::cout << numAsDouble << std::endl;
    
    	// use templated general stream cast
    	numAsDouble = stream_cast<double>(numAsChar);
    
    	std::cout << numAsDouble << std::endl;
    
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Unregistered
    Guest
    thank you so much to all of you that answered!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read from a sequential txt file
    By hen_ry in forum C Programming
    Replies: 8
    Last Post: 05-08-2009, 10:29 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. how do you read a whole line of text from a txt file
    By themexican in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2005, 09:17 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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