我被困住了。我正在尝试创建一个存储 .PGM 文件值的 ascii 文本文件。
我可以很好地读取文件,标题在 ascii 中,并且读取完美。
但问题是,我需要将 16 字节标头之后的所有内容作为 ascii 读取并将其输出为文本文件,例如:
0 235 23 02 255
等等。这不是二进制表示,它看起来类似于:
ÐHHb{{
所以这是我基本读取文件的代码:
void readTxt(char * fileName){
std::ifstream inFile(fileName);
unsigned int * cData = 0;
int rows = 1943;
int cols = 1365;
try{
if(inFile.is_open()){
cData = new unsigned int[rows*cols];
int i = 0;
while(inFile.good()){
if(i > rows*cols -1) break;
inFile >> cData[i]; i++;
}
}
else throw "Could not open file\n";
std::cout <<"done\n";
inFile.close();
}
catch(const char * e){ std::cout << "Err"; }
delete[] cData;
}
但我完全被难住了。我该如何处理这些数据?我不确定,但我认为它存储为带空格的字符,
那么如何将 char[3] = "255" 识别为 int 呢?或者只是 char[0] = "0" 是一个 int。它们有不同的长度。啊,我好困惑。讨厌听起来像个新手,但我不想再浪费时间了!
谢谢