operating on files in C++ need help

R

Rex_chaos

I am a c programmer and new to C++. In C, I am familiar with some
operations on file. Now when shifting from c to c++, using iostream, I
have no idea how to perform the following operations

1. check if a file or directory exists or not
2. delete all the files under a given directory
3. rename
 
W

WW

Rex_chaos said:
I am a c programmer and new to C++. In C, I am familiar with some
operations on file. Now when shifting from c to c++, using iostream, I
have no idea how to perform the following operations

1. check if a file or directory exists or not

Directory: no standard way to do it. Neither in C nor in C++. POSIX
provides something and Boost has a fairly portable library.

Files: again, there is no portable way AFAIK. You can try to open it. If
you can, it is there. If you cannot, it might not be there or you have no
rights or...
2. delete all the files under a given directory

No portable way. Again POSIX or Boost can help:
http://www.boost.org/libs/filesystem/doc/index.htm
3. rename

std::rename, as in C.
 
J

Jonathan Mcdougall

I am a c programmer and new to C++. In C, I am familiar with some
operations on file. Now when shifting from c to c++, using iostream, I
have no idea how to perform the following operations

1. check if a file or directory exists or not

std::ifstream check("file.ext");

if ( ! check )
std::cout << "file does not exist";
2. delete all the files under a given directory

No, unless you have the file list. Remove a single file with

std::remove("filename");

3. rename

No, except by copying under a new name and then remove the old
one.


Jonathan
 
W

WW

Jonathan said:
std::ifstream check("file.ext");

if ( ! check )
std::cout << "file does not exist";

Which can fail for a thousand other reasons. No rights, temporary network
problem, file is opened exclusively by someone else etc. etc. etc.
No, except by copying under a new name and then remove the old
one.

How about the std::rename function in cstdio.h?
 
R

Rob Williscroft

Jonathan Mcdougall wrote in @wagner.videotron.net:
std::ifstream check("file.ext");

if ( ! check )
std::cout << "file does not exist";

All this realy tells you is there was an error when you tried
to open the file for reading, It may be enough to know this but
it isn't a portable "file does not exist".

You could, having failed to open it, then try to create and write
to it, if this succeeds then the file *probably* didn't exist.
No, unless you have the file list. Remove a single file with

std::remove("filename");



No, except by copying under a new name and then remove the old
one.

int std::rename(const char *old, const char *new);

http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=stdio.html#rename


Rob.
 
M

Mike Wahler

Rex_chaos said:
I am a c programmer and new to C++. In C, I am familiar with some
operations on file. Now when shifting from c to c++, using iostream, I
have no idea how to perform the following operations

1. check if a file or directory exists or not

Neither C nor C++ has the capability for this.
2. delete all the files under a given directory

Neither C nor C++ has the capability for this.
3. rename

Both C and C++ have a standard function for this.
Interestingly, its name is 'rename'. (Declared
by standard header <stdio.h> in C, <stdio.h>
or <cstdio> in C++.

-Mike
 
M

Mike Wahler

Jonathan Mcdougall said:
std::ifstream check("file.ext");

if ( ! check )
std::cout << "file does not exist";

That is an invalid conclusion. The only valid conclusion
is "Cannot open the file".

No, unless you have the file list. Remove a single file with

std::remove("filename");



Yes.

except by copying under a new name and then remove the old
one.

See standard function 'rename()'

-Mike
 
J

Jonathan Mcdougall

1. check if a file or directory exists or not
That is an invalid conclusion. The only valid conclusion
is "Cannot open the file".

Do you have another way to check for the existence of a file which would
be more accurate?
See standard function 'rename()'

I know, Attila already told me. Thanks,


Jonathan
 
J

Jonathan Mcdougall

1. check if a file or directory exists or not
Do you have another way to check for the existence of a file which would
be more accurate?

That's not the point, I know. My way does not check for file existence,
it checks for a stream error, whatever it is. I'll go to bed I think.


Jonathan
 
M

Mike Wahler

Jonathan Mcdougall said:
Do you have another way to check for the existence of a file which would
be more accurate?

There is no way with standard C++.

-Mike
 
K

Kevin Goodsell

Mike said:
There is no way with standard C++.

How about the closest standard approximation? What do you suppose that
would be?

One way of dealing with issues like this is to provide an interface for
accomplishing the task, then require that each new port of the code
supply an implementation with an appropriate system-specific solution
(following the idea that you should use portable code when possible, and
encapsulate non-portable code sections). Supplying a "default" portable
implementation that sort of works can be handy also.

-Kevin
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Kevin Goodsell escribió:
One way of dealing with issues like this is to provide an interface for
accomplishing the task, then require that each new port of the code
supply an implementation with an appropriate system-specific solution
(following the idea that you should use portable code when possible, and
encapsulate non-portable code sections). Supplying a "default" portable
implementation that sort of works can be handy also.

Then probably many people use that deafult and obtain wrong results.

Regards.
 
M

Mike Wahler

Kevin Goodsell said:
How about the closest standard approximation? What do you suppose that
would be?

"closest" is a subjective term, and also dependent upon
the platform, thus not standard or portable.
One way of dealing with issues like this is to provide an interface for
accomplishing the task, then require that each new port of the code
supply an implementation with an appropriate system-specific solution
(following the idea that you should use portable code when possible, and >
encapsulate non-portable code sections). Supplying a "default" portable
implementation that sort of works can be handy also.

Yes, separating nonportable stuff is always a good idea.
Other standards such as POSIX are often used for this.

But there is no portable method to use as a "default".

-Mike
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top