问题:在C中将标准输入(stdin)读取为结构化列表时出现问题。

huangapple go评论69阅读模式
英文:

Problem reading a stdin as file in a structured list in C

问题

以下是您要翻译的内容:

基本上我想要的是读取一个文件,在这种情况下,我正在使用stdin作为我的文件 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;
}

但是程序似乎无法工作,有什么帮助吗?

英文:

Basicaly what i want is to read a file in these case im using the stdin as my file FILE *f=stdin;

the files contains something like these

2019 - Frog and Mouse
1982 - Water and gelly
3025 - Sugar
...

I want to be able to read and print the names and the total number of characters of the names

so far i have created a structure and a list

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

then i created the functions to read the list and show the results and also ree the 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);
		}
}

than my main

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

but the program wont seems to work, any help?

答案1

得分: 0

你的代码中有相当多的错误。我已经尝试修复,尽量不对原始代码进行太多修改。请参考注释以获取详细信息。

typedef struct struct_data_uc{
    int uc_number;   // 从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;

// 分配新的L_uc_data并插入列表
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;
        // 需要在这里使用strdup来分配内存并复制
        new->uc_data.uc_name = strdup(name);
        new->next = list;
        return new;
    }
    return list;
}

// 释放整个列表
void UC_free(L_uc_data *list)
{
    while (list) {
        L_uc_data *aux = list->next;
        // 释放strdup分配的内存
        free(list->uc_data.uc_name);
        free(list);
        list = aux;
    }
}

// 读取整个文件并返回新的列表
L_uc_data * UC_read(FILE *f)
{
    char line[MAXSTR];
    L_uc_data *the_list = NULL;
    // 使用fgets获取整行,然后使用sscanf进行解析
    while (fgets(line, MAXSTR, f)) {
        int number;
        char name[MAXSTR];
        // 记得检查sscanf的返回值
         if (2 == sscanf(line, "%d - %[^\n]", &number, name)) {
            // 添加到列表
            the_list = UC_add(the_list, number, name);
        }
    }
    return the_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;
}

希望这能帮助你修复代码中的问题。

英文:

There are quite a few errors in your code. I've tried to fix without modifying the original code too much. See the comments for details.

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

答案2

得分: 0

你至少犯了两个错误。首先,你将stdin作为参数传递给UC_Show,在其中尝试写入它。其次,你没有检查printf返回的值,几乎肯定会指示错误并将errno设置为EBADF,以告诉你确切的错误信息。你不能向stdin写入。

英文:

You're making (at least) 2 mistakes. First, you are passing stdin as the argument to UC_Show, where an attempt is made to write to it. 2nd, you are not checking the value returned by printf, which is almost certainly going to indicate an error and set errno to EBADF to tell you exactly what the error is. You cannot write to stdin.

huangapple
  • 本文由 发表于 2020年1月4日 01:01:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定