Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Delete file using remove() in C++

Avatar of ifish08
ifish08 asked on
CEditors IDEs
8 Comments1 Solution1035 ViewsLast Modified:
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");
}
ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyukFlag of Israel image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answers