elh*_*hdi 22 c file-io file lines
我有一个包含以下三行的文本文件:
12 5 6
4 2
7 9
Run Code Online (Sandbox Code Playgroud)
我可以使用该fscanf
函数读取前3个值并将它们存储在3个变量中.但我无法阅读其余的内容.我尝试使用该fseek
函数,但它只适用于二进制文件.
请帮我将所有值存储在整数变量中.
Vij*_*hew 45
一个简单的解决方案fscanf
:
void read_ints (const char* file_name)
{
FILE* file = fopen (file_name, "r");
int i = 0;
fscanf (file, "%d", &i);
while (!feof (file))
{
printf ("%d ", i);
fscanf (file, "%d", &i);
}
fclose (file);
}
Run Code Online (Sandbox Code Playgroud)
这个怎么样?
fscanf(file,"%d %d %d %d %d %d %d",&line1_1,&line1_2, &line1_3, &line2_1, &line2_2, &line3_1, &line3_2);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,空格fscanf
匹配任何空格的多次出现,直到找到下一个标记.