Computing.Net > Forums > Programming > Simple c++ help

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.

Simple c++ help

Reply to Message Icon

Name: Selix Farel
Date: December 7, 2004 at 23:46:34 Pacific
OS: Windows Me\XP Dual Boot
CPU/Ram: 230 mb
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: December 8, 2004 at 08:09:23 Pacific
Reply:

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!


0

Response Number 2
Name: tImmaY
Date: December 9, 2004 at 01:25:18 Pacific
Reply:

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");
}


0

Response Number 3
Name: egkenny
Date: December 9, 2004 at 18:22:05 Pacific
Reply:

#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;
}


0

Response Number 4
Name: Selix Farel
Date: December 9, 2004 at 19:57:43 Pacific
Reply:

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.


0

Response Number 5
Name: egkenny
Date: December 9, 2004 at 22:14:57 Pacific
Reply:

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;
}


0

Related Posts

See More



Response Number 6
Name: tImmaY
Date: December 10, 2004 at 02:34:36 Pacific
Reply:

Dev-C++ is also free :)
http://www.bloodshed.net/devcpp.html

is there a specific reason your using that compiler?


0

Sponsored Link
Ads by Google
Reply to Message Icon






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: Simple c++ help

Simple C Help!!! www.computing.net/answers/programming/simple-c-help/9795.html

C Program Simple Question (HELP...) www.computing.net/answers/programming/c-program-simple-question-help/10559.html

Binary Search in C. Help Please! www.computing.net/answers/programming/binary-search-in-c-help-please/6847.html