#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int num = 0;
int teacher1();
int teacher2();
struct teacher teacher3(struct teacher t1);
struct teacher teacher4(struct teacher t1, int i);
struct teacher teacher5(struct teacher t1, int i);
int main()
{
int n;
printf("**這是課程設計答辯預約系統**\n");
printf("教師請輸入1,學生請輸入2:\n");
scanf("%d", &n);
getchar();
if (n == 1)
{
teacher1();
}
else if (n == 2)
printf("ok\n");
else
{
printf("你的輸入有誤,請重新輸入。\n\n");
main();
}
}
int teacher1()
{
FILE *fp;
if ((fp = fopen("file.dat", "wb")) == NULL)
{
printf("建立檔案出錯\n");
exit(0);
}
fclose(fp);
int i, j;
char a[8], b[8] = { 't', 'e', 'a', 'c', 'h', 'e', 'r' };
for (i = 1; i != 0;)
{
printf("請輸入正確的用戶標識:\n");
printf("(老師請輸入teacher,學生請輸入學號)\n");
scanf("%s", a);
j = strcmp(a, b);
if (j == 0)
{
i = 0;
break;
}
else
{
printf("你的輸入有誤,請重新輸入。\n\n");
i++;
}
}
teacher2();
return 0;
}
struct teacher
{
char time[11];
int a[13];
int b[13];
}t[10];
int teacher2()
{
int a, b, c;
printf("請輸入可預約的答辯日期:\n");
printf("格式為:xxxx-xx-xx(年-月-日)\n");
printf("(中間的橫桿用減號代替)\n");
scanf("%s", t[num].time);
a = (t[num].time[0] - 48) * 1000 + (t[num].time[1] - 48) * 100 + (t[num].time[2] - 48) * 10 + (t[num].time[3] - 48);
b = (t[num].time[5] - 48) * 10 + (t[num].time[6] - 48);
c = (t[num].time[8] - 48) * 10 + (t[num].time[9] - 48);
if (b <= 0 || b>12)
{
printf("你的輸入有誤,請重新輸入。\n\n");
teacher2();
}
else if (c <= 0 || c > 31)
{
printf("你的輸入有誤,請重新輸入。\n\n");
teacher2();
}
else if ((a % 4 == 0) && (b == 2) && (c >= 30))
{
printf("你的輸入有誤,請重新輸入。\n\n");
teacher2();
}
else if ((a % 4 != 0) && (b == 2) && (c >= 29))
{
printf("你的輸入有誤,請重新輸入。\n\n");
teacher2();
}
else if (((b = 4) || (b = 6) || (b = 9) || (b = 11)) && (c == 31))
{
printf("你的輸入有誤,請重新輸入。\n\n");
teacher2();
}
else
{
teacher3(t[num]);
}
return 0;
}
struct teacher teacher3(struct teacher t1)
{
int i = 0;
printf("請輸入可預約時段:\n");
printf("(時段按1-12節課劃分)\n");
printf("(輸入完畢請以0結尾)\n");
while (1)
{
scanf("%d", &t1.a[i]);
if (t1.a[i] == 0)
{
t1.a[i + 1] = '\0';
teacher4(t1,i);
break;
}
i++;
}
return t1;
}
struct teacher teacher4(struct teacher t1, int i)
{
int j;
for (j = 0; j < i;j++)
{ printf("請輸入第%d時段可預約人數:\n", t1.a[j]);
printf("(每節課答辯人數少于5比較合適)\n");
scanf("%d", &t1.b[j]);
}
teacher5(t1,i);
return t1;
}
struct teacher teacher5(struct teacher t1, int i)
{
int n;
FILE *fp;
if ((fp = fopen("file.dat", "ab")) == NULL)
{
printf("建立檔案出錯\n");
exit(0);
}
if (fwrite(&t[num], sizeof(struct teacher), 1, fp) != 1)
printf("file write error\n");
{
printf("繼續設定日期請輸入1,\n");
printf("預覽預約設定請輸入2,\n");
printf("退出本角色請輸入0。\n");
scanf("%d", &n);
if (n == 1)
{
num++;
teacher2();
}
else if (n == 2)
{
t1.b[12] = '\0';
int j;
for (j = 0; j<= num; j++)
{
fread(&t[j], sizeof(struct teacher), 1, fp);
int m, k;
printf("可預約日期:");
printf("%s\n", t[j].time);
printf("可預約時段:");
for (m = 0; m < i; m++)
printf("%5d", t[j].a[m]);
printf("\n");
printf("可預約人數:");
for (k = 0; k < i; k++)
printf("%5d", t[j].b[k]);
printf("\n");
}
fclose(fp);
teacher5(t1,i);
}
else if (n == 0)
main();
else
{printf("你的輸入有誤,請重新輸入。\n");
teacher5(t1,i);
}
}
return t1;
}
為啥最后輸出的是亂碼

uj5u.com熱心網友回復:
if (fwrite(&t[num], sizeof(struct teacher), 1, fp) != 1)
printf("file write error\n");
這是寫入資料,然后如果輸入選擇預覽就
fread(&t[j], sizeof(struct teacher), 1, fp);
這不行
fwrite之后,檔案指標跑最后去了
fread讀回來的只能是eof了
fwrite后,還要fflush或者直接fclose才會保證把資料寫入磁盤,否則還在磁盤緩沖區里面呢,馬上讀也讀不出來
fflsh(fp)
fseek回到檔案頭再開始讀
此外,為什么要用fread,而不是記憶體當中提取呢?
這個邏輯不對
添加,修改了資料后寫入檔案,查詢在記憶體當中的檔案里面進行就好了
uj5u.com熱心網友回復:
記憶體結構到磁盤的寫入作業, 用一個單獨的函式完成反向從磁盤讀取的作業也是一個單獨函式完成
程式啟動的時候,從磁盤加載資料到記憶體
資料增刪改之后,寫入磁盤
查詢就是記憶體結構直接出結果就好了
除非你的記憶體資料量特別大,有成千上萬條,否則完沒有必要處處從磁盤來操作
uj5u.com熱心網友回復:
大佬,有點聽不懂
,我用fprintf和fscanf輸出結果日期沒錯但是下面的都是0
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/156968.html
標籤:C語言
上一篇:行程的概念,系統資源分配的單元
