读取.txt文件输入不起作用 [英] Reading .txt File Input not Working
问题描述
所以我是C的初学者,我试图打开并读取一个文件,将文件的每个元素存储在一个数组中。以下代码似乎应该在实践中起作用,但是当输出给我时
So I am a beginner to C, and I am trying to open and read a file, storing each element of the file in an array. The following code seems like it should work in practice, but when the output gives me
50
2500
无论出于何种原因。如果有人能就我需要解决的问题提供任何帮助,我们将不胜感激
for whatever reason. If anybody could offer any help on what I need to fix, it would be greatly appreciated
...
推荐答案
当你使用fgets()时,你将使用换行符char作为分隔符从文件中提取字符串(字符数组)。 fscanf()更适合从文件中提取内容。
When you use fgets(), you're pulling strings(character arrays) from the file with the newline char as the delimiter. fscanf() would be more appropriate for pulling ints from files.
程序打印50的原因是因为while循环后magicSquareArray [0]的值是'2 ',而不是2,'2'的ascii值是50。
The reason your program prints 50 is because the value of magicSquareArray[0] after the while loop is '2', not 2, and the ascii value of '2' is 50.
这篇关于读取.txt文件输入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!