一個將掛之人的死活解決不了的也可能是自己傻的問題,我的課設呀,讀取不了檔案。



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct paper
{
int opus;
char sort[30];
char book[20];
char writer[20];
char press[30];
int number;
float price;
}std[15];
int Diaoyon;
void menu()
{
printf("-------------------------\n");
printf("| 簡易圖書管理系統 |\n");
printf("-------------------------\n");
printf("| 1:顯示全部 |\n");
printf("| 2:按書名查詢 |\n");
printf("| 3:按作者查詢 |\n");
printf("| 4:按作者排序 |\n");
printf("| 5:按書名排序 |\n");
printf("| 6:添加記錄 |\n");
printf("| 7:修改資訊 |\n");
printf("| 8:總價 |\n");
printf("| 9:借閱查詢 |\n");
printf("| 0:結束 |\n");
printf("-------------------------\n");
}
int xsqb()
{
int i;
struct paper std[15];
printf("\n編號 分類 書名 作者 出版社 數量 單價");
for (i = 0; i < Diaoyon; i++)
printf("%d %s %s %s %s %d %0.2f", std[i].opus, std[i].sort[30], std[i].book[20], std[i].writer[20], std[i].press[30], std[i].number, std[i].price);
return 0;
}
int asmcx()
{
char book[20] = "";
int ibook = 0, i;
int G;
Loop:
printf("請輸入書名:");
scanf("%s", book);
for (i = 0; i < Diaoyon; i++)
{
if (!strcmp(book, std[i].book))
{
printf("%d %s %s %s %s %d %0.2f", std[i].opus, std[i].sort, std[i].book, std[i].writer, std[i].press, std[i].number, std[i].price);
ibook = 1;
}
}
if (!ibook)
{
printf("\n未找到匹配圖書");
}
printf("\n輸入33繼續查找:\n");
printf("請輸入書名:");
scanf("%d", G);
if (G == 33)
goto Loop;
return 0;
}
int azzcx()
{
char writer[20] = "";
int abook = 0, i;
int M;
Loop:
printf("請輸入作者名:");
scanf("%s", writer);
for (i = 0; i < Diaoyon; i++)
{
if (!strcmp(writer, std[i].writer))
{
printf("%d %s %s %s %s %d %0.2f", std[i].opus, std[i].sort, std[i].book, std[i].writer, std[i].press, std[i].number, std[i].price);
abook = 1;
}
if (!abook)
{
printf("\n未找到匹配圖書");
}
printf("\n輸入00繼續查找:");
printf("請輸入作者名:");
scanf("%d", M);
if (M == 00)
goto Loop;
}
return 0;
}
int azzpx()
{
struct paper books[15];
struct paper tem;
int i, j;
for (i = 0; i < Diaoyon; i++)
books[i] = std[i];
for (i = 1; i < Diaoyon; i++)
{
for (j = 1; j < Diaoyon - i; j++)
{
if (strcmp(books[j].writer, books[j + 1].writer) > 0)
{
tem = books[j];
books[j] = books[j + 1];
books[j + 1] = tem;
}
}
printf("%d %s %s %s %s %d %0.2f", books[15].opus, books[15].sort, books[15].book, books[15].writer, books[15].press, books[15].number, books[15].price);
}
return 0;
}
int asmpx()
{
struct paper Books[15];
struct paper temp;
int i, j;
for (i = 0; i < Diaoyon; i++)
Books[i] = std[i];
for (i = 1; i < Diaoyon; i++)
{
for (j = 1; j < Diaoyon - i; j++)
{
if (strcmp(Books[j].book,Books[j + 1].book) > 0)
{
temp = Books[j];
Books[j] = Books[j + 1];
Books[j + 1] = temp;
}
}
printf("%d %s %s %s %s %d %0.2f", Books[15].opus, Books[15].sort, Books[15].book, Books[15].writer, Books[15].press, Books[15].number, Books[15].price);
}
return 0;
}
int tjjl()
{
int i, a;
float j;
char zxc[30];
Loon:
printf("\n請按格式:編號 分類 書名 作者 數量 單價,\n 請輸入:");
scanf("%d", &a);
if (a >= 1 || a <= Diaoyon)
{
printf("編號重復,請輸入1到%d之外的數字:", Diaoyon);
goto Loon;
}
else
std[a - 1].opus = a;
scanf("%s", zxc);
strcpy(std[a - 1].sort, zxc);
scanf("%s", zxc);
strcpy(std[a - 1].book, zxc);
scanf("%s", zxc);
strcpy(std[a - 1].writer, zxc);
scanf("%s", zxc);
strcpy(std[a - 1].press, zxc);
scanf("%d", &i);
std[a - 1].number = i;
scanf("%0.2f", &j);
std[a - 1].price = j;
Diaoyon++;
printf("添加成功!");
return 0;
}
int xgxx()
{
int i, n;
char wtr[20];
float j;
Loot:
printf("請輸入需修改圖書編號:");
scanf("%d", &n);
if (n<1 || n>Diaoyon)
{
printf("輸入錯誤,編號在1到%d之間", Diaoyon);
goto Loot;
}
printf("所需圖書為:%d %s %s %s %s %d %0.2f", std[n - 1].opus, std[n - 1].sort, std[n - 1].book, std[n - 1].writer, std[n - 1].press, std[n - 1].number, std[n - 1].price);
printf("修改順序為:\n 分類 書名 作者 出版社 數量 單價");
scanf("%s", wtr);
strcpy(std[n - 1].sort, wtr);
scanf("%s", wtr);
strcpy(std[n - 1].book, wtr);
scanf("%s", wtr);
strcpy(std[n - 1].writer, wtr);
scanf("%s", wtr);
strcpy(std[n - 1].press, wtr);
scanf("%d", &i);
std[n - 1].number = i;
scanf("%0.2f", &j);
std[n - 1].price = j;
printf("\n修改后所得為:\n %d %s %s %s %s %d %0.2f", std[n - 1].opus, std[n - 1].sort, std[n - 1].book, std[n - 1].writer, std[n - 1].press, std[n - 1].number, std[n - 1].price);
return 0;
}
int zj()
{
return 0;
}
int jycx()
{
return 0;
}
void ReadFile()
{
FILE *fp;
int i = 0, j;
char qx[20];
if ((fp = fopen("D:/1/JK0638.txt", "r")) == NULL)
{
printf("\nCannot open file\n");
return;
}
fscanf(fp, "%s %s %s %s %s %s %s", qx, qx,qx,qx,qx,qx,qx);
while (!feof(fp))
{
fscanf(fp, "%d %s %s %s %s %d %0.2f", &std[i].opus, std[i].sort, std[i].book, std[i].writer, std[i].press, &std[i].number, &std[i].price);
printf("%d %s %s %s %s %d %0.2f", std[i].opus, std[i].sort, std[i].book, std[i].writer, std[i].press, std[i].number, std[i].price);
i++;
}
Diaoyon = i;
fclose(fp);
}
void main()
{
int ind;
do
{
ReadFile();
menu();
printf("請輸入操作編號:");
scanf("%d", &ind);
switch (ind)
{
case 1:xsqb(); break;
case 2:asmcx(); break;
case 3:azzcx(); break;
case 4:azzpx(); break;
case 5:asmpx(); break;
case 6:tjjl(); break;
case 7:xgxx(); break;
case 8:zj(); break;
case 9:jycx(); break;
case 0:end(1);
}
} while (1);
}
uj5u.com熱心網友回復:
https://blog.csdn.net/zhanghaiyang9999/article/details/107032563詳細介紹了怎么讀檔案,希望對你有幫助
uj5u.com熱心網友回復:
for (i = 0; i < Diaoyon; i++)
printf("%d %s %s %s %s %d %0.2f", std[i].opus, std[i].sort[30], std[i].book[20], std[i].writer[20], std[i].press[30], std[i].number, std[i].price);
這樣的問題在另一個帖子里已經給你修復了,這個帖子你還是這樣發出來,這沒法救~
命是自己的,救與不救還是靠自己。
uj5u.com熱心網友回復:
辜負您了!我確實瞎(T▽T)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/34176.html
標籤:C語言
上一篇:【問題描述】文本檔案score.txt中保存有一組學生的成績(為整數)
下一篇:將電腦時間寫入字符型或整形。
