ReadFile() Problem
-
goldenrose9 wrote:
Why this extra characters at the end?
Because
cout
will output characters until it reaches a\0
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
i had checked this with file mapping. In file mapping when i use cout to display the hSrc value it gives the correct output. then why it is displaying extra characters when i am using ReadFile method. Is there any problem in my code. please help:confused:
Some Day I Will Prove MySelf :: GOLD
-
i had checked this with file mapping. In file mapping when i use cout to display the hSrc value it gives the correct output. then why it is displaying extra characters when i am using ReadFile method. Is there any problem in my code. please help:confused:
Some Day I Will Prove MySelf :: GOLD
goldenrose9 wrote:
...then why it is displaying extra characters when i am using ReadFile method.
It has nothing to do with
ReadFile()
.goldenrose9 wrote:
Is there any problem in my code.
Yes,
hSrc
is not terminated properly."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Content of a.txt
Hello How are you
WelcomeFunction used to read the File
void ReadBytes(__inout BYTE *pBuffer, DWORD dwSize)
{
//BYTE *pBuffer = new BYTE[dwSize];
DWORD dwRead;if (hFile != INVALID\_HANDLE\_VALUE) ReadFile(hFile,pBuffer,dwSize,&dwRead,NULL);
}
int main()
{
myFile mObj;
BYTE* hSrc = new BYTE[26];
mObj.ReadBytes(hSrc,26);
std::cout<The output of the above function gives.
Hello How are you
Welcomeýýýý««««««««îþWhy this extra characters at the end? please help:confused::confused:
Some Day I Will Prove MySelf :: GOLD
modified on Tuesday, February 1, 2011 12:44 AM
You forgot to terminate the buffer with the null-termination character:
int main()
{
myFile mObj;
BYTE* hSrc = new BYTE[27];
mObj.ReadBytes(hSrc,26);
hSrc[26] = '\0';
std::cout<
Cédric Moonen
Software developer
Charting control [v3.0]
OpenGL game tutorial in C++ -
goldenrose9 wrote:
...then why it is displaying extra characters when i am using ReadFile method.
It has nothing to do with
ReadFile()
.goldenrose9 wrote:
Is there any problem in my code.
Yes,
hSrc
is not terminated properly."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
then how to solve this.
Some Day I Will Prove MySelf :: GOLD
-
then how to solve this.
Some Day I Will Prove MySelf :: GOLD
See here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
You forgot to terminate the buffer with the null-termination character:
int main()
{
myFile mObj;
BYTE* hSrc = new BYTE[27];
mObj.ReadBytes(hSrc,26);
hSrc[26] = '\0';
std::cout<
Cédric Moonen
Software developer
Charting control [v3.0]
OpenGL game tutorial in C++thanx :) :thumbsup:
Some Day I Will Prove MySelf :: GOLD
-
You forgot to terminate the buffer with the null-termination character:
int main()
{
myFile mObj;
BYTE* hSrc = new BYTE[27];
mObj.ReadBytes(hSrc,26);
hSrc[26] = '\0';
std::cout<
Cédric Moonen
Software developer
Charting control [v3.0]
OpenGL game tutorial in C++Cedric Moonen wrote:
BYTE* hSrc = new BYTE[27];
here you have initialized the byte array with +1 of the required value that is 26 and null terminated the byte array at 26. A small doubt. do i have to allocated the byte array +1
Some Day I Will Prove MySelf :: GOLD
-
Cedric Moonen wrote:
BYTE* hSrc = new BYTE[27];
here you have initialized the byte array with +1 of the required value that is 26 and null terminated the byte array at 26. A small doubt. do i have to allocated the byte array +1
Some Day I Will Prove MySelf :: GOLD
Yes, you have to take the termination character into account (it still counts as one char and should be allocated too). Remember that if I allocate an array of 27 characters, the last one is the character at index 26 (since the array is zero-based).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Yes, you have to take the termination character into account (it still counts as one char and should be allocated too). Remember that if I allocate an array of 27 characters, the last one is the character at index 26 (since the array is zero-based).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++:) got it. sir, now the program is working fine. but when i pass a bmp or exe file to this function only few bytes from the starting is read into the buffer. Zip Archive
PK
EXE
MZ
BMP File
BM:
am i missing something again.please help..
Some Day I Will Prove MySelf :: GOLD
-
:) got it. sir, now the program is working fine. but when i pass a bmp or exe file to this function only few bytes from the starting is read into the buffer. Zip Archive
PK
EXE
MZ
BMP File
BM:
am i missing something again.please help..
Some Day I Will Prove MySelf :: GOLD
Are you trying to print those files to the console ? :~ This won't work, since they contain binary information which can perfectly be the character with ASCII code 0 (which will be considered as the end of the string). What are you trying to do here ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++