我正在尝试使用fopen上传文件(.pak文件),将其读取并使其等于blockStorage
。
我现在正在做的事情行不通,我认为是因为pfile.data()甚至不存在(它是我尝试并停止做的事情遗留下来的,因为我做错了),而且它还是一个指针所以没有道理
我想知道如何使文件等于blockStorage,因此可以通过调用BlockDecompressImageDXT5
的函数来运行它。我没有包含该函数,因为它相当长,而且我认为它实际上不适用于我当前的问题。
我对c++相当了解,我一直在努力解决这个问题,弄清楚它的不同版本,直到最终找到正确的路径并且知道如何进行处理。
int main() {
FILE* pFile;
pFile = fopen("C:\Users\erast\Desktop\encrypt.pak", "r");
unsigned long x = 0;
unsigned long y = 0;
unsigned long width = 512;
unsigned long height = 512;
const unsigned char* blockStorage = reinterpret_cast<const unsigned char*>(pFile.data());
unsigned long* image = new unsigned long[width * height];
BlockDecompressImageDXT5(width, height, blockStorage, image);
我一直在阅读文档并询问了一段时间,但对于如何使上传的文件等于blockStorage
仍然很困惑。我已经在这个项目上工作了大约一个月,而用我的基本C++知识却很难。我一直在学习,但是学习需要时间。
最佳答案
我相信我已经找到一种方法来完成这项工作。我已经停止使用fopen了,因为有人告诉我它是C语言,并帮助我转向了更多的C++方法。
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "func.h"
using namespace::std;
const std::string readFile(const std::string&& fileName) noexcept {
// taking file as inputstream
std::ifstream file(fileName);
if (file) {
// reading data
std::ostringstream ss;
ss << file.rdbuf();
return ss.str();
}
else {
// file wasn't found
return "Missing file!";
}
};
int main() {
// cout << fileReader("./Client/index.xhtml");
// Variables for file upload and read
// upload my pak file
const std::string pFile = readFile("encrypt.pak");
// obtain file size (*in characters*):
const size_t lSize = pFile.size();
// copy the file into the buffer:
// My variables for my functions
const unsigned long x = 0,
y = 0,
width = 512,
height = 512;
const char* const blockStorage = pFile.c_str();
unsigned long* const image = new unsigned long[width * height];
using ustring = unsigned char*;
// The function I'm calling
BlockDecompressImageDXT5(
width,
height,
reinterpret_cast<const unsigned char* const>(blockStorage),
image
);
}
我希望这对以后尝试将.pak文件作为const unsigned char上载的人有所帮助。
关于c++ - 使用fopen打开.pak文件并将该文件应用于const unsigned char *(C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62629016/