I'm having problems using a fscanf
function. It never reads it correctly and always results in blanks.
fscanf(f, " %[^;];%[^;];%d;%d;%d;%d;%[^;];%d;%[^\n]",
arr[i].loc1, arr[i].loc2, &arr[i].price, &arr[i].rooms,
&arr[i].bathroom, &arr[i].carpark, arr[i].type, &arr[i].area, arr[i].furnish);
The code above always outputs " 0 0 0 0 0". But when I try using a scanf and manually input one of the lines, it works perfectly.
The file it's reading from is a .csv file. Here is the contents:
Mont-Kiara;Kuala-Lumpur;1000000;2;2;0;Built-up;1000;Partly
Cheras;Kuala-Lumpur;310000;3;2;0;Built-up;1000;Partly
Kepong;Kuala-Lumpur;358000;3;3;0;Built-up;1000;Partly
Taman-Desa;Kuala-Lumpur;455000;2;2;0;Built-up;1000;Partly
Kepong;Kuala-Lumpur;358000;3;3;0;Built-up;1000;Partly
Kepong;Kuala-Lumpur;358000;3;3;0;Built-up;1000;Partly
And here is the full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct houseData {
char loc1[101];
char loc2[101];
int price[101];
int rooms[101];
int bathroom[101];
int carpark[101];
char type[101];
int area[101];
char furnish[101];
} arr[1001];
int read() {
int i = 0;
struct houseData arr[800];
char temp1[100];
FILE *f = fopen("file.csv", "r");
while(!feof(f)){
fscanf(f, " %[^;];%[^;];%d;%d;%d;%d;%[^;];%d;%[^\n]"
, &arr[i].loc1, &arr[i].loc2, &arr[i].price, &arr[i].rooms,
&arr[i].bathroom, &arr[i].carpark, &arr[i].type, &arr[i].area, &arr[i].furnish);
i++;
}
fclose(f);
}
int main() {
read();
printf("%s %s %d %d %d %d %s %d %s", arr[i].loc1, arr[i].loc2, *arr[i].price, *arr[i].rooms, *arr[i].bathroom, *arr[i].carpark, arr[i].type, *arr[i].area, arr[i].furnish);
return 0;
}