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\\PROCESS\\";
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\\PROCESS\\" << filename << ".OUT";
//filepath >> fName;
// Attempt to open input file
ifstream inFile(filepath.str().c_str());
if (!inFile)
{
// viaWarp response file does not exist
strcpy(error, "File not found");
return 5;
}
// Read the whole input file contents
string contents;//((istreambuf_iterator<char>(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("approvalcode");
int cnt_error = contents.find("error");
if (cnt_verbiage != string::npos)
{
// Look for "DECLINED"
string sstring = contents.substr(cnt_verbiage + 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_approval_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");
}