Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have some data in a text file. Now I want to copy this data to a character buffer so that I can send it iver a udp socket. How can I copy the data in a text file to a buffer? I have tried fread for this purpose but it also copies some of the redundant data, though I have specified only the number of bytes equal to the file size to be read, still it is reading some redundant data. Below is code snippet that I am trying:

C++
char file_buffer[1000];
fpSend = fopen("sendclInformation.txt", "w+");
WriteFile(sendFile,"Data in File",strlen("Data in File"),&dwWritten,0);
fseek(fpSend, 0, SEEK_END);
size_t file_size = ftell(fpSend); // The size calculated here is 12 so fread must display only 12 bytes but it is displaying large redundant data appended to actual data.
fseek(fpSend, 0, SEEK_SET);
if(file_size>0)   //if file size>0
{
 int bytes_read=0;
 if((bytes_read=fread(file_buffer, file_size, 1, fpSend))<=0)
 {
  //      "Unable to copy file into buffer",
 }
 else
 {
    MessageBox( NULL,file_buffer,"File copied in Buffer",MB_ICONEXCLAMATION|MB_OK);
 }
}
Posted
Updated 10-Apr-13 2:03am
v2

Quote:
still it is reading some redundant data
It is not reading 'redundant data'. Simply your buffer contains uninitialised data (or previously initialised data) after the bytes_read limit: after each read operation only data from buffer[0] to buffer[bytes_read-1] is valid.
 
Share this answer
 
v2
Comments
ayesha hassan 10-Apr-13 8:29am    
so I should copy only buffer[0] to buffer[byres_read-1] to some other new buffer and then send that new buffer over the socket?
CPallini 10-Apr-13 13:47pm    
No need to copy: send just the first bytes_read bytes of buffer over the socket.
first of all initialize your buffer to 0 i.e char file_buffer[1000] = {0}
next, dont use static buffer size as above to store the file data, instead you can calculate the size of the file allocate that much memory to buffer and then copy the contents.
 
Share this answer
 
Comments
H.Brydon 13-Apr-13 13:16pm    
These are good points of advice for a n00b (+5) but initializing the entire array is overkill that is not generally done in well formed programs (except for instances perhaps related to debugging, high security, ...).

Finding file size with fseek()/ftell() calls is not typically the best way to do it.

Also I would add that an if() statement that contains an assignment is a style that seems to be somewhat popular in C, but I flag as questionable for maintenance purposes. Your if() statement contains an assignment, API call and a comparison. That syntax/style is not required here and is packing way too much into that one line.

[edit]hopefully clarified that I meant that Solution#3 author gave good advice to OP, who is n00b in question[/edit]
shailesh91082 14-Apr-13 5:03am    
Then could you please tell me what is the best way to initialize the array ??
and what would be the best way to get filesize ??
Depending on the size of your buffer you may want to look into CreateFileMapping and MapViewOfFile, which will give you a nice array you can play around with directly and then flush.

I find your mix of stdio and Winapi a little confusing and ugly, I'd recommend sticking to one or using #defines depending on your compile environment.

However, on Windows ReadFile and WriteFile will both end up operating on a mapped view of the file due to how they work, the real question is convenience vs performance.
 
Share this answer
 

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