-3

I creating a program that takes a file and ecrypts it, but now i'am with a problem opening the file to read, the fopen() always return 0.

void run(){
    char buffer[260] = { '\0' };
    GetWindowTextA(Path,buffer,260);
    encryptFile(buffer, "C:\\Users\\DownD\\Desktop\\Some.dat");
}

I think the problem is somewhere on this function run(), because when replace the buffer array with some string for example, "C:\\Somefile.exe" replacing the function encryptFile() for: encryptFile("C:\\Somefile.exe", "C:\\Users\\DownD\\Desktop\\Some.dat");.It reads the file nice and clean.

Here it is parts of the rest of the project.

int CCrypter::encryptFile(char* filePath, LPCSTR outFile)
{
    unsigned char* data = NULL;
int cypherSize;

int fSize = readFile(data, filePath);
if (!fSize)
    return 2;

unsigned char *ciphertext = new unsigned char[fSize];

cypherSize = encrypt(data, fSize, ciphertext);
if (!cypherSize)
    return 3;

if (!Create_File(ciphertext, cypherSize, outFile))
    return 4;
return 1;
}


int CCrypter::readFile(unsigned char *&buffer, const char* path)
{
    int lenght = 0;
    OutputDebugString(path);
    FILE* input = fopen(path, "rb");
    if (!input)          // Input is always 0
        return 0;
    fseek(input, 0, SEEK_END);
    lenght = ftell(input);
    buffer = new unsigned char[lenght];
    printf("%d", buffer);
    ZeroMemory(buffer, lenght);
    rewind(input);
    if (!fread(buffer, 1, lenght, input))
        return 0;
    fclose(input);
    return lenght;
}

Just to clarify, i'm using Multi-Byte Character Set

Miguel
  • 49
  • 1
  • 7
  • 3
    Learn to use a debugger. And provide a [mcve]. You'd know all of this already, had you taken the [tour] and read [ask]. – IInspectable Jun 28 '17 at 14:55
  • 1
    `CCrypter::readFile()` does not close the file if `fread()` returns 0. It needs to always close the file if `fopen()` succeeds, regardless of whether `fread()` succeeds or fails. Testing the return value of `fread()` for 0 is not the correct to detect a reading error. It should be using `if (fread(buffer, 1, lenght, input) != lenght)`, or using `ferror()`. But since the code is clearly using C++, why use `fopen()` at all? It should use `std::ifstream` instead. – Remy Lebeau Jun 28 '17 at 21:30
  • @RemyLebeau I used fopen() because i was having some problems with `std::ifstream`, but the problem is in fopen() and when i print the file path before, it seems fine. I really have no ideia waht is the problem – Miguel Jun 28 '17 at 22:02
  • 1
    @Miguel: did you *verify* that `fclose()` is actually being called before your failing `fopen()` is then called? I don't know what problem you had with `std::ifstream`, but it is very easy to use in this situation, eg: [C++ read the whole file in buffer](https://stackoverflow.com/questions/18816126/) – Remy Lebeau Jun 28 '17 at 22:20
  • 1
    If `fopen` returns 0 (or rather `NULL`) then the file could not be opened for some reason. Inspect [`errno`](http://www.cplusplus.com/reference/cerrno/errno/) to get more information about the reason of the failure. What does `OutputDebugString(path)` display? This should also give you a hint. – Jabberwocky Jun 29 '17 at 11:27
  • @MichaelWalz I inspect the errno, as you said, and the error was "Permission denied". I tryed to compile the program and run as administrator, but that didn't work. I also finded out that i only can open files in ProgramFiles Folder. – Miguel Jun 29 '17 at 18:08
  • @RemyLebeau I didn't understand what you said about `fclose()` being called before `fopen()`, because the error is in `fopen()` returning 0, so i think calling `fclose()` would only result in an error. But i am gonna try your sugestion, using `std::ifstream` – Miguel Jun 29 '17 at 18:13
  • @Miguel my point was, there is a hole in your code logic that *can* lead to `fclose` being skipped after a successful `fopen`, which *may* cause a *subsequent* `fopen` to fail. Using `std::ifstream` would avoid the skipped close. – Remy Lebeau Jun 29 '17 at 19:40
  • @RemyLebeau That was the problem, Thanke you mate – Miguel Jun 29 '17 at 22:13

1 Answers1

1

I solved the issue. The problem was that I had opened the file before and did not close it, that was why I was receiving permission denied.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Miguel
  • 49
  • 1
  • 7