【发布时间】:2019-12-13 12:02:00
【问题描述】:
这是程序:
int siz = 0;
int n = 0;
FILE* picture;
picture = fopen("test.jpg", "r");
fseek(picture, 0, SEEK_END);
siz = ftell(picture);
char Sbuf[siz];
fseek(picture, 0, SEEK_SET); //Going to the beginning of the file
while (!feof(picture)) {
n = fread(Sbuf, sizeof(char), siz, picture);
/* ... do stuff with the buffer ... */
/* memset(Sbuf, 0, sizeof(Sbuf));
}
我需要读取文件大小。
我肯定知道这段代码是在另一个编译器上编译的。
如何正确声明siz以便代码编译?
【问题讨论】:
-
std::vector就是为此而设计的。 -
new和std::vector在这些情况下是你的朋友。 -
我假设您使用 Sbuf 作为包含图像字节的数组,使用
unsigned char而不是char会更好吗? -
@Chipster
new在这种情况下可能不是 OP 的朋友。 -
@L.F.很公平。我的想法是,如果他们真的需要数组类型的东西并且不能满足于
std::vector之类的东西,那么这是一种创建可变长度数组的方法。我完全同意有更好的选择。
标签: c++ visual-studio