我试图用命令行参数打开一个文件,并在我的'testdata'文件中读取相反的数字测试数据文件中的数字包含:将数字读入数组并在C中反向打印0
2
20
200
2000
20000
-2
-20
-200
-20000.
这是我写到目前为止的代码。该文件打印出来,显然不是相反的。我假设我只是在某处丢失了一个for循环。我也在考虑,也许我应该使用fscanf
而不是fgets
。任何输入赞赏。
#include <stdio.h>
#define MAX_NUMS 1000
int main(int argc, char *argv[]) {
Int a, n;
char buf[MAX_NUMS];
Int array[MAX_NUMS];
file *pt;
if (argc < 2) {
printf("Usage %s <files..>\n");
}
if ((pt = fopen(argv[1], "r")) == NULL) {
printf("Unable to open %s for reading.\n", argv[1]);
Return 1;
}
while (fgets(buf, MAX_NUMS, pt) != NULL){
printf("%s", buf);
}
for(j = 0; j < MAX_NUMS; j++){
If(fscanf(pt, "%d", &array[a]) != 1);
Break;
For(a = n; a--> 0;){
Printf("%d", array[a]);
}
fclose(pt);
retuern 0;
}
你为什么不读一个数*为数字*在同一时间到一个数组,然后从最高元素循环到最低? –