Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Ummm can anyone help me with C++ tell me how i would check if a file (of name from cin by user) exists? [2] How will i check for null string value? [3] how to delete existing file entred by user.

1-the best way would be to look in your compiler's docs to see if there is a function for this. I don't believe that I/O functions are standardized between compilers. One thing to look for might be 'fstat()'
Another way would be to open the file for reading. If the open succeeds, then the file exists.
FILE *fp;
fp = fopen("filename.txt","r");
if (fp == NULL) {
printf("file doesn't exist\n");
} else {
printf("file exists\n");
fclose(fp);
}2-Depends on what data type:
** char array: **
** you can have only an empty string
char mystr[10];
if (mystr[0] == 0) {
printf("empty string\n");
}** char array pointer: **
** always do the null pointer check first
char *ptr = mystr;
if (ptr == 0) {
printf("null pointer\n");
} else if (ptr[0] == 0) {
printf("empty string\n");
}** string **
** You can really only have an empty string
** you'd just compare to "" or check the length
string str;
if (str.length() == 0) {
printf("empty string\n");
}
3-look to see if there is an 'unlink()' function supported by your compiler. The dirty way would be to use the system command:char cmd[50];
sprintf(cmd, "delete %s", "filename");
system(cmd);
Be sure to come back and let us know if our suggestions helped!

hmm.. i tried out the above method of deletion (which is in C btw) and it didn't work for me. i'm using Dev-C++ so maybe its just me, but here's another way to do it in C++:
Note: in order to use this methd, you need to include fstream: #include <fstream>void del()
{
char file[50];
cout << "\n\nEnter the name of the file to be deleted (including the extension).\n:";
cin >> file;
ofstream del("tempdel.bat", ios::trunc);
del << "@echo off" << endl;
del << "del /f /q " << file << endl;
del.close();
system("tempdel.bat");
system("del tempdel.bat");
}

#include <string>
#include <iostream>
#include <fstream>using namespace std;
int main()
{
ifstream inp;
string filename, command;
char buffer[132];while (1)
{
cout << endl;
cout << "Enter filename to delete: ";
cin.getline(buffer, 132);
filename = buffer;
if (filename.empty()) break; // exit if empty string
inp.open(filename.c_str());
if(inp.good()) // file exists
{
inp.close();
command = "del ";
command += filename.c_str();
cout << "Delete " << filename.c_str() << endl;
system(command.c_str());
}
else
{
cout << "File not found!" << endl;
}
inp.clear(); // clear ios error flags
}
return 0;
}

See the problem is i'm using Turbo C++ its not even closed to ASNI standard C++ language. see i'm forced to do this on Turbo C++ 3.01 and mot codding hardly works.
however tImmaY's suggestion seems to be supported.. i dont know for sure but i'll fill you guys back in.

I modified the program to work with Turbo C++ 3.0. Unfortunately the "system" command does not seem to work with Turbo C++ 3.0 under Windows XP.
I tried the same program with a newer compiler, Borland C++ 5.5. The program works fine with it. Borland C++ 5.5 is a free compiler from Borland.
http://www.borland.com/products/downloads/download_cbuilder.html##include <iostream.h>
#include <fstream.h>
#include <string.h>int main()
{
ifstream inp;
char filename[80], command[80];while (1)
{
cout << endl;
cout << "Enter filename to delete: ";
cin.getline(filename, 132);if (strlen(filename) == 0) break; // exit if empty string
inp.open(filename);
if(inp.good()) // file exists
{
inp.close();
strcpy(command, "del ");
strcat(command, filename);
cout << "Delete " << filename << endl;
system(command);
}
else
{
cout << "File not found!" << endl;
}
inp.clear(); // clear ios error flags
}
return 0;
}

Dev-C++ is also free :)
http://www.bloodshed.net/devcpp.htmlis there a specific reason your using that compiler?

![]() |
![]() |
![]() |

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