file to array
-
Hello all, i want to be able to read any file in binary mode and put the files content into a char array, but it just won't work except of text files. How can this be done? Thanks in advance
char* Files::ReadFile(const char* file) { ifstream::pos_type size; char* file_array; ifstream file (file, ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); file_array = new char[size]; file.seekg (0, ios::beg); file.read (file_array, size); file.close(); return file_array; } return "Unable to open file"; }
-
Hello all, i want to be able to read any file in binary mode and put the files content into a char array, but it just won't work except of text files. How can this be done? Thanks in advance
char* Files::ReadFile(const char* file) { ifstream::pos_type size; char* file_array; ifstream file (file, ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); file_array = new char[size]; file.seekg (0, ios::beg); file.read (file_array, size); file.close(); return file_array; } return "Unable to open file"; }
nmx_de wrote:
but it just won't work
Please, when you have a problem, explain it clearly. " It does't work" is very unclear, it can be anything. We cannot guess what the problem is just by looking at the screen.
Cédric Moonen Software developer
Charting control -
nmx_de wrote:
but it just won't work
Please, when you have a problem, explain it clearly. " It does't work" is very unclear, it can be anything. We cannot guess what the problem is just by looking at the screen.
Cédric Moonen Software developer
Charting controlSorry for that. Thought it would be clear as i might not be the only one ever who tried to read a file into an array. When I read a text file, I get the content of the file properly. When I read another file (for example pdf), i only get an output like this:
%PDF-1.5 $??
Maybe it's an encoding problem, because the string is follow by some special characters. (Took a look at it in UltraEdit)%PDF-1.5 %âãÏÓ 149 0 obj < Is it possible only to read the binary value of the file? Regards, n.
-
Hello all, i want to be able to read any file in binary mode and put the files content into a char array, but it just won't work except of text files. How can this be done? Thanks in advance
char* Files::ReadFile(const char* file) { ifstream::pos_type size; char* file_array; ifstream file (file, ios::in|ios::binary|ios::ate); if (file.is_open()) { size = file.tellg(); file_array = new char[size]; file.seekg (0, ios::beg); file.read (file_array, size); file.close(); return file_array; } return "Unable to open file"; }
I hope you are getting some junk value... in file_array for non text files.. if so, its obvious... because you are trying to view char value which is not char... But for text files the data are in the form of char
Do your Duty and Don't expect the Result
-
Sorry for that. Thought it would be clear as i might not be the only one ever who tried to read a file into an array. When I read a text file, I get the content of the file properly. When I read another file (for example pdf), i only get an output like this:
%PDF-1.5 $??
Maybe it's an encoding problem, because the string is follow by some special characters. (Took a look at it in UltraEdit)%PDF-1.5 %âãÏÓ 149 0 obj < Is it possible only to read the binary value of the file? Regards, n.
thats right... if you write the file_array as that type of file say 1.pdf then if u open the file then you can get the excat data...
Do your Duty and Don't expect the Result
-
Sorry for that. Thought it would be clear as i might not be the only one ever who tried to read a file into an array. When I read a text file, I get the content of the file properly. When I read another file (for example pdf), i only get an output like this:
%PDF-1.5 $??
Maybe it's an encoding problem, because the string is follow by some special characters. (Took a look at it in UltraEdit)%PDF-1.5 %âãÏÓ 149 0 obj < Is it possible only to read the binary value of the file? Regards, n.
What are you trying to do exactly ? And what is the purpose ? It's normal that if you try to display the contents of a binary file, you will have junk characters. That's simply because the purpose of a binary file is not to be readable (by a human I mean). So, what did you expect ?
Cédric Moonen Software developer
Charting control -
thats right... if you write the file_array as that type of file say 1.pdf then if u open the file then you can get the excat data...
Do your Duty and Don't expect the Result
Wow, seems to be one of my bad hair days. I still missed to provide some information. I don't want to have a pdf file (or any other binary file) as a result. I only want to get the binary representation of the file. And this has to be some kind of string or char array.
-
Wow, seems to be one of my bad hair days. I still missed to provide some information. I don't want to have a pdf file (or any other binary file) as a result. I only want to get the binary representation of the file. And this has to be some kind of string or char array.
nmx_de wrote:
I only want to get the binary representation of the file. And this has to be some kind of string or char array.
No, a binary file contains junk characters and if you try to display it, you will have junk characters on the screen. Try to open the file with notepad and you will see what's inside. And what do you mean by 'binary representation' ?
Cédric Moonen Software developer
Charting control -
Sorry for that. Thought it would be clear as i might not be the only one ever who tried to read a file into an array. When I read a text file, I get the content of the file properly. When I read another file (for example pdf), i only get an output like this:
%PDF-1.5 $??
Maybe it's an encoding problem, because the string is follow by some special characters. (Took a look at it in UltraEdit)%PDF-1.5 %âãÏÓ 149 0 obj < Is it possible only to read the binary value of the file? Regards, n.