• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

File I/O in C++

Chupa

Member
Aug 28, 2000
155
0
0
I have a question...I'd like to be able to take a file from the a: drive, and check and see if it exists on the c: drive in a specific directory. If it exists, it needs to rename the file to something different, like just adding a '1' to the end or something. Is there an easy way to do this?
Thanks
 

Chupa

Member
Aug 28, 2000
155
0
0
Yeah, that would work for the renaming part. The problem is that I have to check to see if the file exists. Further complicating the problem is that the filename is not know, just that all *.jpg files on the disk need to be moved to c:

Hm, to clarify, let me explain the situation. I'm at home for the holiday and my mother has a digital camera that stores to a 3.5" disk, and needs a quick and easy way to get the images from the disk to the hard drive. The problem is that, although it makes a valiant effort at changing around the names of pictures, I think that every time the battery is changed, it resets, so there are a lot of pictures taken with the same name, on different disks. I'm wanting to use C++ because I can compile it here using DJGPP, otherwise, if I wanted to use Visual Basic or something more along those lines, I would have to wait til I got back to school.
 

CodeJockey

Member
May 1, 2001
177
0
0
I'm not sure what DJGPP is, but if it has the _findfirst() and _findnext() calls available, they would let you iterate through a list of *.JPG files on the A: drive.
Then you could use PrincessGuard's method to check for each file's existence (I would only add the use of the ios::nocreate mode flag during the construction, so that the open will fail if the file does not exist).

If you are masochistic enough to like using the _findfirst() and _findnext() calls, you could also use them to build a list of files on the C: drive and then compare the names to those found on the A: drive. :)

If you used VC++ and MFC, the CWnd:: DlgDirList() method would be the easy way to go...

Depends on how you want to implement things...
 

MustPost

Golden Member
May 30, 2001
1,923
0
0
I would know how to do this in Linux, but it would probobly take me a little while to figure out how to do it in dos.
 

Chupa

Member
Aug 28, 2000
155
0
0


<< I'm not sure what DJGPP is, but if it has the _findfirst() and _findnext() calls available, they would let you iterate through a list of *.JPG files on the A: drive.
Then you could use PrincessGuard's method to check for each file's existence (I would only add the use of the ios::nocreate mode flag during the construction, so that the open will fail if the file does not exist).

If you are masochistic enough to like using the _findfirst() and _findnext() calls, you could also use them to build a list of files on the C: drive and then compare the names to those found on the A: drive. :)

If you used VC++ and MFC, the CWnd:: DlgDirList() method would be the easy way to go...

Depends on how you want to implement things...
>>



DJGPP is a gcc emulator for DOS.
What library are those calls included in?
If all else fails, I'll just wait till I get back and do it in VC++...
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
Actually, ios::nocreate is non-standard and is (should be) no longer included in <fstream>. By default, ifstream open will (should) fail if the file does not exist. If you think about it, it doesn't make a lot of sense to create a new file for reading if it didn't already exist...

The flag is still included in many inplementations though. Welcome to the wonderfully world of C++ where every compiler is non-standard in different ways.

And DJGPP is not an emulator. It's a DOS port of GCC.
 

CodeJockey

Member
May 1, 2001
177
0
0
You're right, it is stupid to create a file when opening for input.

I was quoting from the VC++ MSDN doc. :

nMode
An integer that contains mode bits defined as ios enumerators that can be combined with the bitwise OR ( | ) operator. The nMode parameter must have one of the following values:
ios::in The file is opened for input (default).
ios::nocreate If the file does not already exist, the function fails.
ios::binary Opens the file in binary mode (the default is text mode).
Note that the ios::nocreate flag is necessary if you intend to test for the file?s existence (the usual case).

It's not just a C++ thing, inconsistent standards have existed for as long as I can remember (the C "standard" i/o library is a prime example).