Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ printf help

  1. Jan 21, 2004 #1
    I have a program due in couple hours. Professor expects us to know C++ when we never really went over programming; he expects us to just know it .. anyhow

    I have two inputs used with scanf

    it's toll and norm

    these two values need to be put into a data file created by the program

    I need a key that allows me to
    create a "data.txt" file and if the file exist to overwrite it by erased etc.. doesn't matter and put in the things as

    printf(" Toll = %e \m Norm = %e", toll, norm);

    and just exit the program

    i know it's coded in C... but that's all i know
    anyhow if i can code in C or C++ doesnt' matter also could you tell me what library i can use.
  2. jcsd
  3. Jan 21, 2004 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Re: C++

    Hi omgitsroy

    I don't have my C++ book on me, so the syntax may be a bit off.

    Code (Text):

    #include <fstream> // File operations


    ofstream output("data.txt" , ios::out) ; //Opens a file object 'output' to file "data.txt"
                                             //for output, overwrites if existing


    output<<"Toll = "<< toll <<"\m  Norm = "<< norm << endl; //Outputs to file just like 'cout' command

    I don't remember how to explicitly close the file, but I don't think it's really necessary unless your program is complex. Formatting can be done with standard i/o commands.

    Hope that's all correct...
  4. Jan 21, 2004 #3
    thanks a lot
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook