将文本文件的内容复制到缓冲区(win32api) [英] Copying contents of a text file into a buffer (win32api)

查看:250
本文介绍了将文本文件的内容复制到缓冲区(win32api)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本文件中有一些数据。现在我想将这些数据复制到字符缓冲区,以便我可以在udp套接字上发送它。如何将文本文件中的数据复制到缓冲区?我为此目的试过了fread,但它也复制了一些冗余数据,虽然我只指定了要读取的文件大小的字节数,但仍在读取一些冗余数据。下面是我正在尝试的代码片段:



  char  file_buffer [  1000 ]; 
fpSend = fopen( sendclInformation.txt w +);
WriteFile(sendFile, 文件中的数据,strlen( 文件中的数据),& dwWritten, 0 );
fseek(fpSend, 0 ,SEEK_END);
size_t file_size = ftell(fpSend); // 此处计算的大小为12,因此fread必须仅显示12个字节,但它显示附加到实际数据的大量冗余数据。
fseek(fpSend, 0 ,SEEK_SET);
if (file_size> 0) // 如果文件大小> 0
{
int bytes_read = 0 ;
if ((bytes_read = fread(file_buffer,file_size, 1 ,fpSend))< = 0
{
// 无法将文件复制到缓冲区,
}
else
{
MessageBox( NULL,file_buffer, 在缓冲区中复制的文件,MB_ICONEXCLAMATION | MB_OK);
}
}

解决方案

引用:

仍在读取一些冗余数据

它没有读取冗余数据。在 bytes_read 限制之后,只需缓冲区包含未初始化的数据(或以前初始化的数据):在每次读取操作后,只有来自 buffer [0] buffer [bytes_read-1] 有效。


首先将缓冲区初始化为0即char file_buffer [ 1000] = {0}

接下来,不要使用上面的静态缓冲区大小来存储文件数据,而是可以计算文件的大小,分配那么多内存来缓冲然后复制内容。


根据缓冲区的大小,您可能需要查看CreateFileMapping和MapViewOfFile,这将为您提供一个很好的数组,您可以直接使用,然后刷新。



我发现你的混合stdio和Winapi有点令人困惑和丑陋,我建议坚持使用#defines或根据你的编译环境使用#defines。 />


但是,在Windows上,ReadFile和WriteFile最终都会因为它们的工作原理而在文件的映射视图上运行,真正的问题是方便性和性能。

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:

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);
 }
}

解决方案

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.


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.


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.


这篇关于将文本文件的内容复制到缓冲区(win32api)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆