![](/img/trans.png)
[英]Opencv and c++ Fisherface algorithm always predict wrong even with the exactly same image as input and reconstruct a black image
[英]The output is always the same even if the algorithm works C/C++
我有这段代码可以检查格式化文件中的最大数字集,即使算法有效,得到上述值的 position,output 始终是最后读取的行。 怎么了?
int main() {
int n, north_key, east_key;
char *identity, *time, *eastIdentity, *eastTime, *northIdentity, *northTime;
float latitude, longitude, max_east = MIN, max_north = MIN;
input = fopen("level2-1.in", "r");
fscanf(input, "%d", &n);
for(int i = 0; i <= n; i++) {
char line[MAX];
fgets(line, MAX, input);
identity = strtok(line, ",");
time = strtok(NULL, ",");
char *aux = strtok(NULL, ",");
latitude = std::atof(aux);
aux = strtok(NULL, "\n");
longitude = std::atof(aux);
if(max_north < latitude) {
max_north = latitude;
north_key = i;
northIdentity = identity;
northTime = time;
}
if(max_east < longitude) {
max_east = longitude;
east_key = i;
eastIdentity = identity;
eastTime = time;
}
printf("%d, %d\n", north_key, east_key);
}
printf("%s,%s, %s,%s\n", northIdentity, northTime, eastIdentity, eastTime);
fclose(input);
return 0;
}
输出都是指向line
段的指针,并且line
被每次迭代覆盖。 此外,在打印输出时, line
已超出 scope。 访问无效的 memory 会导致Undefined Behavior 。
必须通过将输出复制到自己的存储来保存输出。
强烈考虑使用std::string
s(可能还有string
s 一直用std::istringstream
代替strtok
)。
笔记:
for(int i = 0; i <= n; i++)
看起来它可能会读到最后。 i <= n
允许i
在总共n+1
次迭代中达到n
(范围为 [0, n])。 你可能想要i < n
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.