C语言 从c文件中读取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6039936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reading data from file in c
提问by zetologos
I have a txt file named prob which contains:
我有一个名为 prob 的 txt 文件,其中包含:
6 2 8 3
4 98652
914
143 789
1
527 146
85
1 74 8
7 6 3
Each line has 9 chars and there are 9 lines. Since I cant make a string array in c, im be using a two dimensional array. Careful running the code, infinite loops are common and it prints weird output. Im also curious as to where does it stop taking in the string? until newline? expected result for each "save": 6 2 8 3 or watever the line contained.
每行有 9 个字符,有 9 行。由于我无法在 c 中创建字符串数组,因此我使用的是二维数组。小心运行代码,无限循环很常见,它会打印出奇怪的输出。我也很好奇它在哪里停止接收字符串?直到换行?每次“保存”的预期结果:6 2 8 3 或水包含的行。
#include <stdio.h>
FILE *prob;
main()
{
prob = fopen("prob.txt", "r");
char grid_values[9][9];
char save[9];
int i;
for (i = 0; (fscanf(prob, "%s", save) != EOF); i++)
{
int n;
for (n = 0; n <= 9; n++)
{
grid_values[i][n] = save[n];
printf("%c", grid_values[i][n]);
}
}
fclose(prob);
}
回答by Zan Lynx
This looks like a homework problem, so I will try to give you some good advice.
这看起来像是一个家庭作业问题,所以我会尽力给你一些好的建议。
First, read the description of the fscanf function and the description of the "%s" conversion.
首先,阅读 fscanf 函数的说明和“%s”转换的说明。
Here is a snip from the description I have for "%s":
这是我对“%s”的描述的一个片段:
Matches a sequence of non-white-space characters; the next pointer must be a pointer to a character array that is long enough to hold the input sequence and the terminating null character ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.
匹配一系列非空白字符;next 指针必须是指向字符数组的指针,该数组的长度足以容纳输入序列和自动添加的终止空字符 ('\0')。输入字符串在空白处或最大字段宽度处停止,以先发生者为准。
Here are the two important points:
这里有两个重要的点:
- Each of your input lines contains numbers and whitespace characters. So the function will read a number, reach whitespace, and stop. It will not read 9 characters.
- If it did read 9 characters, you do not have enough room in your array to store the 10 bytesrequired. Note that a "terminating null character" will be added. 9 characters read, plus 1 null, equals 10. This is a common mistake in C programming and it is best to learn now to always account for the terminating null in any C string.
- 每个输入行都包含数字和空白字符。因此该函数将读取一个数字,到达空格,然后停止。它不会读取 9 个字符。
- 如果它确实读取了 9 个字符,则数组中没有足够的空间来存储所需的10 个字节。请注意,将添加“终止空字符”。读取 9 个字符,加上 1 个空值,等于 10。这是 C 编程中的一个常见错误,现在最好学习始终考虑任何 C 字符串中的终止空值。
Now, to fix this to read characters into a two dimensional array: You need to use a different function. Look through your list of C stdio functions.
现在,要解决此问题以将字符读入二维数组:您需要使用不同的函数。查看您的 C stdio 函数列表。
See anything useful sounding?
看到任何有用的声音吗?
If you haven't, I will give you a hint: fread. It will read a fixed number of bytes from the input stream. In your case you could tell it to always read 9 bytes.
如果你还没有,我会给你一个提示:fread。它将从输入流中读取固定数量的字节。在您的情况下,您可以告诉它始终读取 9 个字节。
That would only work if each line is guaranteedto be padded out to 9 characters.
只有保证每一行都填充到 9 个字符时,这才有效。
Another function is fgets. Again, carefully read the function documentation. fgets is another function that appends a terminating null. However! In this case, if you tell fgets a size of 9, fgets will only read 8 characters and it will write the terminating null as the 9th character.
另一个功能是 fgets。再次,仔细阅读函数文档。fgets 是另一个附加终止空值的函数。然而!在这种情况下,如果您告诉 fgets 大小为 9,则 fgets 将仅读取 8 个字符,并将终止 null 作为第 9 个字符写入。
But there is even another way! Back to fscanf!
但还有另一种方式!回到 fscanf!
If you look at the other conversion specifiers, you could use "%9c" to read 9 characters. If you use thisoperation, it will notadd a terminating null to the string.
如果您查看其他转换说明符,您可以使用“%9c”来读取 9 个字符。如果使用此操作,它不会向字符串添加终止空值。
With both fread and fscanf "%9c" if you wanted to use those 9 bytes as a string in other functions such as printf, you would need to make your buffers 10 bytes and after every fread or fscanf function you would need to write save[9] = '\0'.
使用 fread 和 fscanf "%9c" 如果您想在其他函数(如 printf)中使用这 9 个字节作为字符串,则需要将缓冲区设置为 10 个字节,并且在每个 fread 或 fscanf 函数之后,您需要编写save[9] = '\0'.
Always read the documentation carefully. C string functions sometimesdo it one way. But not always.
请务必仔细阅读文档。C 字符串函数有时以一种方式执行此操作。但不总是。
回答by Bobby Stenly
if you use fscanf, it will stop after a space delimiter..
如果您使用fscanf,它将在空格分隔符后停止..
try fgetsto do it.. It will read line by line..
尝试fgets这样做.. 它将逐行读取..
for (i = 0; (fgets(save, sizeof(save), prob) != EOF); i++)
the detail of fgetsusage can be found here:
http://www.cplusplus.com/reference/clibrary/cstdio/fgets/
使用细节fgets可以在这里找到:http:
//www.cplusplus.com/reference/clibrary/cstdio/fgets/
--edited--
——编辑——
here's the second
这是第二个
while(!feof(file))
{
fgets(s, sizeof(s), file); ......
}
I think it'll work well..
我认为它会工作得很好..

