我在文本文件中有一些数据。现在我想将此数据复制到字符缓冲区,以便我可以通过 udp 套接字发送它。如何将文本文件中的数据复制到缓冲区?我为此尝试了 fread ,但它也复制了一些冗余数据,尽管我只指定了等于要读取的文件大小的字节数,但它仍在读取一些冗余数据。下面是我正在尝试的代码片段:
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);
}
}