我已经使上一个问题更容易得到答案并完全理解它。
问题是我想编写一个C ++程序,将普通文本文件转换为二进制文件,然后读取该二进制文件并将其转换为文本文件,以便该文本文件等于第一个文本文件。 我已经为它编写了这个简单的代码。
int main()
{
string name1 = "first", name2 = "sec", name3 = "third";
int j = 0, k = 0;
ifstream ifs(name1.c_str()); // Here I want to read from the ordinary text file (name1).
ifs >> j; // Now j equals to 5 because name1 contains digit 5.
ifs.close();
ofstream ofs(name2.c_str(), ios::binary);
ofs.write(as_bytes(j), sizeof(int)); // Here I want to write that j to name2 file in binary mode.
ofs.close();
ifstream ifs1(name2.c_str(), ios::binary); // Here I want to read from that binary file (name2).
ifs.read(as_bytes(k), sizeof(int)); // Here I hope k becomes 5.
ofstream ofs1(name3.c_str());
ofs1 << k; // Here I want to write that k to name3 file in ordinary text mode.
ifs1.close();
ofs1.close();
// Now I hope both name1 and name2 contain 5.
keep_window_open();
return 0;
}
现在ofs.write(as_bytes(j), sizeof(int));
或ifs.read(as_bytes(k), sizeof(int));
究竟意味着什么?
我的问题是为什么,实际上,文件name1
包含数字5,其大小为1个字节。 name2
包含一些字符/符号,如[]
,其大小为4个字节,name3
包含数字0,其大小为1个字节?
我完全感谢您的回复。
请不要通过提供额外信息来解决问题,只考虑这个问题。我想要理解它。 (我的机器是Windows 7 32位。我的编译器是MVS 2012.如果需要更多信息,请告诉我。)
答案 0 :(得分:3)
write
和read
方法用于二进制文件的i / o操作。
他们有以下原型:
write(memory_block, size);
read(memory_block, size);
write
将size
个字节从memory_block
写入关联文件。
read
从关联文件中读取size
个字节并将其写入memory_block
例如,在您的情况下,
ofs.write(as_bytes(j), sizeof(int));
将j
个数字的字节写入名为name2
的旁路文件。
您可以通过文件here了解有关输入和输出的更多信息。
在二进制i / o模式下,将j
变量的字节写入name2
文件。在此模式下,任何I / O操作都独立于任何格式考虑因素执行。在写入数据后,它不会添加回车。这意味着您无法读取name2
文件并查看其中的五个数字。
原因是因为拼写错误:)
首先看一下
行 ifstream ifs(name1.c_str()); // Here I want to read from the ordinary text file (name1).
ifs >> j; // Now j equals to 5 because name1 contains digit 5.
ifs.close();
然后看看
行 ifstream ifs1(name2.c_str(), ios::binary); // Here I want to read from that binary file (name2).
ifs.read(as_bytes(k), sizeof(int)); // Here I hope k becomes 5.
您正在尝试从已关闭的文件流对象中读取字节。任何具有此类对象的操作都会出错。你可以用这种方式检查出来:
assert(!ifs.read(as_bytes(k), sizeof(int)));
这是可能的,因为read
返回对ifs
的可变引用,而ifs
可以转换为布尔值。
由于本节中的所有内容,变量k
的值保持不变。
您无法从已关闭的文件中读取数据,因此无法更改k
值。因为这个老了
k
的值将写入name3
文件。
#include <assert.h>
#include <fstream>
#include <string>
using namespace std;
typedef char byte;
template<typename T>
byte* as_bytes(T* ptr) {
return reinterpret_cast<byte*>(ptr);
}
int main()
{
string
name1 = "first.txt",
name2 = "second.bin",
name3 = "third.txt";
int j = 0, k = 0;
// Here I want to read from the ordinary text file (name1).
ifstream ifs(name1.c_str());
ifs >> j;
// Now j equals to 5 because name1 contains digit 5.
assert(j == 5);
ifs.close();
ofstream ofs(name2.c_str(), ios::binary);
// Here I want to write that j to name2 file in binary mode.
ofs.write(as_bytes(&j), sizeof(int));
ofs.close();
// Here I want to read from that binary file (name2).
ifstream ifs1(name2.c_str(), ios::binary);
// Here I hope k becomes 5.
ifs1.read(as_bytes(&k), sizeof(int));
ofstream ofs1(name3.c_str());
// Here I want to write that k to name3 file in ordinary text mode.
ofs1 << k;
ifs1.close();
ofs1.close();
}
答案 1 :(得分:-5)
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}