【发布时间】:2014-03-01 01:37:37
【问题描述】:
我正在阅读一个包含内容的文本文件:
2, 0, 15, 14, 24, 6, 19, 9, 25, 13, 7, 5, 21, 10, 12, 11, 4, 22, 23, 20, 17, 8, 18, 3, 1, 16
我想将每个单独的元素放入一个 int 数组中,然后打印该数组。我下面的代码给了我一个分段错误,但我无法解决这个问题。任何帮助都会很棒。
FILE *the_cipher_file;
the_cipher_file = fopen("cipher.txt", "r");
int * the_alphabet_array;
int size_of_alphabet;
int the_letter_counter = 0;
while(fscanf(the_cipher_file, "%d", &size_of_alphabet) > 0){
the_alphabet_array[the_letter_counter] = size_of_alphabet;
the_letter_counter++;
printf("%d", size_of_alphabet);
}
【问题讨论】:
-
the_alphabet_array的定义是什么? -
1) 您在哪条线上遇到了段错误? 2)
the_alphabet_array是如何定义的? -
您还需要将逗号扫描为字符。但需要查看更多代码以了解数组大小等。
-
或者他可以显式地将逗号放入格式字符串中。空格也需要跳过。此格式字符串应该可以正常工作:
" %d,".