strcpy:我怎么能不复制\ n

时间:2016-12-01 18:43:24

标签: c strcpy

  var values = [];
  for (var i = 0; i < data.length; i++) {
    var item = {};
    item.y = data[i].power;
    item.x = new Date(data[i]['date']).getTime();
    values.push(item);
  }   

1 个答案:

答案 0 :(得分:1)

  • 从长度中推断1

您的代码有多个简单而明显的改进

  1. 在使用strstr()之前,您应该始终检查返回值。

  2. strlen("SERVERPORT = ")是一种非常丑陋的写作方式12,效率也很低。

  3. 您应该使用更多的空格来使代码可读。

  4. 不要转换malloc()的返回值,只会让它更难阅读,如果您忘记包含 stdlib.h ,可能会隐藏错误。

  5. 在解除引用指针之前,始终检查malloc()是否返回NULL

  6. 拆分=处的每一行,从2个结果值中删除所有周围的空格,然后检查它是哪个变量并指定相应的值。

    因为如果SERVERPORT=1234例如你的代码会失败,即使它很丑陋且=运算符周围的空格更好,两者都应该是有效的,除非你明确地想要这些空格。

    此外,通过移除周围的空白区域,您可以确保'\n'读取的任何getline()都将从该值中删除。

  7. 这是一个刚刚写的快速API,告诉你我将如何做,当然每个人都有自己的品味和做事的方法,但我希望它有助于搞清楚你的​​错误

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    struct KeyValue {
        char *key;
        char *value;
    };
    
    struct KeyFile {
        size_t size;
        struct KeyValue *entries;
        size_t count;
    };
    
    static struct KeyFile *
    keyfile_new(void)
    {
        struct KeyFile *kf;
    
        kf = malloc(sizeof(*kf));
        if (kf == NULL)
            return NULL;
        kf->entries = malloc(10 * sizeof(*kf->entries));
        if (kf->entries == NULL) {
            kf->size = 0;
        } else {
            kf->size = 10;
        }
        kf->count = 0;
        return kf;
    }
    
    static int
    keyfile_add_value(struct KeyFile *kf, const char *const key, const char *const value)
    {
        struct KeyValue *entry;
        if (kf->count + 1 >= kf->size) {
            void *pointer;
            pointer = realloc(kf->entries, (kf->size + 10) * sizeof(*kf->entries));
            if (pointer == NULL)
                return -1;
            kf->entries = pointer;
            kf->size += 10;
        }
        entry = &kf->entries[kf->count++];
    
        entry->key = strdup(key);
        entry->value = strdup(value);
    
        return 0;
    }
    
    static void
    keyfile_free(struct KeyFile *kf) 
    {
        for (size_t i = 0 ; i < kf->count ; ++i) {
            struct KeyValue *entry;
    
            entry = &kf->entries[i];
    
            free(entry->key);
            free(entry->value);
        }
        free(kf->entries);
        free(kf);
    }
    
    static struct KeyFile *
    keyfile_read(const char *const path)
    {
        FILE *file;
        struct KeyFile *kf;
        size_t length;
        char *line;
    
        line = NULL;
        length = 0;
        file = fopen(path, "r");
        if (file == NULL)
            return NULL;
        kf = keyfile_new();
        if (kf == NULL)
            return NULL;
        while (getline(&line, &length, file) > 0) {
            char *op;
            char *key;
            char *value;
            op = strchr(line, '=');
            if (op == NULL) {
                fprintf(stderr, "malformed line!\n");
            } else {
                *op = '\0';
                key = line;
                while (isspace((unsigned char) *key) != 0)
                    ++key;
                value = op + 1;
    
                op -= 1;
                while (isspace((unsigned char) *op) != 0)
                    *(op--) = '\0';
                while (isspace((unsigned char) *value) != 0)
                    value += 1;
                op = value + strlen(value) - 1;
                while (isspace((unsigned char) *op) != 0)
                    *(op--) = '\0';
                if (keyfile_add_value(kf, key, value) != 0)
                    goto error;
            }
        }
        fclose(file);
        free(line);
    
        return kf;
    error:
        keyfile_free(kf);
    
        fclose(file);
        free(line);    
    
        return NULL;
    }
    
    static void
    keyfile_display(const struct KeyFile *const kf)
    {
        for (size_t i = 0 ; i < kf->count ; ++i) {
            const struct KeyValue *entry;
    
            entry = &kf->entries[i];
            fprintf(stdout, "/%s/ => /%s/\n", entry->key, entry->value);
        }
    }
    

    您可以对此进行改进以添加查找功能,以便在设置文件中查找特定值。你可以把它变成一个独立的库,也可以在很多项目中使用它。

相关问题