writing long double into ofstream file?

This is a discussion on writing long double into ofstream file? within the C++ Programming forums, part of the General Programming Boards category; Guess that discussion is useless. Youre expecting some magic trom the compiler that just cannot be done. The one thing ...

  1. #16
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Guess that discussion is useless. Youre expecting some magic trom the compiler that just cannot be done.
    The one thing that you didn't understand about C++ I/O is that the >> and << are just typesafe replacements of the C functions printf and scanf (-> text I/O).
    If you want binary IO then read and write are to be used.
    Kurt

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    You're missing the point. How do I know it deals with text?
    And, again, to newbies logic says it should write 1 and 2 and read 1 and 2, not 12.
    This is why I say be careful when using >> and <<.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Elysia View Post
    How do I know it deals with text?
    RTFM
    Kurt

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by ZuK View Post
    RTFM
    Told you. Doc doesn't mention or does so very poorly - no wonder it causes confusion!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I have to agree with the RTM assessment.

    Quote Originally Posted by Elysia View Post
    Told you. Doc doesn't mention or does so very poorly - no wonder it causes confusion!
    Perhaps you're not reading the right documentation. Even then, it should be 100&#37; obvious you're dealing with text when you open up the file in question!

    But to the reference about a newbie thought process, I would have to disagree up to a point. A newbie would have been used to std::cout and std::cin (hopefully), thereby being used to the idea of text processing, since everything in the console window is text. TBH, I think more newbies would be happy to think of everything as text. What point is it to them if they write to a file that notepad can't understand? lol. Naturally, though, I'm referring to newbies that have at least half a brain, and actually try to figure out why stuff works the way it does.

    For the newbies that are brain dead, they can assume whatever they want. They will shoot themselves in the foot regardless.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Yeah, anyway, let's wrap this up. The I/O is the way it is and can't be changed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The point I'm trying to drive home is that the C and C++ standard IO shouldn't be changed, at least not with regard to your complaint about not being able to write two integers side by side and have the compiler/runtime GUESS as to what the heck you meant when it tries to read it back.
    Last edited by MacGyver; 12-23-2007 at 05:50 AM.

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Everyone has different opinions. The runtime doesn't need to guess if it actually writes in binary mode. Or if it's possible to override the default text-mode behavior with binary behavior.
    We can argue all we want - you think it shouldn't be changed, I think it should, but we'll get nowhere since the library isn't going anywhere. You win. Happy?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Straight binary dumps are unportable. Text dumps are portable. Therefore text should be used so program and files are portable.

    I said this before, and you started complaining how using text is prone to errors. If you format your text to be undecipherable, yes, it's prone to errors. If you use some common sense, then no, text is easier.

  10. #25
    Registered User
    Join Date
    May 2006
    Posts
    23
    Thank you so much for your replies. I did as follows:

    Code:
    #include iomanip
    
    long double varr = 0.038918918918916
    ofstream out; 
    out.open("ktr.txt");
    
    out<<setprecision(18)<< varr;
    it works nice, simple. while reading I also read using getline and converting to long double again using istringstream.

  11. #26
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by wolfindark View Post
    while reading I also read using getline and converting to long double again using istringstream.
    This works for you because you were writing just a single value.
    What you really have to do is output a separator character to the file after each value if you want to write multiple values. The default separators are whitespace.
    And yes it is a good idea to use getline to read into a string and use a stringstream to extract the values. It makes dealing with errors a lot easier.
    Kurt

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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