fopen with std::string

This is a discussion on fopen with std::string within the C++ Programming forums, part of the General Programming Boards category; Thank you Daved, i get it now. Tabstop: "File I/O is buffered, and if you kill the program (which is ...

  1. #31
    Registered User
    Join Date
    Dec 2007
    Posts
    821
    Thank you Daved, i get it now.

    Tabstop:
    "File I/O is buffered, and if you kill the program (which is the only way this program is going to stop) you may or may not get that buffer written out."

    The same program works with fopen(). Does it mean that fopen() not buffered?
    Last edited by Ducky; 07-31-2009 at 05:50 PM.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #32
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    The same program works with fopen(). Does it mean that fopen() not buffered?
    Reads and writes are buffered. fopen() is not a read or a write...
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #33
    Registered User
    Join Date
    Dec 2007
    Posts
    821
    I meant the combination fopen() with fputs().

    Like ofstream with <<.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #34
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Yes, fputs() is buffered. You can flush the buffer to the file by calling fflush(), or you can just close the file with fclose().
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #35
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can always flush the fstream stream manually as well (using std::flush or std::endl).

  6. #36
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by Daved View Post
    >> The only thing to really remember is to use std::ios:binary when you are doing read and write if you want code that works on Windows

    That doesn't make sense and I'm pretty sure it's not true. I'm not sure why you'd think that.
    Try it out yourself then.

  7. #37
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I've done reading and writing on Windows many times in text mode and it has worked just fine.

    Were you specifically talking about the read and write functions?

  8. #38
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by Daved View Post
    Were you specifically talking about the read and write functions?
    Yes.

  9. #39
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    And what is it about Windows that make those functions only work in binary mode that doesn't happen on Linux or other platforms?

  10. #40
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by Daved View Post
    And what is it about Windows that make those functions only work in binary mode that doesn't happen on Linux or other platforms?
    I believe it is the view UNIX has, everything is a file, every file is binary (no such thing as text), thus you automatically get the fstream treated as binary.

  11. #41
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Quote Originally Posted by Tux0r View Post
    I believe it is the view UNIX has, everything is a file, every file is binary (no such thing as text), thus you automatically get the fstream treated as binary.
    You are wrong.

    The short answer is that you should use text mode if you are reading/writing text, and use binary mode if you are reading/writing binary data. This should be done regardless of what platform you use.

    Now it just so happens that Unix systems don't do any character conversions in text mode, so it doesn't matter if use text or binary mode. It is best to just stick to the above principal though, and not rely on the platforms handling of text.

    In this case, the poster is dealing with text, so the file should be opened in text mode.
    bit∙hub [bit-huhb] n. A source and destination for information.

  12. #42
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by bithub View Post
    You are wrong.

    *captain obvious removed*

    Now it just so happens that Unix systems don't do any character conversions in text mode, so it doesn't matter if use text or binary mode. It is best to just stick to the above principal though, and not rely on the platforms handling of text.

    In this case, the poster is dealing with text, so the file should be opened in text mode.
    lol, you say I am wrong then you state what I just said?

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by Tux0r
    lol, you say I am wrong then you state what I just said?
    You stated:
    Quote Originally Posted by Tux0r
    The only thing to really remember is to use std::ios:binary when you are doing read and write if you want code that works on Windows
    Granted, bithub did not quote that, but the principle (or perhaps rule of thumb) that bithub proposed is in contradiction to the one that you proposed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #44
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    The only thing to really remember is to use std::ios:binary when you are doing read and write if you want code that works on Windows
    This is wrong. He is using text, he should not be using binary mode to read and write. My post was to clarify this.

    every file is binary (no such thing as text),
    This is wrong. Every file in unix is not binary. There are plenty of text files (look at everything in the /etc directory). Just because Unix does not perform text conversion does not mean there is "no such thing as text".

    I fail to see how I stated what you said. You said "use binary". I said "use text". Those seem to be the opposite...
    bit∙hub [bit-huhb] n. A source and destination for information.

  15. #45
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by bithub View Post
    This is wrong. He is using text, he should not be using binary mode to read and write. My post was to clarify this.

    This is wrong. Every file in unix is not binary. There are plenty of text files (look at everything in the /etc directory). Just because Unix does not perform text conversion does not mean there is "no such thing as text".

    I fail to see how I stated what you said. You said "use binary". I said "use text". Those seem to be the opposite...
    I don't see why to argue about this but oh well. I was just giving OP an advice that is not directly related to the original question (remember to use std::ios::binary when using read and write if you want windows compatibility). And so:

    >> Now it just so happens that Unix systems don't do any character conversions in text mode, so it doesn't matter if use text or binary mode. It is best to just stick to the above principal though, and not rely on the platforms handling of text.

    is just restating what I said about it being vague on UNIX. I don't see why you said I was wrong and then came to the conclusion that I came to as well (marked bold).


    also http://en.wikipedia.org/wiki/Binary_file#Manipulation
    Last edited by Tux0r; 08-03-2009 at 02:59 PM.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. std::string: Has my compiler gone nuts??
    By Andruu75 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2007, 05:02 AM
  3. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 08:08 PM
  4. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 01:34 PM
  5. returning std::string
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2001, 09:31 PM

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