The code below is supposed to open an existing file to read, but when you enter a nonexisting file it creates that file! Why does this happen and how can I solve this problem.
Thanks...Code:
ifstream inFile;
inFile.open("u.txt" );
Printable View
The code below is supposed to open an existing file to read, but when you enter a nonexisting file it creates that file! Why does this happen and how can I solve this problem.
Thanks...Code:
ifstream inFile;
inFile.open("u.txt" );
I doubt it with an ifstream like that. From: http://wwww.dinkum.com/manuals/defau...e=fstream.html
And here's info about all the openmode's: https://www.dinkumware.com/manuals/?...#filebuf::openCode:explicit basic_ifstream(const char *filename,
ios_base::openmode mode = ios_base::in);
void open(const char *filename,
ios_base::openmode mode = ios_base::in);
In 'r', the file must exist. It will not create it.Quote:
Originally Posted by A person who is very smart
Also you don't have to call open explicitely if you don't need to, you can pass the filename and flags to constructor of ifstream object:
Code:ifstream inFile("u.txt", ios::in);
But it happens I can't understand what is going on
You heard the man. Move out!Code:#include <fstream>
#include <iostream>
int main()
{
std::ifstream in("thisdoesnotexist.txt");
if(!in)
{
std::cout << "Failed. If it now exists, then "
"please give compiler / OS details." << std::endl;
}
in.clear();
in.open("thisdoesnotexist.txt");
if(in)
{
std::cout << "That is so so wierd. Definitely tell " \
"us compiler / OS details" << std::endl;
}
}
To CalculatorQuote:
Originally Posted by babyboomer
why Not main return 0 here on SuccessFully Completion .and Where you Close the File.
Noe Come to Main Question
Have a look on this Link to understand How to Do this in a Efficient Manner
http://www.codeguru.com/forum/showpo...84&postcount=2
Thanx
In C++, return 0 is not a requirement. Also, the ifstream's destructor is called automatically, which closes the file. His problem is that opening an ifstream that does not exist seems to create the file, which is not standard behavior.
Doing a small test under an old version of Visual C++ (ver 5),
it looks like if you use:
instead of the standard headerCode:#include <fstream.h>
The file is created, even if it does not exist. I think there is a no createCode:#include <fstream>
option under fstream.h , but I do not remember what it is called.
EDIT : found it ...
Code:in.open("thisdoesnotexist.txt",ios::nocreate); // non-standard
You should use the standard headers as shown by calculator.
There is no hard and fast rule .but for writing a Program in a Efficient manner all of this is compulsory.have a look hereQuote:
Originally Posted by Calculator
Code:/* Include the necessary libraries for a basic program
Because this program is intended for C++ ,you should include the conio.h library that has the getch() function.
*/
#include <iostream>
#include <conio>
using namespace std;
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
/* Every C++ function must have a main() function
The current C++ Standard recommends that the main() function
return a value. In this case, it will return an integer.
The main() function is the entry point of the program.
This version of main() takes two arguments. Sometimes the
arguments are not used at all, as is the case in this program.
*/
int main(int argc, char* argv[])
{
// Display a simple sentence to the user
cout << "Welcome to the wonderful world of C++ Programming!!!\n";
/* Because we are using C++, we will explicitly
prompt the user to press an key in order to stop the
program. Upon pressing a key, we will call the getch()
function to receive it
*/
cout << "\nPress any key to continue...";
getch();
// The program went alright, so return 0
return 0;
}
Thanx
The real gap in the library is that you cannot open a file in one go to read and write, creating the file if it doesn't exist but not truncating it if it does.
The only way to do that is open for append, then close then open again for read and write.
humptydumpty: <conio.h> is a non-standard header (not usually available on Unix for example), and I've never come across <conio>. Don't use them in portable code. In addition, what does your posted code have to do with "writing a Program in a Efficient manner"?
agreed .that's a Mistake. Never use conio.h use conio .but i forget to mention above example is in case of borland C++.Anyway where you found <conio.h> in my code. Because i am unable to see it.
Thanx:thumb:
That is the opposite.Quote:
Originally Posted by humptydumpty
conio.h is supported by several compilers under Windows and DOS.
Here, you rely on the fact that Borland C++ automatically adds ".h" if it is omitted and there is no matching file.