2017-05-06 57 views
-2

这里是我的代码:我如何malloc和存储在C中的结构变量?

#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 

我想要做的就是让一个名为“列”和存储信息[0变量〜 3] .str1列[0],info [0〜3] .year列[1]等等。 这是为了后面的工作,它是通过调用strcmp函数来选择要按升序(或降序)排序的列。

+1

['fflush(标准输入);'是不确定的行为,不这样做(http://stackoverflow.com/a/38325926/2173917)。 –

+0

如果你的命令的'input'是逗号分隔的,'sscanf'的'%s' - >'%[^,],' – BLUEPIXY

+0

我应该如何存储info [i] .str1,info [i]年中的年份等..? 我应该使用3D malloc吗? –

回答

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 
+0

哇....非常感谢你! –