Discussion:
[Dev-C++] File handle
Noorez Kassam
2004-06-21 01:26:00 UTC
Permalink
My program creates files and the names are what ever the user says. How can
I test to see if a file already exists?

_________________________________________________________________
MSN Premium with Virus Guard and Firewall* from McAfee® Security : 2 months
FREE*
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
Zodiaq
2004-06-21 05:24:04 UTC
Permalink
Hello Noorez and all Dev-C++ users,

Monday, June 21, 2004, 5:24:58 AM, Noorez wrote as follows:

NK> My program creates files and the names are what ever the user says. How can
NK> I test to see if a file already exists?

Duh, sample from Platform SDK:

HANDLE hFile;
char *szFileName;//this is the file name you want to check
...
...

BOOL bFileExists = TRUE;
hFile = CreateFile(szFileName, // open some file
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template

if( hFile == INVALID_HANDLE_VALUE )
{
if( GetLastError( ) == ERROR_FILE_NOT_FOUND )
bFileExists = FALSE;
//in case of other errors file exists but it couldn't be opened for some
reason, you can check for more system error codes
}
else
CloseHandle( hFile );
--
regards, Zodiaq
Noorez Kassam
2004-06-21 21:06:14 UTC
Permalink
I am having trouble doing file handling.

when opening the file i can use all the opening techinques except
ios::nocreate and ios::noreplace.
All the other ones work fine but when i try to use those i get a compile
error.

_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN Premium
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
Jesse Hallam
2004-06-21 22:32:02 UTC
Permalink
As per,

http://www.flipcode.com/cgi-bin/msg.cgi?showThread=00003660&forum=general&id=-1

ios::nocreate is only implemented in the old C++ runtime library --
<iostream.h>, rather than <iostream>.

There's a detailed list of implementation differences here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_differences_in_iostream_implementation.asp


(I presume this is the same situation with ios::noreplace)

Cheerio!

On Mon, 21 Jun 2004 17:05:52 -0600, Noorez Kassam
Post by Noorez Kassam
I am having trouble doing file handling.
when opening the file i can use all the opening techinques except
ios::nocreate and ios::noreplace.
All the other ones work fine but when i try to use those i get a compile
error.
_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN Premium
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
digital self defense, top technical experts, no vendor pitches,
unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Dev-cpp-users mailing list
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
--
Jesse Hallam
University of Waterloo Freshman
Jesse Hallam
2004-06-22 14:50:08 UTC
Permalink
As per, http://www.devx.com/tips/Tip/14544

The following example imitates the ios::nocreate flag. First, it
attempts to open a file for read. If the file doesn't exist, no new
file is created. If the file exists, the program closes it and reopens
it in write mode:

fstream fs("fname", ios_base::in);// attempt open for read
if (!fs)
{
// file doesn't exist; don't create a new one
}
else //ok, file exists. close and reopen in write mode
{
fs.close();
fs.open("fname", ios_base::out); // reopen for write
}


You can just do the opposite for ios::noreplace:


fstream fs("fname", ios_base::in);// attempt open for read
if (!fs)
{
// file doesn't exist; create a new one
fs.open("fname", ios_base::out);
}
else //ok, file exists; close and reopen in write mode
{
fs.close()
fs.open("fname", ios_base::out); // reopen for write
}


On Tue, 22 Jun 2004 09:53:49 -0600, Noorez Kassam
I tried including the old iostream.h file but it still didn't work. Can you
give me a sample code for this?
Subject: Re: [Dev-C++] File handle
Date: Mon, 21 Jun 2004 20:31:06 -0400
As per,
http://www.flipcode.com/cgi-bin/msg.cgi?showThread=00003660&forum=general&id=-1
ios::nocreate is only implemented in the old C++ runtime library --
<iostream.h>, rather than <iostream>.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_differences_in_iostream_implementation.asp
(I presume this is the same situation with ios::noreplace)
Cheerio!
On Mon, 21 Jun 2004 17:05:52 -0600, Noorez Kassam
Post by Noorez Kassam
I am having trouble doing file handling.
when opening the file i can use all the opening techinques except
ios::nocreate and ios::noreplace.
All the other ones work fine but when i try to use those i get a compile
error.
_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN Premium
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
Post by Noorez Kassam
-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
digital self defense, top technical experts, no vendor pitches,
unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Dev-cpp-users mailing list
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
--
Jesse Hallam
University of Waterloo Freshman
_________________________________________________________________
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
--
Jesse Hallam
University of Waterloo Freshman
Continue reading on narkive:
Loading...