所以,我已经在搜索这个网站,看看是否有类似我的问题,我空手而归。这是设置。我使用我的程序写入文件,然后将内容存储在记录中(使用数组)。问题是我似乎无法找到一种方法来搜索特定代码的记录数组并操纵链接到该代码的任何内容。这是我的程序片段:
我想知道如何正确执行案例5.
case 4: ;
FILE *kfile ;
//opens the file called kfile found from the path below.
kfile= fopen("Stock.txt","w");
//prompts the programmer to enter a command to end the input of records.
printf("CTRL+Z' to end input\n");
printf("===========================================");
printf("\nUSE A SINGLE APACE BETWEEN EACH WORD/NUMBER");
printf("\n===========================================\n\n");
//while it is not the end of file, programmer still enters records of persons.
printf("Enter Code(MAX 3 letters), Item Name, Quantity, Price: \n");
scanf("%s %s %d %d", filecode, itemname, &quantity, &fileprice);
while (!feof(stdin) ){
fprintf(kfile,"%s\t%s \t%d\t%d\n", filecode, itemname, quantity, fileprice);
//prints to the file
printf("Enter Code(MAX 3 letters), Item Name, Quantity, Price: \n");
scanf("%s %s %d %d", filecode, itemname, &quantity, &fileprice);
}
fclose(kfile);
break;
case 5: ;
FILE *mfile;
if ((mfile = fopen("Stock.txt","r")) == NULL) {
perror("Error while opening file");
}else {
while (!feof(mfile)) {
fscanf(mfile, "%s\t%s \t%d\t%d", filecode, itemname, &quantity, &fileprice);
array[x].itemname1 = strdup(itemname);
array[x].code1 = strdup(filecode);
array[x].quantity1 = quantity;
array[x].price1 = fileprice;
}
fclose(mfile);
}
printf("Please enter Code: ");
scanf("%s", &codenew);
printf("\n");
printf("\nCode\tItem Name\tQuantity\tPrice\n");
for (x = 1; x <= 100; x++) {
if (strcmp(array[x].code1, codenew)==0) {
// print the record
printf("%s\t%s \t%d\t\t%d\n", array[x].code1,array[x].itemname1, array[x].quantity1,array[x].price1);
}
}
break;
答案 0 :(得分:1)
请注意,case
标签后面的空语句是常规且不必要的(但技术上并非错误,除非它表明有人对C不熟悉)。您可以编写case 5
的代码,以便它只调用一个函数。
case 5:
read_and_match("Stock.txt");
break;
虽然如果文件名本身在变量中会更好。我假设数据存储在结构类型中,例如:
struct Item
{
char *itemname1;
char *code1;
int quantity1;
int price1;
};
该功能本身如下:
void read_and_match(const char *file)
{
FILE *mfile;
if ((mfile = fopen(file, "r")) == NULL)
{
fprintf(stderr, "Error while opening file %s (%d: %s)\n",
file, errno, strerror(errno));
return;
}
char filecode[40];
char itemname[40];
int quantity;
int fileprice;
int num_items;
struct Item array[100];
for (num_items = 0; num_items < 100; num_items++)
{
if (fscanf(mfile, "%39s %39s %d %d",
filecode, itemname, &quantity, &fileprice) != 4)
break;
array[num_items].itemname1 = strdup(itemname);
array[num_items].code1 = strdup(filecode);
array[num_items].quantity1 = quantity;
array[num_items].price1 = fileprice;
}
fclose(mfile);
printf("Please enter Code: ");
char codenew[40];
if (scanf("%39s", codenew) != 1)
return;
printf("\n");
printf("\nCode\tItem Name\tQuantity\tPrice\n");
for (int x = 0; x < num_items; x++)
{
if (strcmp(array[x].code1, codenew) == 0)
{
// print the record
printf("%s\t%s\t%d\t\t%d\n",
array[x].code1, array[x].itemname1, array[x].quantity1, array[x].price1);
}
}
}
坦率地说,这里有两个函数 - 一个读函数和一个搜索函数 - 在这种情况下,你最终得到:
case 5:
{
struct Item array[100];
int n_items = read_items("Stock.txt", array, 100);
if (n_items > 0)
search_list(array, n_items);
}
break;
功能变为:
int read_items(const char *file, struct Item *array, int max_items)
{
FILE *mfile;
if ((mfile = fopen(file, "r")) == NULL)
{
fprintf(stderr, "Error while opening file %s (%d: %s)\n",
file, errno, strerror(errno));
return 0;
}
char filecode[40];
char itemname[40];
int quantity;
int fileprice;
int num_items;
for (num_items = 0; num_items < max_items; num_items++)
{
if (fscanf(mfile, "%39s %39s %d %d",
filecode, itemname, &quantity, &fileprice) != 4)
break;
array[num_items].itemname1 = strdup(itemname);
array[num_items].code1 = strdup(filecode);
array[num_items].quantity1 = quantity;
array[num_items].price1 = fileprice;
}
fclose(mfile);
return num_items;
}
void search_list(struct Item *array, int num_items)
{
printf("Please enter Code: ");
char codenew[40];
if (scanf("%39s", codenew) != 1)
return;
printf("\n");
printf("\nCode\tItem Name\tQuantity\tPrice\n");
for (int x = 0; x < num_items; x++)
{
if (strcmp(array[x].code1, codenew) == 0)
{
// print the record
printf("%s\t%s\t%d\t\t%d\n",
array[x].code1, array[x].itemname1, array[x].quantity1, array[x].price1);
}
}
}
表格格式也需要修复,但这是其他人的练习。