gpt4 book ai didi

C编程: Grouping multiple strings and searching for it using an Unique key

转载 作者:行者123 更新时间:2023-11-30 20:32:07 24 4
gpt4 key购买 nike

假设您正在输入学生的个人信息,并为每个学生创建一个唯一的 ID 号。我遇到的问题是将这些字符串分组并将它们存储在一起,以便可以通过 ID 访问它。

我尝试使用内存分配,但是没有成功。

我对 C 还很陌生,所以我不太确定该怎么做。这是我使用的:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct {
int LogID;
char firstname[20];
char lastname[20];
int mark;
char *subjects[100];
} student;

int reg(void);

int main(void) {
reg();
return 0;
}

//Registration function
int reg(void) {
int choice,shift,found,compare, nosub, y, sub, subcount;
char studentname[20];
FILE *fp;
FILE *fp2;

printf("Enter Student Details:\n\nStudent ID: ");
scanf("%d",&student.LogID);
printf("Name: ");
scanf("%s",student.firstname);
printf("Surname: ");
scanf("%s",student.lastname);
printf("How many subjects does the student take? ");
scanf("%d", &nosub);

//Opens a text file and prints the student's ID and firstname
fp2=fopen("subjects.txt","a+");
fprintf(fp2, "\n\n%d\t%s", student.LogID, student.firstname);

int i = 1;
size_t malloc_size = 100; //Allocating memory size to store subjects student is taking
for(i = 0; i < nosub; i++) {
student.subjects[i] = malloc(malloc_size * sizeof(char));
printf("Please enter the subject :\n(1)Mathematics\n(2)English\n(3) Social Studies\n(4)Science\n");
scanf("%d", &sub);

switch (sub) {
case (1) :
printf("Mathematics\n");
break;
case (2) :
printf("English\n");
break;
case (3) :
printf("Social Studies\n");
break;
case (4) :
printf("Science\n");
break;
}
}
for(i = 0; i < nosub; i++) { //Prints subjects to file, but it doesn't work...
fprintf(fp2,"%s\n", student.subjects[i]);
}
fclose(fp2);
free(student.subjects[i]);
student.subjects[i] = NULL;

fp=fopen("studentfile.txt","a+");
//Prints certain student Information into another file...


fprintf(fp,"\n%d\t%s\t%s\t",student.LogID,student.firstname,
student.lastname);
fclose(fp);
printf("Registration has been successful\n");

getchar();
return 0;
}

最佳答案

我注意到了很多问题,我将解决其中的一些问题,从我建议这样定义的结构开始:

struct studentpack {
int LogID;
char firstname[20];
char lastname[20];
int mark;
char *subjects[100];
};

在您的主要功能下,您可以定义要填写的学生条目。

struct studentpack student;

它将在您的代码中反复使用,就像您在示例中所做的那样,用于从用户输入进行填充。

在开关中,您实际上不需要在案例编号周围添加任何括号。

除了每种情况下的 printf 语句之外,还可以尝试如下操作:

case 1:
printf("%s\n", "Mathematics");
sprintf(student.subjects + i, "%s", "Mathematics");
break;

这将填充您之前分配的内存主题区域。

最后,为了将 ID 分组在一起,我建议从这样的开始:

#define MAXGROUP 1000

struct studentpack *group = calloc(MAXGROUP, sizeof(struct studentpack));

int studentno = 0;

然后,在将主题打印到文件后(由于 sprintf 更改,首先测试它是否正常工作),您将像这样分配给组:

if (studentno < MAXGROUP) {
memcpy(group+studentno, &student, sizeof(struct studentpack));
studentno++;
}

以上内容将存储学生。接下来,您可能需要迭代组变量,访问 group[0].LogID、group[1].LogID 等(一直到最大 1000),只是为了确认所有数据都在内存。

例如,您可以编写一个函数,逐步查找特定 LogID,然后调用包含 printf 语句的 show 或 display 函数来查看屏幕上的所有值。

关于C编程: Grouping multiple strings and searching for it using an Unique key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48433427/

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