I'm using remove(inputPath.c_str()); to remove a file after a while loop is completed, but the file remains. I'm using Vista and logged in as an administrator and the file is located in my home folder. Any help would be appreciated. Thanks in advance.

#include <cstdlib>
#include <iostream> 
#include <fstream>
#include <stdio.h>

using namespace std;

int main(int argc, char *argv[]){
    
    cout << "Input Path: ";
    string inputPath = "";
    getline(cin, inputPath);
    ifstream plainText(inputPath.c_str());
    cout << '\n';
    
    cout << "Output Path: ";    
    string outputPath= "";
    getline(cin, outputPath);
    ofstream cipherText(outputPath.c_str());
    cout << '\n';
    
    cout << "Shift Value = ";
    int shiftValue = 0;
    cin >> shiftValue;
    cout << '\n';
    
    while (!plainText.eof()){
    
        string plainLine = "";
        getline(plainText, plainLine);
        
        int lineLength = 0;
        lineLength = plainLine.length();  
        
        for (int i=0; i<=lineLength; i++){
            
            int decimalValue[lineLength];
            char cipherLine[lineLength];
        
            decimalValue[i] = (int)plainLine[i] + shiftValue; 
            cipherLine[i] = (char)decimalValue[i];
            cipherText << cipherLine[i];  
            }   
  
        cipherText << '\n';
        }
  
    remove(inputPath.c_str());
  
    system("PAUSE");
    return EXIT_SUCCESS;
    }

Recommended Answers

All 4 Replies

got it.
before you use remove, you have to use cipher.close() and plainText.close()

Of course it can't be deleted because the program has it open. You have to close the file before attempting to delete it.

[edit]And what ^^^ said too.

Ahh, how could I be so unconscientious? Thank you very much guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.