我有一個結構數組,他們被保存到一個文件。目前有兩條線在該文件中:fscanf沒有保存數據結構?
a a 1
b b 2
我想在文件中讀取並保存到結構中的數據:
typedef struct book{
char number[11];//10 numbers
char first[21]; //20 char first/last name
char last[21];
} info;
info info1[500]
into num = 0;
pRead = fopen("phone_book.dat", "r");
if (pRead == NULL){
printf("\nFile cannot be opened\n");
}
else{
while (!feof(pRead)) {
fscanf(pRead, "%s%s%s", info1[num].first, info1[num].last, info1[num].number);
printf{"%s%s%s",info1[num].first, info1[num].last, info1[num].number); //this prints statement works fine
num++;
}
}
//if I add a print statement after all that I get windows directory and junk code.
這讓我覺得,在項目未保存到結構中。任何幫助都會很棒。謝謝!
編輯:好吧,它確實保存它,但是當我將它傳遞給我的函數時,它會給我垃圾代碼。
當我把它叫做:
sho(num, book);
我的播放功能:
void sho (int nume, info* info2){
printf("\n\n\nfirst after passed= %s\n\n\n", info2[0].first); //i put 0 to see the first entry
}
1)什麼是「進」? 2)'printf {'是一個語法錯誤,它不能正常工作。 – Kninnug
你能用文本編輯器打開文件「phone_book.dat」嗎? – SHR
請提供您真正使用的代碼。它不能是你顯示的代碼,因爲這將不會由於各種原因而編譯。 – alk