1

Im using the win32 function ReadFile:

CHAR lpBuffer[256]; DWORD nBytesRead; DWORD nCharsWritten; ReadFile(hPipeRead,lpBuffer,sizeof(lpBuffer), &nBytesRead,NULL) || !nBytesRead) 

now im catcing the response from stdout in to lpBuffer with this i like to convert it to std string , the problem is when i do simple :

std::string szReturnlpBuffer(lpBuffer); 

the value of the szReturnlpBuffer contains alot of garbege carecthers after the real string: its looks like this the szReturnlpBuffer value :

"Im stdout from the Qt applicationÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ"

what im doing wrong here ?

1
  • If it's a Qt application, why not use QString? Commented May 7, 2011 at 13:26

2 Answers 2

3

You need to specify the size of the string:

std::string szReturnlpBuffer(lpBuffer, nBytesRead); 

because otherwise it reads till it finds a null character, which causes undefined behavior when it gets outside lpBuffer's memory.

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

Comments

1

You need to terminate the string with a null character:

lpBuffer[nBytesread] = '\0'; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.