本文介绍了C,如何一起使用fgets和fscanf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个家庭作业表格.即使用文件IO.

有这样的TXT文件:

Brian
s213551 50 70 70 50
Alex Fernandes
s210011 70 81 50 89
Young Lee
s211213 60 80 60 90
... and more

我必须阅读此文件并将其保存到struct中的var中.和教授.对我说.我必须一起使用fgets和fscanf.如果我仅使用fscanf,则它的效果不佳,因为"Alex Fernandes"中有空格.

但是即使我同时使用fgets和fscanf,它也不起作用.所以我需要你的帮助.

这是我的出处:

#include <stdio.h>
#include <stdlib.h>

typedef struct sMember{
        char name[10];
        char id[10];
        int score[4];
        double avg;
        char grade;
}MEM;

int main(int argc, char *argv[])
{
  MEM member[50];
  FILE *f;
  char fileName[10];
  char s[512];
  int i;
  printf("File Name : ");
  scanf("%s", fileName);
  fflush(stdin);

  i=0;
  if((f = fopen(fileName, "r")) != NULL )
  {

//        while(!feof(f)) 
          while(fgets(member[i].name, sizeof(member[i].name), f) != 0)
        {
         fscanf(f, "%s %d %d %d %d", member[i].id, &member[i].score[0], &member[i].score[1], &member[i].score[2], &member[i].score[3]);

         printf("%s %s %d %d %d %d\n", member[i].name, member[i].id, member[i].score[0], member[i].score[1], member[i].score[2], member[i].score[3]);
         //printf("t: %s\n", member[i].name);
         i++;               
        }
  } 
  else 
  {
      printf("File is Not Exist.\n"); 
  }


  fclose(f);
  system("PAUSE");  
  return 0;
}
解决方案

只需在fscanf()参数的末尾添加一个空格以使用换行符.

fscanf(f, "%s %d %d %d %d ", member[i].id, &member[i].score[0], &member[i].score[1], &member[i].score[2], &member[i].score[3]);

i hav a homework form univ.that is use file IO.

there is like this TXT file:

Brian
s213551 50 70 70 50
Alex Fernandes
s210011 70 81 50 89
Young Lee
s211213 60 80 60 90
... and more

I have to read this file and save to var in struct.and prof. said to me. I have to use fgets and fscanf together.if I use only fscanf its not working nicely because "Alex Fernandes" has space in it.

but even though I use fgets and fscanf together, its not working..so i need help u.

this is my source:

#include <stdio.h>
#include <stdlib.h>

typedef struct sMember{
        char name[10];
        char id[10];
        int score[4];
        double avg;
        char grade;
}MEM;

int main(int argc, char *argv[])
{
  MEM member[50];
  FILE *f;
  char fileName[10];
  char s[512];
  int i;
  printf("File Name : ");
  scanf("%s", fileName);
  fflush(stdin);

  i=0;
  if((f = fopen(fileName, "r")) != NULL )
  {

//        while(!feof(f)) 
          while(fgets(member[i].name, sizeof(member[i].name), f) != 0)
        {
         fscanf(f, "%s %d %d %d %d", member[i].id, &member[i].score[0], &member[i].score[1], &member[i].score[2], &member[i].score[3]);

         printf("%s %s %d %d %d %d\n", member[i].name, member[i].id, member[i].score[0], member[i].score[1], member[i].score[2], member[i].score[3]);
         //printf("t: %s\n", member[i].name);
         i++;               
        }
  } 
  else 
  {
      printf("File is Not Exist.\n"); 
  }


  fclose(f);
  system("PAUSE");  
  return 0;
}
解决方案

Just add a space in the end of fscanf() parameter to consume the newline character.

fscanf(f, "%s %d %d %d %d ", member[i].id, &member[i].score[0], &member[i].score[1], &member[i].score[2], &member[i].score[3]);

这篇关于C,如何一起使用fgets和fscanf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 03:32