1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C 構造体 csv読み込み ファイル入出力

Last updated at Posted at 2018-11-26

Cでcsvを扱ったときのメモ

構造体.c
# include <stdio.h>
# define NUM 3
typedef struct student {
  char name[60];
  char gender[10];
  int age;
} STUDENT;

int main(void) {
  int i;
  STUDENT people[3];
  FILE * file;

  //ファイル読み込み
  file = fopen("student.txt", "r");
  for(i=0;i<NUM;i++) {
    fscanf(file, "%[^,],%[^,],%d", people[i].name, people[i].gender, &people[i].age);
    // fscanf(file, "%s", people[i].name);
    // fscanf(file, "%s", people[i].gender);
    // fscanf(file, "%d", &people[i].age);
  }
  fclose(file);

  //出力
  for(i=0;i<NUM;i++) {
    printf("%s %s %d\n", people[i].name, people[i].gender, people[i].age);
  }

  return 0;
}
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?