How do you output a variable declared as double to a text file in C ++

I am very new to C ++ and I am wondering how you output / write variables declared as double to a txt file. I know how to output strings using fstream, but I can't figure out how to send anything else. I am starting to think that you cannot send anything other than lines to a text file, is that correct? If so, how would you convert the information stored in a variable to a string variable?

Here is my code I'm trying to implement in this concept, it's pretty simple:

int main()
{

double invoiceAmt = 3800.00;
double apr = 18.5;            //percentage

//compute cash discount
double discountRate = 3.0;    //percentage
double discountAmt;

discountAmt = invoiceAmt * discountRate/100;

//compute amount due in 10 days
double amtDueIn10;

amtDueIn10 = invoiceAmt - discountAmt;

//Compute Interest on the loan of amount (with discount)for 20 days
double LoanInt;

LoanInt = amtDueIn10 * (apr /360/100) * 20;

//Compute amount due in 20 days at 18.5%.
double amtDueIn20;

amtDueIn20 = invoiceAmt * (1 + (apr /360/100) * 20);
return 0;
}

      

So, I am trying to use these variables and output them to a text file. Also please let me know what I need to use for this source code. Feel free to give suggestions on how to improve my code in other ways and also please.

Thanks in advance.

+2


source to share


6 answers


As shown in your tags, you are using file streams:

std::ofstream ofs("/path/to/file.txt");
ofs << amtDueIn20;

      

Depending on what you want the file for, you probably have to write more stuff (like spaces, etc.) to get decent formatting.

Change due to persistent rmagoteaux22 issues:

This code



#include <iostream>
#include <fstream>

const double d = 3.1415926;

int main(){
    std::ofstream ofs("test.txt");
    if( !ofs.good() ) {
        std::cerr << "Couldn't open text file!\n";
        return 1;
    }
    ofs << d << '\n';
    return 0;
}

      

compiles for me (VC9) and writes it to test.txt

:

3.14159

      

You may try?

+6


source


Just use the stream writing operator <<which has an overloaded definition for double (defined in basic_ostream)



#include <fstream>

...


    std::fstream stmMyStream( "c:\\tmp\\teststm.txt", std::ios::in | std::ios::out | std::ios::trunc );

    double dbMyDouble = 23.456;
    stmMyStream << "The value is: " << dbMyDouble;

      

+1


source


To answer your first question, in C, you use printf (and to output the file fprintf). IIRC, cout also has a large number of modifiers, but I will not mention them as you originally mentioned fstream (more C oriented than C ++) -


oops, missed the flow indicator, ignored my "C" comments, and used C ++


to improve your program, be sure to use parentheses when calculations like above should be 100% sure that everything evaluates the way you want them to be (don't rely on order of precedence)

0


source


Generally speaking, printf, wprintf, etc. are used to write to output.

In the case of files, these methods are named fprintf_s, fsprintf_s, etc.

Please note that the '_s' methods are new safe variants of the previous formatting methods. You should always use these new secure versions.

Examples: http://msdn.microsoft.com/en-us/library/ksf1fzyy%28VS.80%29.aspx

Note. These methods use a format specifier to convert the given type to text. For example,% d acts as a space holder for an integer. Likewise% f for double.

0


source


Just use the operator <<

in the output stream:

#include <fstream>

int main() {
  double myNumber = 42.5;
  std::fstream outfile("test.txt", std::fstream::out);
  outfile << "The answer is almost " << myNumber << std::endl;
  outfile.close();
}

      

0


source


I had the same problem, where the stream was outputting lines from, but stopped as soon as it reached a variable. With a little more Googling, I found this solution on the forum:

In Xcode 3.2, when creating a new project based on the stdC ++ project template, the build targets for Debug config add preprocessor macros that are incompatible with gcc-4.2: _GLIBCXX_DEBUG = 1 _GLIBXX_DEBUG_PEDANTIC = 1

Destroy them if you want Debug / gcc-4.2 to run correctly.

http://forums.macrumors.com/showpost.php?p=8590820&postcount=8

0


source







All Articles