如何在C中的malloc中存储和存储变量?

时间:2017-05-06 06:34:59

标签: c malloc

这是我的代码:

#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”的变量并在列[0]中存储info [0~3] .str1,在[1]列中存储info [0~3] .year,依此类推。 这是为了以后的工作,即通过调用strcmp函数来选择我想按升序(或降序)排序的列。

1 个答案:

答案 0 :(得分:0)

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

#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