停止的fscanf? [英] Stopping fscanf?

查看:278
本文介绍了停止的fscanf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写学校一年级的书计划,我感到困惑的一件事。我们有一个文件从具有多个学生ID和多个分数来读取。请问这样做:

  =结果的fscanf(成绩册,%D%D,ID,SCO1,SCO2,sco3);

存储号码到这些变量,因为它读取然后停止光标当它运行的变量来存储中的信息?......那么我应该直接进入计算功能面前来计算最终成绩是循环的fscanf下一个学生?

  =结果的fscanf(成绩册,%D%D,ID,SCO1,SCO2,sco3);
          getCalc(SCO1,SCO2,sco3);

是允许的?谢谢你的帮助。


解决方案

  

应该这样做:


  
  

结果=的fscanf(成绩册,%D%D,ID,SCO1,
  SCO2,sco3);


  
  

存储号码到这些变量,因为它会读取
  停止光标时,它运行的变量来存储信息
  在?


没有,除非它是在一个while循环,它停止准确读取四个条目一次,除非您使用的是结果后的值来控制while循环和调用的fscanf()多次扫描4项在每个呼叫。

例如: -

  //一直读取,除非遇到低于4个参数,这可能是这种情况
//如果达到文件结束,或者您的数据格局的变化。而(的fscanf(成绩册,%D%D,ID,SCO1,SCO2,sco3)== 4)
{
 //可以通过个别ID的数据,来计算功能,并计算所需的
 //款项,全部或局部的功能等数据,实在没有理由使用通
 //地址在你的情况,所以才转用按值传递方法的数据。
 getCalc(ID,SCO1,SCO2,sco3);
}

I'm writing a grade book program for school and I'm confused about one thing. We have a file to read from that has multiple student IDs and multiple scores. Would doing this:

results = fscanf(gradebook, "%d %d %d %d", id, sco1, sco2, sco3);

store the number into those variables as it reads then stop the cursor when it runs out of variables to store the information in?...then should I jump right into a calculation function to calculate the final grade before having it loop the fscanf for the next student?

results = fscanf(gradebook, "%d %d %d %d", id, sco1, sco2, sco3);
          getCalc(sco1, sco2, sco3);

Is that allowed? Thank you for your help.

解决方案

Would doing this:

results = fscanf(gradebook, "%d %d %d %d", id, sco1, sco2, sco3);

store the number into those variables as it reads then stop the cursor when it runs out of variables to store the information in?

No, unless it is in a while loop, it stops exactly after reading the four entries ONCE, unless you are using the value from results to control a while loop and call fscanf() multiple times to scan 4 entries at every call.

Example:-

//Keeps reading unless it encounters less than 4 parameters, which might be the case
//if an end of file is reached, or your data pattern changes.

while(  fscanf(gradebook, "%d %d %d %d", id, sco1, sco2, sco3) == 4 )
{
 //You can pass the data of individual id, to calculate function, and compute the required
 //sum, total or other data locally in the function, there is really no reason to use pass
 //address in your case, so just transfer data using pass by value method. 
 getCalc(id,sco1,sco2,sco3);   
}

这篇关于停止的fscanf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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