我只是想用 3 个命令行参数运行这个程序:2 个整数和 1 个文件名。
我一直在运行我的程序:
a.out 1 2 devices.txt
devices.txt 看起来像这样:
1
我的主要方法如下所示:
int main(int argc, char* argv[]){
int MaxIterations, T, numDevices;
FILE *devices;
printf("Num arguments: %d \n", argc);
if(argc < 3 || argc > 4){
printf("ERROR, need exactly 2 or 3 arguments!\n");
return 1;
} else if (argc == 3){
MaxIterations = argv[1]; // max iterations allowed
T = argv[2]; // time interval
devices = fopen("devices.in", "r");
} else {
MaxIterations = argv[1];
T = argv[1];
devices = fopen(argv[3], "r");
if(devices == NULL){
fprintf(stderr, "CANT OPEN FILE: %s!\n", argv[3]);
exit(1);
}
}
FILE* file = fopen ("devices.txt", "r");
int i = 0;
fscanf(devices, "%d", numDevices);
printf("Number of devices: %d \n", numDevices);
fclose(devices);
return 0;
}
我做错了什么导致我出现段错误?我添加了调试 printf 来确定 seg 错误实际触发的位置,它看起来像它:
fscanf(devices, "%d", numDevices);