所以我尝试将这样的Matlab代码转换为C++:
ss = 'file.mask'
fp = fopen(ss, 'rb');
sx = fread(fp, 1, 'int32')
sy = fread(fp, 1, 'int32')
mask = zeros(sy, sx);
mm = mask(100, 100:200);
count = zeros(10, 1);
for i = 1:sy
row = fread(fp, sx, 'uchar');
for j = 1:sx
for k = 1:10
if row(j) == k - 1
count(k) = count(k) + 1;
end
end
end
mask(i, :) = row;
end
fclose(fp);
所以我想知道Matlab
fread
的C++模拟是什么吗? 最佳答案
如果您真的想要C++模拟而不是C模拟(甚至是C / C++模拟),那么您应该使用std::ifstream
:
std::ifstream in("file.mask", std::ios_base::in | std::ios_base::binary);
int sx, sy;
in.read(reinterpret_cast<char*>(&sx), sizeof(int));
in.read(reinterpret_cast<char*>(&sy), sizeof(int));
std::vector<unsigned char> row(sx);
for(...)
{
...
in.read(reinterpret_cast<char*>(&row[0]), sx);
...
}