gpt4 book ai didi

c - 如何在 C 中对结构体进行 malloc 和存储变量?

转载 作者:行者123 更新时间:2023-11-30 20:33:53 25 4
gpt4 key购买 nike

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Read
{
char str1[64];
int year;
char type[64];
char str2[64];
} read;

void remove_new_line(char*);

int main(int argc, char *argv[])
{
FILE *fp = NULL;
char line[256];
char* token;
int i = 0;
char input[50];
char command[50];
char val1[30];
char val2[30];
read info[8];

if (argc <= 1)
{
printf("The file does not exist\n");
return 0;
}
fp = fopen(argv[1], "r");
if (fp == NULL)
{
printf("No File Exists\n");
return 0;
}
while (fgets(line, sizeof(line), fp))
{
remove_new_line(line);
token = strtok(line, ",");
strcpy(info[i].str1, token);
token = strtok(NULL, ",");
info[i].year = atoi(token);
token = strtok(NULL, ",");
strcpy(info[i].type, token);
token = strtok(NULL, ",");
strcpy(info[i++].str2, token);
}

while (1)
{
scanf(" %49[^\n]s", input);
fflush(stdin);

command[0] = 0;
val1[0] = 0;
val2[0] = 0;

sscanf(input, "%s, %s, %s", command, val1, val2);

if (strcmp(command, "SORT") == 0)
{
printf("%s | %d | %s | %s\n", info[0].str1, info[0].year, info[0].type, info[0].str2);
}
else if (strcmp(command, "QUIT") == 0)
{
exit(0);
}
break;
}
return 0;
}
void remove_new_line(char* str)
{
char* p;
if (p = strchr(str, '\n'))
{
*p = '\0';
}
}

文本文件是:

Dive,2011,Electronic,Tycho
Portraits,2015,Electronic,Maribou State
Mer De Noms,2000,Hard Rock,A Perfect Circle
Awake,2014,Electronic,Tycho
Epoch,2016,Electronic,Tycho
Farewell,2016,Chamber,Cicada
The Beatles,1968,Rock,The Beatles
Sines,2014,Post-Metal,Jakob
<小时/>

我想做的是创建一个名为“column”的变量,并将info[0~3].str1存储在column[0]中,将info[0~3].year存储在column[1]中,依此类推。这是为了后面的工作,就是通过调用strcmp函数来选择我想要升序(或降序)排序的列。

最佳答案

正如我在评论中指出的,您当前的代码在将数据读入结构方面做得很好。这样,编写可以对任何字段上的结构进行排序的函数就不难了,而不是需要使用以某种方式创建要排序的列的挥手描述。

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

typedef struct Read
{
char str1[64];
int year;
char type[64];
char str2[64];
} read;

static void remove_new_line(char*);

static void print_info(const char *tag, int n, read *info)
{
printf("%s:\n", tag);
for (int j = 0; j < n; j++)
printf("%s | %d | %s | %s\n", info[j].str1, info[j].year, info[j].type, info[j].str2);
}

static int cmp_str1(const void *v1, const void *v2)
{
const read *r1 = v1;
const read *r2 = v2;
return strcmp(r1->str1, r2->str1);
}

static int cmp_type(const void *v1, const void *v2)
{
const read *r1 = v1;
const read *r2 = v2;
return strcmp(r1->type, r2->type);
}

static int cmp_str2(const void *v1, const void *v2)
{
const read *r1 = v1;
const read *r2 = v2;
return strcmp(r1->str2, r2->str2);
}

static int cmp_year(const void *v1, const void *v2)
{
const read *r1 = v1;
const read *r2 = v2;
return (r1->year > r2->year) - (r1->year < r2->year);
}

int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s file\n", argv[0]);
return 1;
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL)
{
fprintf(stderr, "Unable to open file %s for reading\n", argv[1]);
return 1;
}

read info[8];
char line[256];
int i = 0;
while (fgets(line, sizeof(line), fp) != 0 && i < 8)
{
remove_new_line(line);
//printf("In: %s\n", line);
char *token = strtok(line, ",");
//printf("T1: [%s]\n", token);
strcpy(info[i].str1, token);
token = strtok(NULL, ",");
//printf("T2: [%s]\n", token);
info[i].year = atoi(token);
token = strtok(NULL, ",");
//printf("T3: [%s]\n", token);
strcpy(info[i].type, token);
token = strtok(NULL, ",");
//printf("T4: [%s]\n", token);
strcpy(info[i++].str2, token);
}
fclose(fp);

print_info("\nBefore sorting", i, info);
qsort(info, i, sizeof(info[0]), cmp_str1);
print_info("\nSort on str1", i, info);
qsort(info, i, sizeof(info[0]), cmp_year);
print_info("\nSort on year", i, info);
qsort(info, i, sizeof(info[0]), cmp_type);
print_info("\nSort on type", i, info);
qsort(info, i, sizeof(info[0]), cmp_str2);
print_info("\nSort on str2", i, info);

return 0;
}

static void remove_new_line(char* str)
{
char* p;
if ((p = strchr(str, '\n')) != 0)
{
*p = '\0';
}
}

运行示例:

Before sorting:
Dive | 2011 | Electronic | Tycho
Portraits | 2015 | Electronic | Maribou State
Mer De Noms | 2000 | Hard Rock | A Perfect Circle
Awake | 2014 | Electronic | Tycho
Epoch | 2016 | Electronic | Tycho
Farewell | 2016 | Chamber | Cicada
The Beatles | 1968 | Rock | The Beatles
Sines | 2014 | Post-Metal | Jakob

Sort on str1:
Awake | 2014 | Electronic | Tycho
Dive | 2011 | Electronic | Tycho
Epoch | 2016 | Electronic | Tycho
Farewell | 2016 | Chamber | Cicada
Mer De Noms | 2000 | Hard Rock | A Perfect Circle
Portraits | 2015 | Electronic | Maribou State
Sines | 2014 | Post-Metal | Jakob
The Beatles | 1968 | Rock | The Beatles

Sort on year:
The Beatles | 1968 | Rock | The Beatles
Mer De Noms | 2000 | Hard Rock | A Perfect Circle
Dive | 2011 | Electronic | Tycho
Awake | 2014 | Electronic | Tycho
Sines | 2014 | Post-Metal | Jakob
Portraits | 2015 | Electronic | Maribou State
Epoch | 2016 | Electronic | Tycho
Farewell | 2016 | Chamber | Cicada

Sort on type:
Farewell | 2016 | Chamber | Cicada
Epoch | 2016 | Electronic | Tycho
Dive | 2011 | Electronic | Tycho
Awake | 2014 | Electronic | Tycho
Portraits | 2015 | Electronic | Maribou State
Mer De Noms | 2000 | Hard Rock | A Perfect Circle
Sines | 2014 | Post-Metal | Jakob
The Beatles | 1968 | Rock | The Beatles

Sort on str2:
Mer De Noms | 2000 | Hard Rock | A Perfect Circle
Farewell | 2016 | Chamber | Cicada
Sines | 2014 | Post-Metal | Jakob
Portraits | 2015 | Electronic | Maribou State
The Beatles | 1968 | Rock | The Beatles
Awake | 2014 | Electronic | Tycho
Dive | 2011 | Electronic | Tycho
Epoch | 2016 | Electronic | Tycho

关于c - 如何在 C 中对结构体进行 malloc 和存储变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43817510/

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