Link to home
Create AccountLog in
Avatar of ifish08
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\\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");
}
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

So, what is the problem?
ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of ifish08
ifish08

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.
Avatar of ifish08

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


Avatar of ifish08

ASKER

Thank you experts!!