从在C文本文件中读取int类型 [英] Read int values from a text file in C

查看:1145
本文介绍了从在C文本文件中读取int类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下三行文本文件:

I have a text file that contains the following three lines:

12 5 6
4 2
7 9

我可以使用的fscanf 函数读取第3值,并将其存放在3个变量。但我不能读休息。
我尝试使用 fseek的的功能,但它仅适用于二进制文件。

I can use the fscanf function to read the first 3 values and store them in 3 variables. But I can't read the rest. I tried using the fseek function, but it works only on binary files.

请帮我所有的值存储在整数变量。

Please help me store all the values in integer variables.

推荐答案

用一个简单的解决方案的fscanf

A simple solution using 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);        
}

这篇关于从在C文本文件中读取int类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆