同一文件中的文本和二进制数据

发布于 2024-11-27 05:35:56 字数 1070 浏览 0 评论 0原文

CString strFile = "c:\\test.txt";

CStdioFile aFile;

UINT nOpenFlags = CFile::modeWrite | CFile::modeCreate | CFile::typeText;

CFileException anError;

if (!aFile.Open(strFile, nOpenFlags, &anError))
{
    return false
}

int nSize = 4*sizeof(double);
double* pData = new double[2];

CString strLine, str;

// Write begin of header
strLine = _T(">>> Begin of header <<<\n");
aFile.WriteString(strLine);

// Retrieve current position of file pointer
int lFilePos = (long) aFile.GetPosition();

// Close file
aFile.Close();

nOpenFlags = CFile::modeWrite | CFile::typeBinary;

if (!aFile.Open(strFile, nOpenFlags, &anError))
{
    return false;
}

for(int i = 0 ; i < 2 ; i++)
{
    pData[i] = i;     
}

// Set position of file pointer behind header
aFile.Seek(lFilePos, CFile::begin);

// Write complex vector
aFile.Write(pData, nSize);

// Write complex vector
aFile.Write(pData, nSize);

// Close file
aFile.Close();

意图创建一个包含文本数据和二进制数据的文件。这段代码是用MFC编写的。我想在 C# 中类似地创建一个文件,其中包含文本数据 a 和二进制数据。请让我知道使用哪个流类来创建这个

CString strFile = "c:\\test.txt";

CStdioFile aFile;

UINT nOpenFlags = CFile::modeWrite | CFile::modeCreate | CFile::typeText;

CFileException anError;

if (!aFile.Open(strFile, nOpenFlags, &anError))
{
    return false
}

int nSize = 4*sizeof(double);
double* pData = new double[2];

CString strLine, str;

// Write begin of header
strLine = _T(">>> Begin of header <<<\n");
aFile.WriteString(strLine);

// Retrieve current position of file pointer
int lFilePos = (long) aFile.GetPosition();

// Close file
aFile.Close();

nOpenFlags = CFile::modeWrite | CFile::typeBinary;

if (!aFile.Open(strFile, nOpenFlags, &anError))
{
    return false;
}

for(int i = 0 ; i < 2 ; i++)
{
    pData[i] = i;     
}

// Set position of file pointer behind header
aFile.Seek(lFilePos, CFile::begin);

// Write complex vector
aFile.Write(pData, nSize);

// Write complex vector
aFile.Write(pData, nSize);

// Close file
aFile.Close();

Intention to create a file which contains both text data and binary data. This code is written in MFC. I wanted to similarly created a file in C# which contains both text data a and binary data. Please let me know which stream class is used to create this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事灯 2024-12-04 05:35:56

文本可以写成二进制数据 =>只需对整个文件使用二进制模式即可完成。

文本模式所做的唯一事情是在写入时将“\n”转换为“\r\n”,并在读取时将其转换回来。由于该文件部分是二进制的,因此无论如何都无法在常规文本编辑器中进行编辑,因此您不需要进行转换。如果该文件仅适用于您的应用程序,您就不必关心,如果它适用于另一个应用程序,只需手动使用它所需的任何换行符序列即可。

Text can be written as binary data => simply use binary mode for the whole file and be done.

The only thing the text mode does is that it converts "\n" to "\r\n" on write and back on read. Since the file is partly binary and therefore not editable in regular text editor anyway, you don't need that conversion. If the file is just for your application, you just don't care and if it's for another application, just use whatever newline sequence it requires manually.

阳光下的泡沫是彩色的 2024-12-04 05:35:56

至于 C#,可能这篇文章 可以给你你正在寻找的答案。

C# 解决方案还可以指导您为 c 编写类似的内容,但我怀疑您只能靠自己,即您可以使用通用读/写文件。在 C++ 中,您可以使用 运算符>>运算符<<

As to C#, possibly this S.O. article can give you the answer you are looking for.

The C# solution could also guide you in writing something similar for c, but I suspect you are on your own, i.e., you can use generic read/write to file. In C++, you have the possibility of doing formatted input/output from/to streams by using operator>> and operator<<.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文