Я пытаюсь сохранить .txt-файл в struct, не будучи успешным. Мне не нужна первая строка, но мне нужно все остальное. Спасибо, ребята! =)Сохранение .txt-файла в структуры
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
struct candidates
{
char name[25];
char gender[5];
int height;
int weight;
};
struct candidates values[25];
FILE *fp;
if ((fp=fopen("candidatesdata.txt","r"))==NULL){printf("Unable to open file\n");}
int x = 0;
int iterations = 0;
while((fscanf(fp,"%s,%s,%d,%d",&values[x].name, &values[x].gender, &values[x].height, &values[x].weight))!=EOF)
{
x++;
iterations +=1;
}
fclose(fp);
//values[15].weight = 300;
printf("\n%d\t%d",iterations,values[1].height);
return 0;
}
Текстовый файл выглядит следующим образом:
Name,Gender,Height,Weight
Tanner,M,71.8,180.25
John,M,70.75,185.3
Parker,F,65.25,120.3
Meeks,M,57.25,210.2
И ваша очередь stion/ошибка? – ckruczek
ваш 'fscanf' ожидает целых чисел по высоте и весу, но они, очевидно, числа с плавающей точкой. – Aif
неудивительно, но структура по-прежнему не сохраняет данные в txt вправо –