Thread: fstream doesn't work well

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Question fstream doesn't work well

    The code
    Code:
    fstream myfile;
    
    myfile.open("C:\\myfile.ddt")
    Doesn't create a new file.

    I tried
    Code:
    myfile.open("C:\\myfile.ddt", ios::in | ios::out)
    too.

    My book says it should create a file if the file does not exist.
    It is my first use of fstream. Using ios:ut alone is alright.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    i dont think fstream will create a file but if you use ofstream it will..

    though fstream should if you do a ios:: out by itself..

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    it didn't work because you didn't tell it whether it is for input or output. The code below will also truncate an existing file to 0 length if it already exists.
    Code:
    fstream myfile;
    
    myfile.open("C:\\myfile.ddt", ios::out | ios::trunc)

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Of course it is for both input and output else I wouldn't use fstream. It is a database so it can't be 0 lengthed each time.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Open for output first, then close immediately and open for input and output.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Thanks, I'll do that.
    My book is old. And this behavior of fstream seems bad to me.

    Once I started a thread and asked if C++ STL uses C functions or not. From the answers I got, I concluded it doesn't. But at least in my compiler it uses C functions to handle the files.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by siavoshkc
    Once I started a thread and asked if C++ STL uses C functions or not. From the answers I got, I concluded it doesn't. But at least in my compiler it uses C functions to handle the files.
    That was a wrong conclusion. If it's the thread I remember, we were very explicit in telling you that it is the library implementation's business how it achieves the specified functionality, and that you as the client programmer have no business inquiring about it. Let the library do what it wants, as long as it does what it claims.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    it is the library implementation's business how it achieves the specified functionality, and that you as the client programmer have no business inquiring about it.
    I just wanted to know that, by standard ,C++ STL uses C routines or not. I concluded there is no standard for it, and what is standard, is just the function duty.
    [edit]
    When new C++ standard will be defined?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Whether or not it uses C routines does not mater. The implementation is hidden because you need not know how it was implemented to use it. As long as the implementation follows standards and performs as expected by those standards.

    From what I hear, new ISO C++ should be in '09. But, that was from the freenode ##C++ channel.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by siavoshkc
    Thanks, I'll do that.
    My book is old. And this behavior of fstream seems bad to me.
    you should buy a new book instead of relying on a book that is out-of-date.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> And this behavior of fstream seems bad to me.
    If you attempt to open a file for input and that file does not exist, does it really make sense to create an empty file?

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by siavoshkc
    Of course it is for both input and output else I wouldn't use fstream. It is a database so it can't be 0 lengthed each time.
    sorry, apparently I misread your original post. I think the documentation on STL functions is pretty poor, but I reall reading somewhere that the file will not be created when opened for input ( in|out is considered open for input). descriptions for C's fopen() are much more clear about that.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Wraithan
    From what I hear, new ISO C++ should be in '09. But, that was from the freenode ##C++ channel.
    Let's put it this way: they want it to really be a C++0x standard, i.e. they want to finish it before 2010. But they're not exactly confident they'll finish it before 09, so yeah, that's the most likely year.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Whether or not it uses C routines does not mater. The implementation is hidden because you need not know how it was implemented to use it. As long as the implementation follows standards and performs as expected by those standards.
    It is not my fault. I went to MSDN for fstream, it passed me to basic_fsteram and from there to rdbuf->open() and next to fopen(), explaining
    Code:
    ios_base::in becomes "r" (open existing file for reading).
    
    ios_base::out or ios_base::out | ios_base::trunc becomes "w" (truncate existing file or create for writing).
    
    ios_base::out | app becomes "a" (open existing file for appending all writes).
    
    ios_base::in | ios_base::out becomes "r+" (open existing file for reading and writing).
    
    ios_base::in | ios_base::out | ios_base::trunc becomes "w+" (truncate existing file or create for reading and writing).
    
    ios_base::in | ios_base::out | ios_base::app becomes "a+" (open existing file for reading and for appending all writes).
    Why C++ STL should call a C function. This makes people think C is more powerful that C++.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by siavoshkc
    Why C++ STL should call a C function. This makes people think C is more powerful that C++.
    How so? How do you define power, anyway?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  2. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  3. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 11:53 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM