我的工作是创建一个游戏。我想隐藏我所有的.tga文件。我将我所有文件的字符串内容连接到一个单独的文件中,以使其可供玩家使用。我希望我的程序通过从保存的内容创建一个临时的.tga文件来加载图片。
因此,我尝试从原始文件的内容复制.tga文件。更准确地说,我将.tga文件作为文本读取,然后写入。即使Notepad++发现原始文件和新文件相同,新文件也不能作为.tga文件打开。Windows检测具有1字节偏移量的文件的大小。
你能解释一下我哪里做错了吗?或者给我一个更好的隐藏文件的方法。
问候
发布于 2019-01-28 03:14:19
更准确地说,我将.tga文件作为text读取并写入它
这可能是您的问题所在:您必须以二进制文件的形式读取和写入.tga文件。否则,字节序列0x0D 0x0A (CR LF,Windows行结束)的任何出现都可以用单个0x0A (LF,Unix行结束)替换,反之亦然,或者可以剥离或附加0x1A (DOS文件结束)。根据您使用的代码,您还可能最终剥离任何0x00 (NUL)字节。
发布于 2019-01-31 06:01:08
我试图用我的程序(c++)将.tga文件作为二进制文件进行读/写,但生成的文件仍然损坏。代码如下。
std::string name = "my_picture.tga";
std::ifstream FileIn(name, std::ios_base::binary);
std::vector<char> listChar;
bool stopp = false;
if (FileIn) {
while (!(stopp))
{
char xin;
FileIn.read(reinterpret_cast<char*>(&xin), sizeof(char));
listChar.push_back(xin);
if (FileIn.eof()) stopp = true;
}
FileIn.close();
}
std::ofstream FileOut(".\\test.tga", std::ios_base::binary);
bool isCarierReturn = false;
for (char xout : listChar) {
isCarierReturn = xout == '\r';
if (!isCarierReturn) FileOut.write(reinterpret_cast<const char*>(&xout), sizeof(char));
}
FileOut.close();我在十六进制读取器上比较了原始文件和新文件,文件实际上是不同的。
原始文件和新文件之间的区别在于结束行的不匹配,而不是原始文件上只有0x0A ('\n'),新文件的字节序列是0x0D 0x0A ('\r‘和'\n')。在其他一些图片上,生成的文件是不完整的,分隔符总是在0x1A值之前(如@Christoph Lipka所说)。
我设法通过测试字符是否为载波返回来写入正确的序列,在这种情况下不写入字符,仅跳过字节0x0D,如下所示:
std::ofstream FileOut(".\\test.tga", std::ios_base::binary);
bool isCarrierReturn = false;
char xout_p1 = '\0';
if (listChar.size() >= 1) xout_p1 = listChar.at(0);
for (unsigned i(0); i < listChar.size(); i++) {
char xout = xout_p1;
if (i < listChar.size() - 1) xout_p1 = listChar.at(i + 1);
else xout_p1 = '\0';
isCarrierReturn = xout == '\r' && xout_p1 == '\n';
if (!isCarrierReturn) FileOut.write(reinterpret_cast<const char*>(&xout), sizeof(char));
}
FileOut.close();通过将文件读取为二进制文件,解决了文件读取不完整的问题。
它起作用了。
https://stackoverflow.com/questions/54391339
复制相似问题