給定一個結構:
struct Info {
char name[70];
char address[120];
char email[100];
} info[100];
您將如何將.csv檔案傳輸到此結構中?
例子
拿這個.csv檔案
name,address,email
Carl March,3112 Agriculture Lane,[email protected]
Annie Scrims,1773 Sycamore Lake Road,[email protected]
您將如何將所有這些轉移到info結構中?空格會導致問題,使用 for 回圈似乎太笨拙了。
uj5u.com熱心網友回復:
而不是使用scanf("%s,%s,%s", ...)你應該使用%[^,\n].
這是一個例子:
#include <stdio.h>
struct Info {
char name[70];
char address[120];
char email[100];
} info[100];
int main() {
char buf[1024], eol[2];
int i = 0;
while (i < 100 && fgets(buf, sizeof buf, stdin)) {
if (sscanf(buf, "i[^,\n],9[^,\n],
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/457189.html
標籤:C
