[英]Read from file a number and after that an array in C
How can I read from a file a number and after an array.如何从文件中读取数字和数组之后。 i mean my file looks like that: 3 7 8 9
我的意思是我的文件看起来像这样:3 7 8 9
3 is the number of components, 7, 8 9 the other components of the array, arr[1], arr[2], arr[3]. 3 是组件的数量,7、8 9 是数组的其他组件,arr[1]、arr[2]、arr[3]。
one way to perform the desired functionality is:执行所需功能的一种方法是:
First, open the file for reading:首先,打开文件进行阅读:
FILE *fp = fopen( "filename.txt" );
Then check that the call to fopen()
was successful and handle any error:然后检查对
fopen()
的调用是否成功并处理任何错误:
if( ! fp )
{
perror( "fopen to read filename.txt failed" );
exit( EXIT_FAILURE );
}
Note: perror()
outputs both your error message and the text reason the system thinks the error occurred to stderr
.注意:
perror()
将您的错误消息和系统认为错误发生的文本原因输出到stderr
。 which is where error messages should be output.这就是错误消息应该是 output 的地方。
reserve a variable to hold the count of following values:保留一个变量来保存以下值的计数:
int maxLoops;
then read the first number and use that number as the max iterations of a loop, of course, checking for errors然后读取第一个数字并将该数字用作循环的最大迭代次数,当然,检查错误
if( fscanf( fp, "%d", &maxLoops ) != 1 )
{
fprintf( stderr, "fscanf to read loop count failed\n" );
exit( EXIT_FAILURE );
}
Note: the scanf()
family of functions does not set errno
when some input format specifier
(in this case %d
) fails, so need to output an error message using something like fprinf()
.注意:当某些
input format specifier
(在本例中%d
)失败时, scanf()
系列函数不会设置errno
,因此需要使用fprinf()
类的方法向 output 发送错误消息。
Note: the scanf()
family of functions returns the number of successful input format conversions
(or EOF)注意:
scanf()
系列函数返回成功input format conversions
(或 EOF)的次数
Note: exit()
and EXIT_FAILURE
are exposed via:注意:
exit()
和EXIT_FAILURE
通过以下方式暴露:
#include <stdlib.h>
then, reserve an array for the following entries in the file, using the Variable Array Length feature of C然后,使用 C 的可变数组长度功能为文件中的以下条目保留一个数组
int dataArray[ maxLoops ];
Now, set up the loop that will read the rest of the data现在,设置将读取数据的 rest 的循环
for( int i = 0; i < maxLoops; i++ )
{
for each pass through the loop read another entry into the array, of course, checking for errors对于每次通过循环读取另一个条目到数组中,当然,检查错误
if( fscanf( fp, "%d", &dataArray[i] ) != 1 )
{
fprintf( stderr, "fscanf for data value failed\n" );
exit( EXIT_FAILURE );
}
} // end the loop
then, cleanup before doing anything else:然后,在做任何其他事情之前进行清理:
fclose( fp );
What you do with the data is up to you.您如何处理数据取决于您自己。 You might want to print out each of the data values with a loop, similar to:
您可能希望使用循环打印出每个数据值,类似于:
for( int i = 0; i < maxLoops; i++ )
{
printf( "entry %d = %d\n", i, dataArray[i] );
}
Note: when calling printf()
no need to obtain the address of a variable (unless that is what you want to print).注意:调用
printf()
时不需要获取变量的地址(除非那是你想要打印的)。 However, when inputting a variable, as when calling fscanf()
need the address of the variable.但是,在输入变量时,如调用
fscanf()
时需要变量的地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.