Why doesn't this code work?

R

Rudolfs.Bundulis

As I understood it's better to use fstream instead of fstream.h. When I
was porting my code (i'm using VC6.0++) i needed to let go of such
things as ios::noreplace and ios::nocreate. I neede to check if file
exists before creating it. If it exists we prompt that we are
overwriting. The files ar being passed in as command line arguments.

I did it like that:

#include <fstream>

std::fstream outFile;
....
....
....
outFile.open(argv[2],std::ios_base::in);// the second argument is the
output file
if (! outFile)
{//file does not exist
outFile.open(argv[2],std::ios::eek:ut);
cout<<"Testing...";
}
else
{
cout<<"Warning: Output file already exists!\n";//prompt that it
exists
outFile.close();//close it
outFile.open(argv[2],std::ios::eek:ut); //open it for output
cout<<"Testing...";
}
outFile.close();

but this stuff didn't work if the file did not exist. It just created
an empty file;

I changed it like this:

std::ifstream tmpFile;
std::eek:fstream outFile;
....
....
tmpFile.open(argv[2],std::ios_base::in);
if (!tmpFile)
{
outFile.open(argv[2],std::ios_base::eek:ut);
cout<<"Testing...";

}
else
{
cout<<"Warning: Output file already exists!\n";
tmpFile.close();
outFile.open(argv[2],std::ios_base::eek:ut);
cout<<"Testing...";
}
outFile.close();

this stuff works fine, but i would like to get rid of the tmpFile (if
possible).
Any ideas? Why didn' t the first one work?
 
V

Victor Bazarov

As I understood it's better to use fstream instead of fstream.h. When
I was porting my code (i'm using VC6.0++) i needed to let go of such
things as ios::noreplace and ios::nocreate. I neede to check if file
exists before creating it. If it exists we prompt that we are
overwriting. The files ar being passed in as command line arguments.

I did it like that:

#include <fstream>

std::fstream outFile;
...
...
...
outFile.open(argv[2],std::ios_base::in);// the second argument is the
output file
if (! outFile)
{//file does not exist
outFile.open(argv[2],std::ios::eek:ut);
cout<<"Testing...";
}
else
{
cout<<"Warning: Output file already exists!\n";//prompt that it
exists
outFile.close();//close it
outFile.open(argv[2],std::ios::eek:ut); //open it for output
cout<<"Testing...";
}
outFile.close();

but this stuff didn't work if the file did not exist. It just created
an empty file;

I changed it like this:

std::ifstream tmpFile;
std::eek:fstream outFile;
...
...
tmpFile.open(argv[2],std::ios_base::in);
if (!tmpFile)
{
outFile.open(argv[2],std::ios_base::eek:ut);
cout<<"Testing...";

}
else
{
cout<<"Warning: Output file already exists!\n";
tmpFile.close();
outFile.open(argv[2],std::ios_base::eek:ut);
cout<<"Testing...";
}
outFile.close();

this stuff works fine, but i would like to get rid of the tmpFile (if
possible).
Any ideas? Why didn' t the first one work?

First off, you're using a rather old compiler/library implemenation.
Perhaps you should consider switching to a more up-to-date release
of their compiler/library.

Second, if the behaviour is specific to VC++, you need to ask in the
VC++ newsgroup (microsoft.public.vc.language).

Third, when handling files (existence, etc.) it is recommended to use
platform-specific mechanisms. I am sure you can always request the
OS to give you the status of the file. Standard means (as you are
finding out) are not very generic in those areas.

V
 
B

BobR

Victor Bazarov wrote in message ...
As I understood it's better to use fstream instead of fstream.h. When
I was porting my code (i'm using VC6.0++) i needed to let go of such
things as ios::noreplace and ios::nocreate. I neede to check if file
exists before creating it. If it exists we prompt that we are
overwriting. The files ar being passed in as command line arguments.
I did it like that:

#include <fstream>
...
I changed it like this:

std::ifstream tmpFile;
std::eek:fstream outFile;
...
tmpFile.open(argv[2],std::ios_base::in);
if (!tmpFile){
outFile.open(argv[2],std::ios_base::eek:ut);
cout<<"Testing...";
}
else{
cout<<"Warning: Output file already exists!\n";
tmpFile.close();
outFile.open(argv[2],std::ios_base::eek:ut);
cout<<"Testing...";
}
outFile.close();

this stuff works fine, but i would like to get rid of the tmpFile (if
possible).
Any ideas? Why didn' t the first one work?


Third, when handling files (existence, etc.) it is recommended to use
platform-specific mechanisms.

Ouch!

[ from an old post ]
// nwadc10 wrote:
// > I'm having trouble checking to see if a file already exists.
// Use the stat method which is portable on Windows, UNIX, Linux, and most
OS.

// other includes here
#include <sys/stat.h> // hmmm, maybe not as portable??

bool FileExist(char const *FileName){
struct stat my_stat;
return (stat(FileName, &my_stat) == 0);
}

bool IsDirectory(char const *FileName){
struct stat my_stat;
if(stat(FileName, &my_stat) != 0) return false;
return ((my_stat.st_mode & S_IFDIR) != 0);
}

// Many times, it's not just important to determind if the file exist, but
// it's also important to determind if it a file or directory.
// ----------------------------------------------------------------------


// int main(int argc, char* argv[]){
void FileExistMain(std::eek:stream &cout = std::cout){
// using std::cout // for NG posting
bool v1 = FileExist("c:/autoexec.bat");
bool v2 = FileExist("c:/nofile.bat");
bool v3 = FileExist("c:/config.sys");
bool v4 = FileExist("c:/nofile2.bat");
bool v5 = IsDirectory("c:/windows");
bool v6 = IsDirectory("c:/notA_dir");
bool v7 = IsDirectory("c:/WINNT");

cout<<"bool v1 = FileExist(\"c:\\autoexec.bat\"); ="<<v1<<std::endl;
cout<<"bool v2 = FileExist(\"c:\\nofile.bat\"); ="<<v2<<std::endl;
cout<<"bool v3 = FileExist(\"c:\\config.sys\"); ="<<v3<<std::endl;
cout<<"bool v4 = FileExist(\"c:\\nofile2.bat\"); ="<<v4<<std::endl;
cout<<"bool v5 = IsDirectory(\"c:\\windows\"); ="<<v5<<std::endl;
cout<<"bool v6 = IsDirectory(\"c:\\notA_dir\"); ="<<v6<<std::endl;
cout<<"bool v7 = IsDirectory(\"c:\\WINNT\"); ="<<v7<<std::endl;
return;
}

I did not save the posters name, so, I can not credit him/her.
 
M

Marcus Kwok

BobR said:
[ from an old post ]
// nwadc10 wrote:
// > I'm having trouble checking to see if a file already exists.
// Use the stat method which is portable on Windows, UNIX, Linux, and most
OS.

// other includes here
#include <sys/stat.h> // hmmm, maybe not as portable??

bool FileExist(char const *FileName){
struct stat my_stat;
return (stat(FileName, &my_stat) == 0);
}

bool IsDirectory(char const *FileName){
struct stat my_stat;
if(stat(FileName, &my_stat) != 0) return false;
return ((my_stat.st_mode & S_IFDIR) != 0);
}

// Many times, it's not just important to determind if the file exist, but
// it's also important to determind if it a file or directory.
// ----------------------------------------------------------------------

I did not save the posters name, so, I can not credit him/her.

http://www.codecomments.com/archive323-2005-10-664071.html
seems to say that it was Axter.
 
L

Larry Smith

BobR said:
Marcus Kwok wrote in message ...

Thank you, Marcus.
[ I made note of that in my header file for future ref.. ]

I have a slow connection and limited hours due to budget (un-employed). I
appreciate your time & effort.

Any comment on the portability of this header?
#include <sys/stat.h> // hmmm, maybe not as portable??

It's portable to most Unix/Linux versions.
 
M

Marcus Kwok

BobR said:
Marcus Kwok wrote in message ...

Thank you, Marcus.
[ I made note of that in my header file for future ref.. ]

I have a slow connection and limited hours due to budget (un-employed). I
appreciate your time & effort.

It wasn't much effort really... since in the original post "determind"
is misspelled :)
Any comment on the portability of this header?
#include <sys/stat.h> // hmmm, maybe not as portable??

AFAIK anything in sys/ is not a standard header in terms of C++, but
maybe it is considered a standard POSIX header.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top