473,606 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operating on files in C++ need help

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
Jul 19 '05 #1
15 11767
WW
Rex_chaos wrote:
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.

--
WW aka Attila
Jul 19 '05 #2
> 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("fi lename");

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


No, except by copying under a new name and then remove the old
one.
Jonathan
Jul 19 '05 #3
WW
Jonathan Mcdougall wrote:
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";


Which can fail for a thousand other reasons. No rights, temporary network
problem, file is opened exclusively by someone else etc. etc. etc.
3. rename


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


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

--
WW aka Attila
Jul 19 '05 #4
Jonathan Mcdougall wrote in news:Y0Efb.5836 2$1M6.1109254
@wagner.videotr on.net:
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";


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.
2. delete all the files under a given directory


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

std::remove("fi lename");

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


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


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

http://www.dinkumware.com/manuals/re...io.html#rename
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #5
WW wrote:

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


<cstdio>, no '.h'.

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

Jul 19 '05 #6
WW
Kevin Goodsell wrote:
WW wrote:

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


<cstdio>, no '.h'.


Well said. :-)

--
WW aka Attila
Jul 19 '05 #7
> >> 3. rename

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


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


That`s the second time I forget it. Sorry.
Jonathan
Jul 19 '05 #8

"Rex_chaos" <re*******@21cn .com> wrote in message
news:f7******** *************** **@posting.goog le.com...
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
Jul 19 '05 #9

"Jonathan Mcdougall" <jo************ ***@DELyahoo.ca > wrote in message
news:Y0******** *************@w agner.videotron .net...
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";


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

2. delete all the files under a given directory
No, unless you have the file list. Remove a single file with

std::remove("fi lename");

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


No,


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


See standard function 'rename()'

-Mike
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4355
by: Woojay Jeon | last post by:
OK, I tried a Google search on this Usenet group but couldn't find a solution, so I'm posting my question here (if there's a better archive than the one in Google, please let me know). Does anybody know how to detect the operating system under which the current Python program is running, especially whether it's Windows or Unix? I have a program that needs to search for files in "c:\test" if it's running under Windows, and...
0
15162
by: Mark Depenbrock | last post by:
--Apple-Mail-2--68472726 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed Can not make mysql connection - error log: 030708 08:53:48 mysqld started
5
13270
by: Raj | last post by:
Hi all, Can anyone help me with a script which would delete files or move them to a different folder at some scheduled time..! Please.....!!! Thanks in advance...
10
3226
by: Vavel | last post by:
Hi all! I want to insert the record into the table by using an application program that includes the following statements: EXEC SQL BEGIN DECLARE SECTION; long hvInt_Stor; long hvExt_Stor; EXEC SQL END DECLARE SECTION; hvInt_Stor = MMDB_STORAGE_TYPE_INTERNAL; hvExt_Stor = MMDB_STORAGE_TYPE_EXTERNAL;
1
1765
by: pat | last post by:
Abraxas Software Understanding YOUR GOALS & Using CodeCheck Implementing Corporate Source Code Guidelines C/C++ Source Code GuideLine Automation The goals of CodeCheck are: 1 To create a standard which will enable you to provide the customer with a quality product in a timely manner. 2 To promote standardization of software development among programmers. 3 To act as a learning tool for new programmers. 4 To act as a reference tool for...
1
1029
by: dazza000 | last post by:
Hi I wan't to write a file renaming utility that is integrated with windows. for example: you open a normal folder, select one or multiple files, then hava a vb dialog box come up with new file name bases and extensions , then click a button and see the names change in the windows folder window. I already know how to rename the files, but I am limited to using file browsing within VB. So how do I pick up the path names of selected files...
4
1570
by: lallous | last post by:
Hello If one has two operating systems on two different partitions, say Vista and XP. Is there is a way to install VS2005 on one partition and make it work on both operating systems, without needing to install VS2005 on both OSes (so that the registry is patched correctly) ? Regards,
15
1408
by: Fro | last post by:
Hi, I have a php-script which writes uploaded files into a directory. My php-script gives a specific names to the saved files. I found in the directory a file which has a name which could not be given by the php- script. Could it be that somebody (which is not a user of the operating system) communicate with the operating system (creates files) without the usage of my php-script? Or it is impossible and I have to search for a mistake in...
6
5304
by: pavanip | last post by:
Hi, I have developed one windows application using vb.net. I have setup that application on windows operating system its working fine on windows os. Now I am trying to do that exe run on Mac operating system. Is there any other know the solution how the windows application compatible with Mac operating system. To support vista os I created manifest file and resource script files.Please help me how the windows application compatible with...
0
8440
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8431
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8306
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5966
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5466
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3937
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1557
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.