如何在C ++中以Windows窗体形式读取dbc和asc文件 [英] How to read a dbc and asc file in windows form in C++
本文介绍了如何在C ++中以Windows窗体形式读取dbc和asc文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想阅读.asc文件和.dbc文件,任何人都可以建议我如何使用c ++在Windows窗体中阅读它们。
什么我试过了:
i尝试搜索但没有得到正确的信息
i want to read a .asc file and .dbc file, can anyone suggest me how to read them in windows form using c++.
What I have tried:
i tried searching but not getting correct info
推荐答案
你看了它们像任何其他文件一样使用二进制文件,例如使用Windows API函数CreateFile
/ReadFile
或C标准库fopen
/fread
函数。在阅读之前,您必须确定文件大小并分配该大小的缓冲区加1。附加字节用于在读取后附加的尾随NULL
字节,因为两种文件类型都是纯文本文件。
Aftwerwards您可以以char *
字符串的形式访问数据。
请注意,需要以二进制模式打开因为只有这样才能读取数据。否则,非MS文本文件(仅LF终止行)将被转换为CR-LF终止行,使得分配缓冲区太小。
You read them as binary files like any other file using for example the Windows API functionsCreateFile
/ReadFile
or the C standard libraryfopen
/fread
functions. Before reading you have to determine the file size and allocate a buffer of that size plus one. The additional byte is for a trailingNULL
byte to be appended after reading because both file types are plain text files.
Aftwerwards you can access the data aschar*
strings.
Note that opening in binary mode is required because only then the data are read as they are. Otherwise non-MS text files (LF only terminated lines) would be converted to CR - LF terminated lines making the allocarted buffer too small.
Quote:
我可以得到一个示例代码
can i get a example code for that
#include <sys/stat.h>
struct stat st;
FILE *f = fopen("test.asc", "rb");
if (NULL != f)
{
fstat(_fileno(f), &st);
char *lpszFileData = new char[st.st_size + 1];
fread(lpszFileData, 1, st.st_size, f);
fclose(f);
lpszFileData[st.st_size] = 0;
// Use lpszFileData here
// Delete when no longer needed
delete[] lpszFileData;
}
[/ EDIT]
[/EDIT]
这篇关于如何在C ++中以Windows窗体形式读取dbc和asc文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文