Connecting Tech Pros Worldwide Forums | Help | Site Map

operating on files in C++ need help

Rex_chaos
Guest
 
Posts: n/a
#1: Jul 19 '05
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

WW
Guest
 
Posts: n/a
#2: Jul 19 '05

re: operating on files in C++ need help


Rex_chaos wrote:[color=blue]
> 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[/color]

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...
[color=blue]
> 2. delete all the files under a given directory[/color]

No portable way. Again POSIX or Boost can help:
http://www.boost.org/libs/filesystem/doc/index.htm
[color=blue]
> 3. rename[/color]

std::rename, as in C.

--
WW aka Attila


Jonathan Mcdougall
Guest
 
Posts: n/a
#3: Jul 19 '05

re: operating on files in C++ need help


> I am a c programmer and new to C++. In C, I am familiar with some[color=blue]
> 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[/color]

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

if ( ! check )
std::cout << "file does not exist";
[color=blue]
> 2. delete all the files under a given directory[/color]

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

std::remove("filename");

from <cstdio> iirc. You'll have to use some implementation-specific
functions to get more informations about the file system.
[color=blue]
> 3. rename[/color]

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


Jonathan


WW
Guest
 
Posts: n/a
#4: Jul 19 '05

re: operating on files in C++ need help


Jonathan Mcdougall wrote:[color=blue][color=green]
>> 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[/color]
>
> std::ifstream check("file.ext");
>
> if ( ! check )
> std::cout << "file does not exist";[/color]

Which can fail for a thousand other reasons. No rights, temporary network
problem, file is opened exclusively by someone else etc. etc. etc.
[color=blue][color=green]
>> 3. rename[/color]
>
> No, except by copying under a new name and then remove the old
> one.[/color]

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

--
WW aka Attila


Rob Williscroft
Guest
 
Posts: n/a
#5: Jul 19 '05

re: operating on files in C++ need help


Jonathan Mcdougall wrote in news:Y0Efb.58362$1M6.1109254
@wagner.videotron.net:
[color=blue][color=green]
>> 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[/color]
>
> std::ifstream check("file.ext");
>
> if ( ! check )
> std::cout << "file does not exist";[/color]

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.
[color=blue]
>[color=green]
>> 2. delete all the files under a given directory[/color]
>
> No, unless you have the file list. Remove a single file with
>
> std::remove("filename");
>
> from <cstdio> iirc. You'll have to use some implementation-specific
> functions to get more informations about the file system.
>[color=green]
>> 3. rename[/color]
>
> No, except by copying under a new name and then remove the old
> one.
>
>[/color]

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

http://www.dinkumware.com/manuals/re...io.html#rename


Rob.
--
http://www.victim-prime.dsl.pipex.com/
Kevin Goodsell
Guest
 
Posts: n/a
#6: Jul 19 '05

re: operating on files in C++ need help


WW wrote:
[color=blue]
>
> How about the std::rename function in cstdio.h?
>[/color]

<cstdio>, no '.h'.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

WW
Guest
 
Posts: n/a
#7: Jul 19 '05

re: operating on files in C++ need help


Kevin Goodsell wrote:[color=blue]
> WW wrote:
>[color=green]
>>
>> How about the std::rename function in cstdio.h?
>>[/color]
>
> <cstdio>, no '.h'.[/color]

Well said. :-)

--
WW aka Attila


Jonathan Mcdougall
Guest
 
Posts: n/a
#8: Jul 19 '05

re: operating on files in C++ need help


> >> 3. rename[color=blue][color=green]
> >
> > No, except by copying under a new name and then remove the old
> > one.[/color]
>
> How about the std::rename function in cstdio.h?[/color]

That`s the second time I forget it. Sorry.


Jonathan


Mike Wahler
Guest
 
Posts: n/a
#9: Jul 19 '05

re: operating on files in C++ need help



"Rex_chaos" <rex_chaos@21cn.com> wrote in message
news:f7a7417.0310040943.2efd8b9d@posting.google.co m...[color=blue]
> 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[/color]

Neither C nor C++ has the capability for this.
[color=blue]
> 2. delete all the files under a given directory[/color]

Neither C nor C++ has the capability for this.
[color=blue]
> 3. rename[/color]

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


Mike Wahler
Guest
 
Posts: n/a
#10: Jul 19 '05

re: operating on files in C++ need help



"Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message
news:Y0Efb.58362$1M6.1109254@wagner.videotron.net. ..[color=blue][color=green]
> > 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[/color]
>
> std::ifstream check("file.ext");
>
> if ( ! check )
> std::cout << "file does not exist";[/color]

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

[color=blue]
>[color=green]
> > 2. delete all the files under a given directory[/color]
>
> No, unless you have the file list. Remove a single file with
>
> std::remove("filename");
>
> from <cstdio> iirc. You'll have to use some implementation-specific
> functions to get more informations about the file system.
>[color=green]
> > 3. rename[/color]
>
> No,[/color]

Yes.
[color=blue]
> except by copying under a new name and then remove the old
> one.[/color]

See standard function 'rename()'

-Mike


Jonathan Mcdougall
Guest
 
Posts: n/a
#11: Jul 19 '05

re: operating on files in C++ need help


> > > 1. check if a file or directory exists or not[color=blue][color=green]
> >
> > std::ifstream check("file.ext");
> >
> > if ( ! check )
> > std::cout << "file does not exist";[/color]
>
> That is an invalid conclusion. The only valid conclusion
> is "Cannot open the file".[/color]

Do you have another way to check for the existence of a file which would
be more accurate?
[color=blue][color=green][color=darkred]
> > > 3. rename[/color]
> >
> > No,[/color]
>
> Yes.
>[color=green]
> > except by copying under a new name and then remove the old
> > one.[/color]
>
> See standard function 'rename()'[/color]

I know, Attila already told me. Thanks,


Jonathan


Jonathan Mcdougall
Guest
 
Posts: n/a
#12: Jul 19 '05

re: operating on files in C++ need help


> > > > 1. check if a file or directory exists or not[color=blue][color=green][color=darkred]
> > >
> > > std::ifstream check("file.ext");
> > >
> > > if ( ! check )
> > > std::cout << "file does not exist";[/color]
> >
> > That is an invalid conclusion. The only valid conclusion
> > is "Cannot open the file".[/color]
>
> Do you have another way to check for the existence of a file which would
> be more accurate?
>[/color]

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


Mike Wahler
Guest
 
Posts: n/a
#13: Jul 19 '05

re: operating on files in C++ need help


"Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message
news:GoFfb.60051$1M6.1153235@wagner.videotron.net. ..[color=blue][color=green][color=darkred]
> > > > 1. check if a file or directory exists or not
> > >
> > > std::ifstream check("file.ext");
> > >
> > > if ( ! check )
> > > std::cout << "file does not exist";[/color]
> >
> > That is an invalid conclusion. The only valid conclusion
> > is "Cannot open the file".[/color]
>
> Do you have another way to check for the existence of a file which would
> be more accurate?[/color]

There is no way with standard C++.

-Mike


Kevin Goodsell
Guest
 
Posts: n/a
#14: Jul 19 '05

re: operating on files in C++ need help


Mike Wahler wrote:
[color=blue]
>
> There is no way with standard C++.
>[/color]

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Julián Albo
Guest
 
Posts: n/a
#15: Jul 19 '05

re: operating on files in C++ need help


Kevin Goodsell escribió:
[color=blue]
> 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.[/color]

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

Regards.
Mike Wahler
Guest
 
Posts: n/a
#16: Jul 19 '05

re: operating on files in C++ need help


"Kevin Goodsell" <usenet1.spamfree.fusion@neverbox.com> wrote in message
news:ijGfb.2075$gA1.7@newsread3.news.pas.earthlink .net...[color=blue]
> Mike Wahler wrote:
>[color=green]
> >
> > There is no way with standard C++.
> >[/color]
>
> How about the closest standard approximation? What do you suppose that
> would be?[/color]

"closest" is a subjective term, and also dependent upon
the platform, thus not standard or portable.
[color=blue]
> 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 >[/color]
encapsulate non-portable code sections). Supplying a "default" portable[color=blue]
> implementation that sort of works can be handy also.[/color]

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


Closed Thread


Similar C / C++ bytes