如何在txt中获取内容并将其存储在c中的数组中
How to get things in txt and store in array in c
我是编程小白,所以如果下面的代码不好,请不要怪我。我正在做一个关于库存系统的项目。本系统有修改、查询等五项功能。我的想法是让用户输入记录号,然后将txt中存储的数据取回数组,并显示正确的ID.With这个,用户可以编辑的东西是一定结构的数组。但我不知道如何将 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;
}
检查下面提到的片段。这应该敲响一些警钟。
fp = fopen("stock.txt", "r");
if (fp == NULL){
printf("Cannot open file \n");
return 0;
}
i = 0;
ch[i] = fgetc(fp);
while (ch != EOF){
i++;
ch[i] = fgetc(fp);
}
fclose(fp);
return 1;
我是编程小白,所以如果下面的代码不好,请不要怪我。我正在做一个关于库存系统的项目。本系统有修改、查询等五项功能。我的想法是让用户输入记录号,然后将txt中存储的数据取回数组,并显示正确的ID.With这个,用户可以编辑的东西是一定结构的数组。但我不知道如何将 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;
}
检查下面提到的片段。这应该敲响一些警钟。
fp = fopen("stock.txt", "r");
if (fp == NULL){
printf("Cannot open file \n");
return 0;
}
i = 0;
ch[i] = fgetc(fp);
while (ch != EOF){
i++;
ch[i] = fgetc(fp);
}
fclose(fp);
return 1;