1

I need to read a stream out of a binary file from a function. I'd like to call it by reference of that char * to have a pointer to the start of the stream in the end. However, Every attempt of mine, did either not change the pointer, or caused an Memory Access Violation.

I call the method from another function.

Here the calling function:

APP_ERROR EncryptionHandler::encryptFile(char *file)
{
char *i_Stream = ""; // I get a compiler error if I dont initialize this
if(this->readFileStream("picture.png", i_Stream) != OPERATION_SUCCESSFUL) // Call the function and return a custom error, if the function does so
return ERROR_FILE_READING;
}

Here the function to read the file

APP_ERROR EncryptionHandler::readFileStream(char *fileName, char *Stream)
{
char *fileStream;
FILE *file = fopen(fileName, "rb");
// Some logic to get the file size
fileStream = new char[maxFileSize];
fread(fileStream, 1, maxFileSize, file); // Fill the stream with the fread function
fclose(file);
Stream = fileStream; // Set the given Pointer to my fileStream pointer
return OPERATION_SUCCESSFUL;
}

How ever in the calling function, the Variable i_Stream has never changed. It's still pointing to "" which causes problems in my program, later I don't get this, as I set the given pointer = my fileStream pointer

However following methods didn't work aswell:

this->readFileStream("picture.png", char &i_Stream);
i_Error EncryptionHandler::readFileStream(char *fileName, char **Stream)


this->readFileStream("picture.png", char *i_Stream);
i_Error EncryptionHandler::readFileStream(char *fileName, char &Stream)

Procedures like memcpy aren't the right thing I think, as I've already got this pointer to my Stream. And they caused Access Violation Errors aswell...

There must be an easy way to give that stream pointer from within my file reading function to my calling methods variable...

I cannot use the return value of the function, because I am using my own Error system, as you can see...

It is also binary Data and the use of any strings is not welcome.

So what is the proper way of calling a char array by reference?

2 Answers 2

3

You should declare EncryptionHandler::readFileStream() as

APP_ERROR EncryptionHandler::readFileStream(char *fileName, char *&Stream)

Note the type of parameter Stream. With reference of pointer, you can pass the change of Stream inside readFileStream() back to caller.

Without the reference, the pointer is simply copied into readFileStream().

Sign up to request clarification or add additional context in comments.

1 Comment

Oh thank you a lot, you saved my night! To understand this. This is a pointer to a reference now? Or a reference to a pointer? *& looks a bit confusing to me
0

In addition to what @timrau said, I see a memory allocation problem. I don't see any code that allocates memory to hold the data that you are reading from the file. Something like the code below should work.

PP_ERROR EncryptionHandler::readFileStream(char *fileName, char*& Stream)
{
   char *fileStream;
   FILE *file = fopen(fileName, "rb");
   // Some logic to get the file size

   // Allocate memory for the data.
   fileStream = new char[maxFileSize];

   fread(fileStream, 1, maxFileSize, file); // Fill the stream with the fread function
   fclose(file);
   Stream = fileStream; // Set the given Pointer to my fileStream pointer
   return OPERATION_SUCCESSFUL;
}

You can simplify the function a little bit by just using Stream.

PP_ERROR EncryptionHandler::readFileStream(char *fileName, char*& Stream)
{
   FILE *file = fopen(fileName, "rb");
   // Some logic to get the file size

   // Allocate memory for the data.
   Stream = new char[maxFileSize];

   fread(Stream, 1, maxFileSize, file); // Fill the stream with the fread function
   fclose(file);
   return OPERATION_SUCCESSFUL;
}

1 Comment

Yeah sure. I have fileStream = new char[maxFileSize] in my file reading method to alloc the memory ;) Thx for the tip though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.