我有這個CSV文件,我要讀它,並打印每個不同的客戶(每行一個客戶)從CSV文件中讀取並解決「;」問題
IDClient;Name;Surname;Address;City;State;Postal_Code
111A;Howard;Snyder;2732 Baker Blvd.;Eugene;OR;97403
222B;Yoshi;Latimer;City Center Plaza 516 Main St.;Elgin;OR;97827
333C;John;Steel;12 Orchestra Terrace;Walla Walla;WA;99362
444D;Jaime;Yorres;87 Polk St. Suite 5;San Francisco;CA;94117
我寫了這個代碼,儘管一些警告似乎除了一些麻煩的工作與;
int main(int argc, char const *argv[])
{
FILE *fp;
fp = fopen ("clientes.txt", "rt");
if (fp == NULL) {
printf("Error.\n");
}
else {
printf("File is open\n");
printCustomers(fp);
fclose(fp);
}
return 0;
}
void printCustomers (FILE *fp)
{
char id[5], first [8], last [10], adress[17], city[12], state[3], zip[6];
printf("Printing.\n");
fscanf(fp, "%*[^\n]\n", NULL); //Skip first line
printf("-----------------------\n");
while (fscanf(fp,"%4s %7s %9s %16[^\n] %11[^\n] %2s %5s", &id, &first, &last, &adress, &city, &state, &zip) == 7)
{
printf("IdClient: %s\nName: %s\nSurname: %s\nAdress: %s\nCity: %s\nState: %s\nZip: %s\n", id, first, last, adress, city, state, zip);
printf("-----------------------\n");
}
}
我的輸出是這樣的,你可以看到;
正在破壞輸出。我認爲這可以用fscanf格式化來解決,但我不知道該怎麼做。
-----------------------
IdClient: 111A
Name: ;Howard
Surname: ;Snyder;2
Adress: 732 Baker Blvd.;
City: Eugene;OR;9
State: 74
Zip: 03
-----------------------
IdClient: 222B
Name: ;Yoshi;
Surname: Latimer;C
Adress: ity Center Plaza
City: 516 Main St
State: .;
Zip: Elgin
-----------------------
IdClient: ;OR;
Name: 97827
Surname: 333C;John
Adress: ;Steel;12 Orches
City: tra Terrace
State: ;W
Zip: alla
-----------------------
IdClient: Wall
Name: a;WA;99
Surname: 362
Adress: 444D;Jaime;Yorre
City: s;87 Polk S
State: t.
Zip: Suite
----------------------
如果我沒有解釋好自己或缺少什麼東西,請告訴我。
產量已是那樣的爲每一個客戶,可能與我目前的fscanf這是一個有點難以達到這一點,因爲不是所有的領域都有每個客戶相同的長度:
-----------------------
IdClient: 111A
Name: Howard
Surname: Snyder
Adress: 2732 Baker Blvd.
City: Eugene
State: OR
Zip: 97403
-----------------------
「CSV」中的「C」代表「逗號」,而不是「分號」。 –
是的,這是真的我以爲是一樣的,告訴我的老師,但他仍然稱它爲一個CSV文件;)反正差異是荒謬的,它只是改變逗號分號。 – RBNMUSTDIE