首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >逐行读取数字并将其拆分为C中的多个变量

逐行读取数字并将其拆分为C中的多个变量
EN

Stack Overflow用户
提问于 2016-04-07 21:13:39
回答 1查看 45关注 0票数 0

samplegame.txt(actual_file)

代码语言:javascript
复制
6
2 3 3
11 2
18 1
10 3
22 14
30 19
8 16
12 24
20 33

我希望将数据.txt文件读入我的C program.for中--每行分隔它们并将它们存储在变量中,这样我就可以在进一步的步骤中使用。

代码语言:javascript
复制
 FILE* file = fopen (argv[1], "r");
  int i = 0;

  fscanf (file, "%d", &i);
  while (!feof (file))
    {
      //printf ("%d ", i);
      fscanf (file, "%d", &i);
    }
  fclose (file);

但是我把所有的数字都列在一行里,不知道如何把每一组分开

代码语言:javascript
复制
int board_size = 6  << 1st line 

int minion_box_size = 2; << 2nd line
int trap_size = 3;
int trampoline_size = 3;

int minion_box[minion_box_size] = {11,18}; << 3rd to N line 
int minion_walk[minion_box_size] = {2,1};

int trap_position[trap_size] = {10,22,30}; << N+1 to M line 
int trap_end[minion_box_size] = {3,14,19};

int trampoline_position[trampoline_size] = {8,12,20}; << M+1 to k line
int trampoline_jump[trampoline_size] = {16,24,33};

有人有建议吗?

samplegame.txt(explained)

代码语言:javascript
复制
______Board_________

6       //  size of game board 
_____size of each item_________
2 3 3      //  first number in this line is Minion_box,which means there is 2 Minion_box
           //  Second number is trap_holes,which means there is 3 traps
           //  third number is trampoline,which means there is 3 trampolines
_____Minion_Box_______
//  first number     : where the minion_box are 
//  Second number    : how many step that the minion can walk 
11 2       //  the box is in 11th block , minion has aiblity to walk 2 step
18 1       //  the box is in 18th block , minion has ability to walk 1 step
______Traps__________
//  first number     : where the traps are  
//  Second number    : the block you gonna be,after stepping on the trap.
10 3       // there is a trap hole in the 10th block,step on it and you will fall down 3rd block
22 14      // a trap hole in the 22th block,fall down to 14th block
30 19      // a trap hole in the 30th block,fall down to 19th block
____trampoline_______
//  first number     : where the trampoline are
//  Second number    : the block you gonna be,after stepping on the trampoline.
8 16       // there is a trampoline in 8th block,if you step on it you will jump to 16th block
12 24      // trampoline in 12th block,jump to 24th block
20 33      // trampoline in 20th block,jump to 33th block

很抱歉我的英语很差,发帖太长了,因为我的英语不好,所以我试着尽可能多地展示这个例子:

EN

回答 1

Stack Overflow用户

发布于 2016-04-07 21:25:02

如果您想逐行读取文件行,最好使用fgets()sscanf()

代码语言:javascript
复制
char buffer[BUFFER_SIZE]; // with a #define BUFFER_SIZE 1024 or whatever sooner in the code
int first_value, second_value;

if (fgets(buffer, BUFFER_SIZE, f) == NULL)
{
    if ferror(f)
    {
        perror("");
    }

    exit(1);
}

if (sscanf(buffer, "%d %d", &first_value, &second_value) != 2)
{
    fprintf(stderr, "missing value\n");
    exit(1);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36487335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档