ファイルの読み込みについて
Posted: 2016年1月23日(土) 17:26
ファイルを入力するプログラムを書いていますが、ファイルが上手く読み込まれません。
読み込むファイルはこちらです。
Jane
1.72
55
Tom
1.76
72
Judy
1.76
67
Mary
1.80
78
Jhon
1.76
76
こちらが僕が書いたプログラムです
#include <stdio.h>
#include <stdlib.h>
#define NUM 5
int main (void)
{
int i;
char name[NUM][100];
double height[NUM];
int weight[NUM];
double BMI[NUM];
FILE *fp;
char file_name[] = "q.txt";
/* ファイルオープン */
if ((fp = fopen(file_name, "r")) == NULL) {
fprintf(stderr, "%s %lf %d\n", "error: can't read file.");
return EXIT_FAILURE;
}
for(i=0;i<NUM;i++){
fscanf(fp, "%s\n", name);
}
for(i=0;i<NUM;i++){
fscanf(fp, "%lf\n", &height);
}
for(i=0;i<NUM;i++) {
fscanf(fp, "%d\n", &weight);
}
for(i=0;i<NUM;i++){
printf("%s\n", name);
}
for(i=0;i<NUM;i++){
printf("%lf\n", &height);
}
for(i=0;i<NUM;i++){
printf("%d\n", &weight);
}
// BMI=(double)weight/(height*height);
/*for(i=0;i<NUM;i++){
printf("%s",name[i]);
if(BMI[i]>25&&30>=BMI[i]){
printf("%f 注意\n",BMI[i]);
}
else if(BMI[i]>30){
printf("%f 警告\n",BMI[i]);
}
else{
printf("%f\n",BMI[i]);
}
}*/
getchar();
getchar();
return 0;
}
読み込むファイルはこちらです。
Jane
1.72
55
Tom
1.76
72
Judy
1.76
67
Mary
1.80
78
Jhon
1.76
76
こちらが僕が書いたプログラムです
#include <stdio.h>
#include <stdlib.h>
#define NUM 5
int main (void)
{
int i;
char name[NUM][100];
double height[NUM];
int weight[NUM];
double BMI[NUM];
FILE *fp;
char file_name[] = "q.txt";
/* ファイルオープン */
if ((fp = fopen(file_name, "r")) == NULL) {
fprintf(stderr, "%s %lf %d\n", "error: can't read file.");
return EXIT_FAILURE;
}
for(i=0;i<NUM;i++){
fscanf(fp, "%s\n", name);
}
for(i=0;i<NUM;i++){
fscanf(fp, "%lf\n", &height);
}
for(i=0;i<NUM;i++) {
fscanf(fp, "%d\n", &weight);
}
for(i=0;i<NUM;i++){
printf("%s\n", name);
}
for(i=0;i<NUM;i++){
printf("%lf\n", &height);
}
for(i=0;i<NUM;i++){
printf("%d\n", &weight);
}
// BMI=(double)weight/(height*height);
/*for(i=0;i<NUM;i++){
printf("%s",name[i]);
if(BMI[i]>25&&30>=BMI[i]){
printf("%f 注意\n",BMI[i]);
}
else if(BMI[i]>30){
printf("%f 警告\n",BMI[i]);
}
else{
printf("%f\n",BMI[i]);
}
}*/
getchar();
getchar();
return 0;
}