Click to See Complete Forum and Search --> : A file problem
babyboomer
July 30th, 2006, 02:48 PM
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.
ifstream inFile;
inFile.open("u.txt" );
Thanks...
Calculator
July 30th, 2006, 02:58 PM
I doubt it with an ifstream like that. From: http://wwww.dinkum.com/manuals/default.aspx?Page=fstream.html
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);
And here's info about all the openmode's: https://www.dinkumware.com/manuals/?manual=embedded&page=fstream.html#filebuf::open
The member function endeavors to open the file with filename filename, by calling fopen(filename, strmode). Here strmode is determined from mode & ~(ate | binary):
* 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 | ios_base::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).
If mode & ios_base::binary is nonzero, the function appends b to strmode to open a binary stream instead of a text stream. It then stores the value returned by fopen in the file pointer fp. If mode & ios_base::ate is nonzero and the file pointer is not a null pointer, the function calls fseek(fp, 0, SEEK_END) to position the stream at end-of-file. If that positioning operation fails, the function calls close(fp) and stores a null pointer in the file pointer.
In 'r', the file must exist. It will not create it.
JohnyDog
July 30th, 2006, 03:10 PM
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:
ifstream inFile("u.txt", ios::in);
babyboomer
July 30th, 2006, 03:16 PM
But it happens I can't understand what is going on
Calculator
July 30th, 2006, 03:29 PM
#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;
}
}
You heard the man. Move out!
humptydumpty
July 30th, 2006, 11:18 PM
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.
ifstream inFile;
inFile.open("u.txt" );
Thanks...
To Calculator
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/showpost.php?p=1408684&postcount=2
Thanx
Calculator
July 31st, 2006, 11:50 AM
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.
Philip Nicoletti
July 31st, 2006, 12:21 PM
Doing a small test under an old version of Visual C++ (ver 5),
it looks like if you use:
#include <fstream.h>
instead of the standard header
#include <fstream>
The file is created, even if it does not exist. I think there is a no create
option under fstream.h , but I do not remember what it is called.
EDIT : found it ...
in.open("thisdoesnotexist.txt",ios::nocreate); // non-standard
You should use the standard headers as shown by calculator.
humptydumpty
July 31st, 2006, 11:44 PM
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.
There is no hard and fast rule .but for writing a Program in a Efficient manner all of this is compulsory.have a look here
/* 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
NMTop40
August 1st, 2006, 04:38 AM
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.
TomWidmer
August 1st, 2006, 08:05 AM
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"?
humptydumpty
August 2nd, 2006, 12:50 AM
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:
SuperKoko
August 2nd, 2006, 02:19 AM
Never use conio.h use conio
That is the opposite.
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.