[問題] 讀檔
檔案是a.txt
裡面的格式是
小明 5000 50 20 30
小華 4500 40 70 80
typedef struct man_s {
char *name; //名字
int pay;
int par1;
int par2;
int par3;
} man_t;
然後我有宣告一個structure
希望把檔案裡面的東西讀到結構裡
可是再讀到pay時, name會跟pay相同
請問這是哪邊有問題?
man_t man[2];
FILE *FP;
char str[100];
int i,j,tmp,count = 0;
if((FP = fopen("a.txt", "r+b")) == NULL){
printf("File : %s 並不在目前的資料夾下 請重新確認!\n","a.txt");
system("pause");
exit(0);
}
for(i=0;fscanf(FP,"%s",str)!=EOF;i++){
if(i%5 == 0){
man[count].name = str;
}else if(i%5 == 1){
tmp = atoi(str);
man[count].pay = tmp;
}else if(i%5 == 2){
tmp = atoi(str);
man[count].var1 = tmp;
}else if(i%5 == 3){
tmp = atoi(str);
man[count].var2 = tmp;
}else{
tmp = atoi(str);
man[count].var3 = tmp;
count++;
}
printf("%s\n",man[count].name);
printf("%d\n",man[count].pay);
printf("%d\n",man[count].var1);
printf("%d\n",man[count].var2);
printf("%d\n",man[count].var3);
system("pause");
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 180.177.2.215
→
02/06 22:11, , 1F
02/06 22:11, 1F
→
02/06 22:11, , 2F
02/06 22:11, 2F
→
02/06 22:12, , 3F
02/06 22:12, 3F
推
02/06 22:32, , 4F
02/06 22:32, 4F
→
02/06 22:33, , 5F
02/06 22:33, 5F
→
02/06 22:35, , 6F
02/06 22:35, 6F
→
02/06 22:35, , 7F
02/06 22:35, 7F
→
02/06 22:35, , 8F
02/06 22:35, 8F
→
02/06 22:36, , 9F
02/06 22:36, 9F
→
02/06 22:36, , 10F
02/06 22:36, 10F
推
02/06 22:45, , 11F
02/06 22:45, 11F
推
02/09 18:30, , 12F
02/09 18:30, 12F
討論串 (同標題文章)