ifstream/ofstream are both creating a file...

Started by
5 comments, last by Sand_Hawk 21 years, 6 months ago
I am using ofstream/ifstream objects for file access. But when I use this code:

ifstream FromFile("From.cpp");
ofstream ToFile("To.cpp", ios::noreplace);
 
If From.cpp doesn''t exist it gets created. But I don''t want the file to be created. What am I doing wrong? Sand Hawk ---------------- -Earth is 98% full. Please delete anybody you can.
My Site
----------------(Inspired by Pouya)
Advertisement
ios::noreplace fails if the file exists..

use ios::nocreate, which will fail if the file doesn''t exist.


:D ~z~ :D
Hehe, he allready heard the anwer in TA... yay to my books
quote:Original post by Anonymous Poster
ios::noreplace fails if the file exists..
use ios::nocreate, which will fail if the file doesn''t exist.


These flags do not exist in standard C++.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I thought that, but wasn''t sure. Thanx for the info.
But they work, don''t they? :D
Not with std::fstream, std::ifstream, nor with std::ofstream, which are the only standard file stream classes, they don't.

std::ifstream ifs( "foo.txt" ) would simply not create the file, but just fail (i.e. ifs.bad() would be true).

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on October 3, 2002 2:01:37 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement