ifish08
asked on
Delete file using remove() in C++
Hi Experts,
I am hoping you can help me... I am trying to delete a file once I am done with it using C++. I am compiling with Visuall C++ 6.0 into a dll. I don't have the luxury of using C# since the code that is calling the DLL is not compatible with it. The last line is where my trouble begins... Here is my code:
#include <string>
#include <sstream>
#include <fstream>
#include <stdio.h>
using namespace std;
//======================== ========== ========== ========== ========== ========== ===
// Return:
// 1 = DECLINED
// 2 = INVALID CARD
// 3 = APPROVAL, with Approval Code parameter set
// 4 = ERROR, with error parameter set
// 5 = File does not exist
// 6 = No approval code exists
//======================== ========== ========== ========== ========== ========== ===
extern "C" _declspec(dllexport)
int viaWarpResponseRead(char* filename, char* approvalCode, char* error)
{
// Clear output parameters
*approvalCode = '\0';
*error = '\0';
// Build filename to read
stringstream filepath; stringstream filetoDelete;
//string path = "C:\\Phone\\viaWarp\\PROCE SS\\";
string path = "C:\\Phone\\";
string fExt = ".OUT";
std::string fName = std::string(filename);// convert char* filename to string
fName = fName += ".OUT";
//string fileToDelete = path +=fName += fExt; //concatenate strings to create filepath with filename
filepath << "C:\\Phone\\viaWarp\\PROCE SS\\" << filename << ".OUT";
//filepath >> fName;
// Attempt to open input file
ifstream inFile(filepath.str().c_st r());
if (!inFile)
{
// viaWarp response file does not exist
strcpy(error, "File not found");
return 5;
}
// Read the whole input file contents
string contents;//((istreambuf_it erator<cha r>(inFile) ), istreambuf_iterator<char>( ));
string line;
while (getline(inFile, line))
{
contents += line + "\n";
}
// Look for keywords within the file
int cnt_verbiage = contents.find("verbiage");
int cnt_approval_code = contents.find("approvalcod e");
int cnt_error = contents.find("error");
if (cnt_verbiage != string::npos)
{
// Look for "DECLINED"
string sstring = contents.substr(cnt_verbia ge + 16, 30);
bool isDeclined = sstring.find("DECLINED") != string::npos;
if (isDeclined)
{
// Declined credit card
strcpy(error, "Declined");
return 1;
}
// Look for "INVALID CARD"
bool isInvalidCard = sstring.find("INVALID CARD") != string::npos;
if (isInvalidCard)
{
// Invalid credit card
strcpy(error, "Invalid card");
return 2;
}
if (cnt_approval_code != string::npos)
{
// Extract the approval code value
sstring = contents.substr(cnt_approv al_code + 20, 30);
int cnt2 = (sstring.find(">", 1) - 1);
string codeValue = sstring.substr(0, cnt2);
// Credit card approved. Send back the approval code
strcpy(approvalCode, codeValue.c_str());
return 3;
}
else
{
// No approval code available
strcpy(error, "No approval code");
return 6;
}
}
if (cnt_error != string::npos)
{
// Extract the error
string sstring = contents.substr(cnt_error + 13, 30);
int cnt2 = (sstring.find(">", 1) - 1);
string serror = sstring.substr(0, cnt2);
// Copy error code to output parameter
strcpy(error, serror.c_str());
}
// error
return 4;
remove("C:\\Phone\\viaWarp \\PROCESS\ \test.OUT" );
}
I am hoping you can help me... I am trying to delete a file once I am done with it using C++. I am compiling with Visuall C++ 6.0 into a dll. I don't have the luxury of using C# since the code that is calling the DLL is not compatible with it. The last line is where my trouble begins... Here is my code:
#include <string>
#include <sstream>
#include <fstream>
#include <stdio.h>
using namespace std;
//========================
// Return:
// 1 = DECLINED
// 2 = INVALID CARD
// 3 = APPROVAL, with Approval Code parameter set
// 4 = ERROR, with error parameter set
// 5 = File does not exist
// 6 = No approval code exists
//========================
extern "C" _declspec(dllexport)
int viaWarpResponseRead(char* filename, char* approvalCode, char* error)
{
// Clear output parameters
*approvalCode = '\0';
*error = '\0';
// Build filename to read
stringstream filepath; stringstream filetoDelete;
//string path = "C:\\Phone\\viaWarp\\PROCE
string path = "C:\\Phone\\";
string fExt = ".OUT";
std::string fName = std::string(filename);// convert char* filename to string
fName = fName += ".OUT";
//string fileToDelete = path +=fName += fExt; //concatenate strings to create filepath with filename
filepath << "C:\\Phone\\viaWarp\\PROCE
//filepath >> fName;
// Attempt to open input file
ifstream inFile(filepath.str().c_st
if (!inFile)
{
// viaWarp response file does not exist
strcpy(error, "File not found");
return 5;
}
// Read the whole input file contents
string contents;//((istreambuf_it
string line;
while (getline(inFile, line))
{
contents += line + "\n";
}
// Look for keywords within the file
int cnt_verbiage = contents.find("verbiage");
int cnt_approval_code = contents.find("approvalcod
int cnt_error = contents.find("error");
if (cnt_verbiage != string::npos)
{
// Look for "DECLINED"
string sstring = contents.substr(cnt_verbia
bool isDeclined = sstring.find("DECLINED") != string::npos;
if (isDeclined)
{
// Declined credit card
strcpy(error, "Declined");
return 1;
}
// Look for "INVALID CARD"
bool isInvalidCard = sstring.find("INVALID CARD") != string::npos;
if (isInvalidCard)
{
// Invalid credit card
strcpy(error, "Invalid card");
return 2;
}
if (cnt_approval_code != string::npos)
{
// Extract the approval code value
sstring = contents.substr(cnt_approv
int cnt2 = (sstring.find(">", 1) - 1);
string codeValue = sstring.substr(0, cnt2);
// Credit card approved. Send back the approval code
strcpy(approvalCode, codeValue.c_str());
return 3;
}
else
{
// No approval code available
strcpy(error, "No approval code");
return 6;
}
}
if (cnt_error != string::npos)
{
// Extract the error
string sstring = contents.substr(cnt_error + 13, 30);
int cnt2 = (sstring.find(">", 1) - 1);
string serror = sstring.substr(0, cnt2);
// Copy error code to output parameter
strcpy(error, serror.c_str());
}
// error
return 4;
remove("C:\\Phone\\viaWarp
}
So, what is the problem?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The problem: FIle does not delete.
Because there is no code deleting it. At least you didn't post this code. In the code I see here - remove with the hardcoded file name is after return.
ASKER
OK - I moved the remove() above the return () for each scenario. The code runs through and brings back the approval code, but still will not delete the file - FYI- i picked a random file to delete. Do I need to close the stream before performing the remove function?
Yes, you need to close the stream before.
As pgnatyuk has said, iIf the file is open, On Windows it will be locked. On Linux it would delete as you expect at least the name would be gone but the file would persist on disk until you closed it).
Also, you should be checking the return value. On a successful call remove will return 0. Otherwise, -1 shall be returned. You can check errno to find out why it failed.
http://www.opengroup.org/onlinepubs/000095399/functions/remove.html
Also, you should be checking the return value. On a successful call remove will return 0. Otherwise, -1 shall be returned. You can check errno to find out why it failed.
http://www.opengroup.org/onlinepubs/000095399/functions/remove.html
ASKER
Thank you experts!!