【发布时间】:2018-11-30 06:27:48
【问题描述】:
我是一个编程菜鸟,所以如果下面的代码不好,请不要怪我。我正在做一个关于库存系统的项目。该系统有修改和搜索五个功能。我的想法是让用户输入记录号并将存储在txt中的数据返回到数组并显示具有正确ID的数据。这样,用户可以编辑事物是某个结构数组。但我不知道如何将 txt 中的数据获取到数组中。 它是 poissibe 或任何替代解决方案。
声明
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define true 1
#define false 0
struct RecordData {
char RecordNum[10];
char ItemName[ 50 ];
int ItemNum ;
char Category[ 50 ];
int Quantity;
char Recipient[ 50 ];
char Destination[ 50 ];
char Delivery[ 100 ];
};
struct RecordData record[MAX];
FILE *fp;
函数添加记录
int addrecord(){
int x, i = 0;
char ch, yesno;
char space = ' ';
doagain:
printf("1) Enter Record Number:");
gets(record[i].RecordNum);
printf("2) ItemName\nEnter :");
gets(record[i].ItemName);
printf("3) ItemNumber\nEnter :");
scanf("%d%c", &record[i].ItemNum, &ch);
printf("4) Category\nEnter : ");
gets(record[i].Category);
printf("5) Quantity\nEnter : ");
scanf("%d%c", &record[i].Quantity, &ch);
printf("7) Recipient\nEnter : ");
gets(record[i].Recipient);
printf("8) Final Destination\nEnter : ");
gets(record[i].Destination);
printf("9) Delivery status \nEnter : ");
gets(record[i].Delivery);
fp = fopen("stock.txt", "a");
fprintf(fp, "%04d\n", i);
fprintf(fp, "%s\n%d\n%s\n%d\n%s\n%s\n%s\n%c\n", record[i].ItemName, record[i].ItemNum,
record[i].Category, record[i].Quantity, record[i].Recipient, record[i].Destination, record[i].Delivery, space);
fclose(fp);
enterys:
printf("Do you want to add other record? Yes(Y) or No(N)");
scanf("%s", &yesno);
switch (yesno){
case 'Y':
case 'y':
i++;
goto doagain;
break;
case 'N':
case 'n':
printf("end program\n");
system("cls");
return main();
break;
default:
printf("you have enter wrong input");
goto enterys;
}
}
主要
int main(){
int num;
char space, ch;
mainGUI:
printf("1. Add New Item<s>:");
printf("\n2. Display Item Record:");
printf("\n3. Search Item Information:");
printf("\n4. Modify Item Record<s>:");
printf("\nDelete Item Record<s>:");
printf("\nWhat is your option? <1-5>");
scanf("%d", &num);
system("cls");
switch (num)
{
case 0:
printf("Quit^_^");
system("cls");
break;
case 1:
printf("You Are Now Adding New Item<s>:\n");
addrecord();
break;
case 2:
printf("You Are Now Displaying Item<s>:");
display();
printf("Press any Button to Go Back Menu");
scanf("%c%c", &ch, &space);
if (space == ' '){
system("cls");
goto mainGUI;
}else{
system("cls");
goto mainGUI;
}
break;
case 3:
printf("You Are Now searching items New Item<s>:");
break;
case 4:
printf("You Are Now Adding New Item<s>:");
break;
case 5:
printf("You Are Now Adding New Item<s>:");
break;
default:
printf("Enter Wrong input\n");
goto mainGUI;
break;
}
return 0;
}
【问题讨论】:
-
替代解决方案?一个数据库。
-
项目要求我使用c语言。如果需要的不是那样,我会的
-
不要使用
gets()。永远。 -
为什么?使用 scanf("%s", &char) 代替?