简体   繁体   English

为什么不保存我的变量? 我一直在测试此代码,但我的变量未保存到结构数组中

[英]Why aren't my variables being saved? I've been testing this code and my variables aren't being saved into my struct array

Here's my code and output of my variables: What else do I need to add to my while loop so that I can have my variables and methods work. 这是我的代码和变量的输出:我还需要添加其他内容到while循环中,以使变量和方法正常工作。

int main() {
  FILE *ifp;
  ifp = fopen("processes.txt","r");
  if(ifp == NULL) {
      printf("Error Opening file.\n");
      exit(1);
  }

  char str[100];
  int i=0;

  while(fgets(str, 100, ifp) != NULL) {
    fscanf(ifp," %d %0.1f %d %d", &arr[i].pid, &arr[i].AT, &arr[i].duration, &arr[i].rank);
    i++;
  }
}
printf("ProceId AT   duration rank\n");

for (int j = 0; j < i - 1; ++j){
    printf("%d\t%0.1f\t%d\t%d\n",arr[j].pid,arr[j].AT,arr[j].duration,arr[j].rank );
}


ProceId AT   duration rank
1398    0.0     0       0
2306    0.0     0       0
3219    0.0     0       0
4478    0.0     0       0
5653    0.0     0       0
6399    0.0     0       0
7777    0.0     0       0

Here is the file which has a line of strings that i don't need which is why i used fgets. 这是有一行不需要的字符串的文件,这就是为什么我使用fgets的原因。

ProcessID   ArrTime Duration  Rank

1398        1.0     16      3
2306        4.5     6       7
3219        3.0     11      1
4478        2.0     3       5
5653        3.5     7       2
6399        4.0     8       6
7777        2.5     17      8   

Change this: 更改此:

fscanf(ifp," %d %0.1f %d %d", &arr[i].pid, &arr[i].AT, &arr[i].duration, &arr[i].rank);

to this: 对此:

fscanf(ifp," %d %f %d %d", &arr[i].pid, &arr[i].AT, &arr[i].duration, &arr[i].rank);

since you were using an invalid conversion specifier. 因为您使用的是无效的转换说明符。

Pay attention to the compiler warnings next time, they usually tell the whole story: 下次要注意编译器警告,它们通常会讲述整个故事:

warning: invalid conversion specifier '.'
      [-Wformat-invalid-specifier]
        fscanf(ifp," %d %0.1f %d %d", &arr[i].pid, &arr[i].AT, &arr[i].d...
                       ~~~^
warning: format specifies type 'int *' but the argument has type
      'float *' [-Wformat]
        fscanf(ifp," %d %0.1f %d %d", &arr[i].pid, &arr[i].AT, &arr[i].d...
                              ~~                   ^~~~~~~~~~
                              %f
warning: data argument not used by format string
      [-Wformat-extra-args]
  ..." %d %0.1f %d %d", &arr[i].pid, &arr[i].AT, &arr[i].duration, &arr[i].ra...
     ~~~~~~~~~~~~~~~~~                                             ^

The warnings suggest that because of the invalid conversion specifier, the %d format specifier is used for the float field of your struct (named AT ), and then everything becomes a mess, since you are short by one format specifier... 警告提示由于无效的转换说明符, %d格式说明符用于结构体的float字段(名为AT ),然后一切都变得一团糟,因为您缺少一个格式说明符...

Read more in How to only accept a certain precision (so many decimals places) in scanf? 如何仅在scanf中接受某个精度(这么多小数位)中阅读更多内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM