How to input doubles in to files

This is a discussion on How to input doubles in to files within the C++ Programming forums, part of the General Programming Boards category; This is my code Code: String="("+(std::string)P+"*"+(std::string)L+")"+" ^ "+(std::string)T+" /4= "+(std::string)A+"\n"; a_file<<String; P,L,T,A are doubles how can I get a_file to ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    How to input doubles in to files

    This is my code

    Code:
    	String="("+(std::string)P+"*"+(std::string)L+")"+" ^ "+(std::string)T+" /4= "+(std::string)A+"\n";
    	a_file<<String;
    P,L,T,A are doubles how can I get a_file to write doubles in to a file

    I tried without the (std::string) and I tried just doin

    Code:
    a_file<<"("<<P<<"*"<<L<<")"<<" ^ "<<T<<" /4= "<<A<<"\n";
    I just don't know what to do

    I also tried

    Code:
    	String="("+P+"*"+L+")"+" ^ "+T+" /4= "+A+"\n";
    	a_file<<String;
    Last edited by bikr692002; 08-27-2006 at 01:52 PM.

  2. #2
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Have you tried setting the stream to print in fixed or scientific notation?
    Code:
    a_file << std::fixed <<"("<<P<<"*"<<L<<")"<<" ^ "<<T<<" /4= "<<A<<"\n";

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    no I haven't tried that yet. A person told me to look up sprintf

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    man this sprintf is really fuggin confusing

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Just tried your way and it worked. Thank you, Hikaru.

  6. #6
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by bikr692002
    man this sprintf is really fuggin confusing
    Yeah, it is. It's also hard to use in C++ since it's a function designed for C. C++ has better stuff like strings and stringstreams that are really intuitive and easy to use, not to mention a lot safer.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    sprintf() is not difficult to use.

    Code:
    char text[5];
    float fValue=20.0;
    sprintf(text,"%f",fValue);
    
    printf("%f",etxt);
    MS has another function called sprintf_s which is:

    sprintf(char *dest,size_t length,const char *format,char *src)

    Don't believe MSVC 2005 when it tells you sprintf() has been declared deprecated. It hasn't except by Microsoft alone. It's obvious MS cannot manually count string length + null terminator so they felt the need to make their own function. It in turn probably does some type of internal copy using the length and then calls sprintf().

    Make sure your char buffer has enough room to accomodate the conversion or you will get some odd results. You must also allow for the null-terminator.
    Last edited by VirtualAce; 08-27-2006 at 06:40 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    But in this case, you really don't need sprintf. Just output the double directly to the stream. If it is not in the format you expect, use a manipulator like Hikaru showed. If that's still not quite right, show the output you get and the output you want.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Yeah and I didn't address that but Daved is right. No need for sprintf here. But it's not hard to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  2. How to get input from multiple files?
    By jumpyg in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 08:20 PM
  3. h/w help
    By helpme in forum C Programming
    Replies: 20
    Last Post: 10-21-2003, 09:36 AM
  4. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  5. string input from files
    By jriano in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2003, 01:08 PM

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