gpt4 book ai didi

c - 如何获取txt中的内容并将其存储在c中的数组中

转载 作者:行者123 更新时间:2023-11-30 19:28:54 24 4
gpt4 key购买 nike

我是编程小白,所以如果下面的代码写得不好,请不要怪我。我正在做一个关于库存系统的项目。本系统共有修改、搜索等五个功能。我的想法是让用户输入记录号并将存储在txt中的数据返回到数组并显示具有正确ID的记录。这样,用户可以编辑的东西就是特定结构的数组。但我不知道如何将 txt 中的数据获取到数组。它是泊西贝或任何替代解决方案。

声明

 #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;

关于c - 如何获取txt中的内容并将其存储在c中的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53552388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com