Computing.Net > Forums > Programming > C++ how to check

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ how to check

Reply to Message Icon

Name: Mel
Date: May 21, 2002 at 14:32:22 Pacific
Comment:

Does anyone know how to check if a file already exists in a C++ script? I know it will be really simple, but I cannot figure it out! Thanks for your help.



Sponsored Link
Ads by Google

Response Number 1
Name: berdon
Date: May 21, 2002 at 15:17:50 Pacific
Reply:

Acutally it is pretty easy, though the function is not made specifically to do it. Here is an example.

#include

{
FILE * pFile;
pFile = fopen ("myfile.txt","r");
if (pFile==NULL)
{
printf("File does not exist\n");
}
return 0;
}


0

Response Number 2
Name: Don Arnett
Date: May 21, 2002 at 15:22:27 Pacific
Reply:

Please excuse me while I mention that C++ does not have scripts, it has programs, files, modules, and/or functions. C++ files have to be compiled and linked to create an executable file that can be executed.

A script is an ascii text file of commands that you execute by executing the text file.


0

Response Number 3
Name: Pixy
Date: May 21, 2002 at 17:32:59 Pacific
Reply:

Here is another way of checking if the file exists, the one I use:
"
#include
#include

ifstream in;

in.open("thefile.txt", ios::nocreate);

if (in.fail())
{
cout <<"File does not exist!";
exit (-1);//to exit program if needed
}

"

This is the way I do it~~

Hope this helps, Pixy


0

Response Number 4
Name: Pixy
Date: May 21, 2002 at 17:35:06 Pacific
Reply:

Sorry, the 2 "#include" dont show properly, they are stdlib.h and fstream.h

Pixy


0

Response Number 5
Name: Mel
Date: May 22, 2002 at 19:52:47 Pacific
Reply:

Thanks everyone! That is exactly what I needed. And sorry about the "script" comment, but everyone knew what I was talking about...so who cares!


0

Related Posts

See More



Response Number 6
Name: Ralph Marczynski
Date: July 28, 2002 at 00:28:56 Pacific
Reply:

If we all started to call dogs cats and cats dogs while pointing at them, everyone would know what we're talking about....but it just ain't right. The difference is HUGE.



0

Sponsored Link
Ads by Google
Reply to Message Icon

packet sniffer C++ Mania: Links fixed



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ how to check

how to check batch file www.computing.net/answers/programming/how-to-check-batch-file/2856.html

how to programm in C++ (for null le www.computing.net/answers/programming/how-to-programm-in-c-for-null-le/12576.html

C++ How to save calculated matrices www.computing.net/answers/programming/c-how-to-save-calculated-matrices/13652.html