Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's my snippet:

CFile cfile_object;
char szSampleText[50];
UINT lBytesRead = cfile_object.Read(szSampleText, 50);


I'm reading text file using above code but it has garbage value at end of file.

Here "szSampleText" has garbage value while debugging. How to use CFile::Read() without
returning garbage value. Pls help!

What I have tried:

CFile cfile_object;
char szSampleText[50];
UINT lBytesRead = cfile_object.Read(szSampleText, 50);
Posted
Updated 21-Jan-19 2:31am
Comments
Richard MacCutchan 21-Jan-19 8:06am    
You need to check the return value (lBytesRead) to see how much new data was actually received.
[no name] 21-Jan-19 8:12am    
+5
rmds 21-Jan-19 8:20am    
I don't get it.
rmds 21-Jan-19 8:19am    
This is what I get in lBytesRead in debugging
"lBytesRead 44 unsigned int"
rmds 21-Jan-19 8:19am    
This is what I get in lBytesRead in debugging
"lBytesRead 44 unsigned int"

1 solution

Quote:
This is what I get in lBytesRead in debugging
"lBytesRead 44 unsigned int"
So what has been read is 44 bytes out of a possible max of 50. So 44 bytes will be transferred to yoru buffer, and the remaining elements will not be affected in any way; their previous content - random according to your snippet - will remain as they were. This may give an impression of "random data at the end" and no terminating null '\0' will be appended. So if you print the array as a string assuming it is a valid null-terminated string, you will get random rubbish after your actual data.

In addition, Read is really intended for binary data, it takes no account of the file content. So check your file with a hex editor and make sure that it contains text, and only text: it if contains binary data, then you will get binary data, which may not be immediately printable! Use the debugger to compare the file content shown in the hex editor with the actual content of your array.
 
Share this answer
 
Comments
rmds 21-Jan-19 12:23pm    
Thanks! you're eye opener
OriginalGriff 21-Jan-19 12:34pm    
You're welcome!
rmds 21-Jan-19 12:33pm    
I also have another doubt. I'm sending file from server to client using CSocket. I'm calling Socketid.Receive() in client end. Where the text file will be received? Here's the snippet.
"Server-end"
ULONGLONG dwLength = sourceFile.GetLength();
char* buffer = new char[(int)dwLength + 1];
buffer[dwLength] = 0;
UINT nActual = sourceFile.Read(buffer, dwLength);
if(DataBytes == SOCKET_ERROR) {
AfxMessageBox(L"Error while sending");
return;
}
nActual = nActual - DataBytes;
}
"Client-End"

char *pBuf =new char [1025];
CString strData;
int iLen;
iLen=m_SocketClient.Receive(pBuf,1024);

After I run this code, I'm not finding any text file in Client Folder.
OriginalGriff 21-Jan-19 12:35pm    
Well no ... it won't save it to disk unless you explicitly tell it to!
rmds 21-Jan-19 12:46pm    
Is there a way to save the file to drive... instead of CFile::ModeCreate and then write into file? I dont wanna create file and then write into it. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900