c - 在 C 中的结构化列表中将标准输入作为文件读取时出现问题
问题描述
基本上我想要的是在这种情况下使用标准输入作为我的文件来读取文件FILE *f=stdin;
这些文件包含类似这样的内容
2019 - Frog and Mouse
1982 - Water and gelly
3025 - Sugar
...
我希望能够读取和打印姓名和姓名的总字符数
到目前为止,我已经创建了一个结构和一个列表
typedef struct struct_data_uc{
int *uc_number;
char *uc_name;
} S_data_uc;
typedef struct List_uc_data{
S_data_uc uc_data;
struct List_uc_data *next;
} L_uc_data;
L_uc_data* UC_add(L_uc_data *list, L_uc_data data_uc){
L_uc_data *new;
new=(L_uc_data*)malloc(sizeof(L_uc_data));
if(new!=NULL){
(*new)=data_uc;
new->next=list;
return new;
}
return list;
}
然后我创建了读取列表并显示结果的函数,还创建了列表
void UC_free(L_uc_data *list){
L_uc_data *aux;
while(list!=NULL){
aux=list->next;
free(list);
list=aux;
}
}
void UC_read(L_uc_data *data_uc, FILE *fin, FILE *fout){
char str[MAXSTR];
if(fout!=NULL)
fscanf(fin,"%d - %c",&data_uc->uc_data.uc_number,&data_uc->uc_data.uc_name);
void UC_Show(L_uc_data *data_uc, FILE *fout, int prompt){
if(prompt==0){
fprintf(fout,"%d - %c\n",
data_uc->uc_data.uc_number,
data_uc->uc_data.uc_name);
}else{
fprintf(fout,"%d - %c\n",
data_uc->uc_data.uc_number,
data_uc->uc_data.uc_name);
}
}
比我的主要
int main(){
FILE *f=stdin;
L_uc_data *list=NULL, *i, data_uc;
UC_read(&data_uc, stdin, stdout);
list=UC_add(list,data_uc);
for(i=list;i!=NULL;i=i->next)
UC_Show(i,f,0);
return 0;
}
但该程序似乎无法正常工作,有什么帮助吗?
解决方案
您的代码中有很多错误。我试图在不过多修改原始代码的情况下进行修复。有关详细信息,请参阅评论。
typedef struct struct_data_uc{
int uc_number; // Changed from int *
char *uc_name;
} S_data_uc;
typedef struct List_uc_data{
S_data_uc uc_data;
struct List_uc_data *next;
} L_uc_data;
// Allocate a new L_uc_data and insert into list
L_uc_data* UC_add(L_uc_data *list, int number, const char *name)
{
L_uc_data *new = malloc(sizeof(L_uc_data));
if (new != NULL) {
new->uc_data.uc_number = number;
// Need strdup here to alloc mem and copy
new->uc_data.uc_name = strdup(name);
new->next = list;
return new;
}
return list;
}
// Free the entire list
void UC_free(L_uc_data *list)
{
while (list) {
L_uc_data *aux = list->next;
// Free the mem from strdup
free(list->uc_data.uc_name);
free(list);
list = aux;
}
}
// Reads the entire file and returns a new list
L_uc_data * UC_read(FILE *f)
{
char line[MAXSTR];
L_uc_data *the_list = NULL;
// Using fgets to get the entire line, then sscanf to parse
while (fgets(line, MAXSTR, f)) {
int number;
char name[MAXSTR];
// Remember to check the return from sscanf
if (2 == sscanf(line, "%d - %[^\n]", &number, name)) {
// Add to list
the_list = UC_add(the_list, number, name);
}
}
return the_list;
}
// Print the entire list
void UC_show(L_uc_data *list, FILE *fout)
{
while (list) {
fprintf(fout, "%d - %s\n", list->uc_data.uc_number, list->uc_data.uc_name);
list = list->next;
}
}
int main()
{
L_uc_data *list = UC_read(stdin);
UC_show(list, stdout);
UC_free(list);
return 0;
}
推荐阅读
- python - 连接具有相同和不同列名的 pandas 数据帧列表
- python-3.x - 通过 pip 安装后找不到 pandas
- ajax - ExtJS Ajax 调用失败 404
- c - 使用 getchar() 时进行多次完整性检查,我该如何避免?
- java - UTF-8 编码问题;我存储的内容和在用户界面中看到的内容
- spring - Spring Boot 入门包会影响性能吗?
- python - 我可以使用 Django 将我的 Python 脚本合并到网页上吗?
- javascript - 使用 elem.setAttribute 时图像不会显示
- javascript - chrome.tabs.sendMessage 不能在异步中工作然后运行
- javascript - 比较数组Javascript中索引元素中的数字