[問題] binary file讀寫問題
新手想問一個簡單的問題...
上網查但都找不到我想要的
我的程式原本是用text file寫成
run起來是ok的也符合規格
但後來發現有多一條規定要用binary file方式讀寫> <
規格要求有五個項目
第一(主項目)、二(次項目)、三(名稱)都是字串且字串長度都很簡短
第四項是int 代表第五項的行數
而第五項是一段文字(代表此樣物品的簡介)
這五個項目我是用Linked list寫
而這隻程式的作用是將txt檔當作小小小小型資料庫使用
使用者可以查詢關鍵字
新增資料進入這樣
寫入程式片段
while(temp!=NULL){
fwrite(temp->recipe_type,sizeof(temp->recipe_type),1,file1);
fwrite(temp->subtype,sizeof(temp->subtype),1,file1);
fwrite(temp->name,sizeof(temp->name),1,file1);
fwrite(&temp->file_line,sizeof(int),1,file1);
fwrite(temp->recipe,sizeof(temp->recipe),1,file1);
temp=temp->next;
}
讀取程式片段
fread(new_recipe->recipe_type,sizeof(new_recipe->recipe_type),1,file1);
fread(new_recipe->subtype,sizeof(new_recipe->subtype),1,file1);
while(1){
ch=fgetc(file1);
new_recipe->name[count]=ch;
if(ch=='\n'){
break;
}
count++;
}
new_recipe->name[count]='\0';
fread(&new_recipe->file_line,sizeof(int),1,file1);
do{
ch=fgetc(file1);
if(ch=='\n'){
line_counter++;
}
new_recipe->recipe[counter]=ch;
counter++;
}while(line_counter!=(new_recipe->file_line));
而我目前遇到的問題是
如果不是用text file 那該如何辨識換行?
我有上網查但看不太懂ˊˋ
還有第三項規定要自成一行,但我用fwread寫出來是全部連在一行
有方法讓fwrite寫入換行嗎?
還有想問fprintf fscanf fgetc fgets都無法使用在binary file上面嗎?
第一次要用binary file完全摸不著頭緒阿:(
謝謝
※ 編輯: parkko 來自: 114.35.167.242 (06/17 16:14)
→
06/17 16:39, , 1F
06/17 16:39, 1F
→
06/17 16:40, , 2F
06/17 16:40, 2F
→
06/17 16:42, , 3F
06/17 16:42, 3F
→
06/17 16:42, , 4F
06/17 16:42, 4F
→
06/17 16:46, , 5F
06/17 16:46, 5F
→
06/17 16:46, , 6F
06/17 16:46, 6F
→
06/17 16:47, , 7F
06/17 16:47, 7F
→
06/17 16:47, , 8F
06/17 16:47, 8F
→
06/17 17:05, , 9F
06/17 17:05, 9F
→
06/17 20:34, , 10F
06/17 20:34, 10F
→
06/17 20:34, , 11F
06/17 20:34, 11F
→
06/17 20:35, , 12F
06/17 20:35, 12F
→
06/17 20:35, , 13F
06/17 20:35, 13F
→
06/17 20:53, , 14F
06/17 20:53, 14F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章