从链接列表保存到文件并加载回来

时间:2017-05-19 19:05:27

标签: c file linked-list

我无法从文件加载到链接列表,一整天都在尝试

首先,这是我的结构

data just_female;
  set have;
  where sex = 'F';
  rename count = count_female;
run;

data just_male;
  set have;
  where sex = 'M';
  rename count = count_male;
run;

data want;
  merge
    just_female
    just_male
  ;
  by
    group
    replicate
  ;

  keep
    group
    replicate
    count_female
    count_male
  ;
run;

这是我的保存功能,我认为它的工作正常,因为文件已创建且内容已存在。

typedef struct Sensor {
    int id;
    int intervalo;
    char local[30];
    char tipo[30];
    //bool active;
    int active;
    struct Sensor* anterior;
    struct Sensor* proximo;
} Sensor;

现在,无论我读到什么,我似乎无法使其工作是加载功能。

继承人我的atm

void gravaLista(Sensor* l) {
    FILE *ficheiro;
    Sensor* temp = l;
    ficheiro = fopen("sensores.txt", "r+t");

    if (ficheiro == NULL) {
        ficheiro = fopen("sensores.txt", "w+t");
    }

    while (temp != NULL) {
        fprintf(ficheiro, "%d%d%d%30s%30s", temp->id, temp->intervalo, temp->active,
        temp->local, temp->tipo);
        temp = temp->proximo;
    }

    fclose(ficheiro);
}

辅助函数在加载函数之外工作正常,但是当我尝试在加载后打印列表时,没有任何内容被打印出来。我错过了什么?

编辑:生病也只是发布帮助函数

int CarregaTodos(Sensor** l) {
    Sensor sens;
    FILE *ficheiro;
    int i = 0;

    ficheiro = fopen("sensores.txt", "r+t");

    if (ficheiro == NULL) {
        printf("no file\n", "sensores.txt");
        return i;
    }

    rewind(ficheiro);
    while (fscanf(ficheiro, "%d%d%d%30s%30s", &sens.id, &sens.intervalo, &sens.active,
            &sens.local, &sens.tipo) == 5) {

            //novo() function returns a pointer to a new element and insereSensor adds the new element to the last position of the list
            insereSensorFim(&l, novo(sens.id, sens.intervalo, sens.local, sens.tipo));                              //this function inserts the new element at the end of the list
    }

    fclose(ficheiro);
    return i;
}

edit2:现在修复了,感谢所有发表评论的人,您可以阅读所有评论或只是https://stackoverflow.com/a/44078897/8038340

3 个答案:

答案 0 :(得分:1)

正确使用printf()scanf()非常困难。可以用它们做各种各样的魔术,但你需要知道它们是如何工作才能发挥魔力。

在示例代码中,通过在输出中不包括记录分隔符,您将使自己的生活变得更加困难。换行符是常规和最简单的分隔符,但您可以根据需要选择其他分隔符,也可以不选择分隔符。但是,如果您选择无分隔符,则必须知道有关问题中未提供的数据的信息。如果字符串从不包含空格,则格式化程度可能不那么严格。但是你必须有一些方法可以知道一个号码的结束位置和下一个号码的开头 - 你不能简单地将所有数字按照样本printf()格式一起刷,除非它们全部为负数,或者您在正数(%+d)上添加一个加号。必须有一些方法告诉scanf()何时停止阅读一个并开始下一个数字。

此代码详细说明了我在众多评论中所写的内容。输出格式使用固定宽度字段;这使得阅读起来更容易。它不假设字符串中没有空格,因此它使用%29c读取29个字符,并添加空终止符并通过strip_blanks()删除尾随空格。它包括打印列表的代码;它使用该代码。

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

typedef struct Sensor
{
    int id;
    int intervalo;
    char local[30];
    char tipo[30];
    int active;
    struct Sensor *anterior;
    struct Sensor *proximo;
} Sensor;

static void insereSensorFim(Sensor **Lista, Sensor *novo);
static Sensor *novoSensor(int id, int tempo, char *l, char *t);

static const char *outfile = "sensores.txt";

static
void gravaLista(Sensor *l)
{
    FILE *ficheiro = fopen(outfile, "w");
    if (ficheiro == NULL)
    {
        fprintf(stderr, "Failed to open file '%s' for writing\n", outfile);
        exit(1);
    }

    Sensor *temp = l;
    while (temp != NULL)
    {
        fprintf(ficheiro, "%11d%11d%11d%-29.29s%-29.29s", temp->id, temp->intervalo, temp->active,
                temp->local, temp->tipo);
        temp = temp->proximo;
    }

    fclose(ficheiro);
}

/* Strip trailing blanks and null terminate string */
static inline void strip_blanks(char *data, size_t size)
{
    assert(size > 0);
    size_t offset = size - 1;
    data[offset--] = '\0';
    while (offset > 0 && data[offset] == ' ')
        data[offset--] = '\0';
}

static
int CarregaTodos(Sensor **l)
{
    Sensor sens;
    FILE *ficheiro;
    int i = 0;

    ficheiro = fopen(outfile, "rt");

    if (ficheiro == NULL)
    {
        fprintf(stderr, "Failed to open file '%s'\n", outfile);
        exit(1);
    }

    while (fscanf(ficheiro, "%11d%11d%11d%29c%29c", &sens.id, &sens.intervalo, &sens.active,
                  sens.local, sens.tipo) == 5)
    {
        strip_blanks(sens.local, sizeof(sens.local));
        strip_blanks(sens.tipo, sizeof(sens.tipo));
        insereSensorFim(l, novoSensor(sens.id, sens.intervalo, sens.local, sens.tipo));
    }

    fclose(ficheiro);
    return i;
}

static inline void str_copy(char *dst, const char *src, size_t size)
{
    assert(size > 0);
    strncpy(dst, src, size - 1);
    dst[size - 1] = '\0';
}

static
Sensor *novoSensor(int id, int tempo, char *l, char *t)
{
    Sensor *novoSensor = (Sensor *)malloc(sizeof(struct Sensor));
    if (novoSensor == NULL)
    {
        fprintf(stderr, "Failed to allocate %zu bytes memory\n", sizeof(struct Sensor));
        exit(1);
    }

    novoSensor->id = id;
    novoSensor->intervalo = tempo;
    str_copy(novoSensor->local, l, sizeof(novoSensor->local));
    str_copy(novoSensor->tipo, t, sizeof(novoSensor->tipo));
    novoSensor->active = 1;
    novoSensor->anterior = NULL;
    novoSensor->proximo = NULL;

    return novoSensor;
}

static
void insereSensorFim(Sensor **Lista, Sensor *novo)
{
    Sensor *atual = *Lista;

    if ((*Lista == NULL))
        *Lista = novo;
    else
    {
        while (atual->proximo != NULL)
            atual = atual->proximo;
        atual->proximo = novo;
        novo->anterior = atual;
    }
}

static void print_sensor(Sensor *sensor)
{
    printf("%5d %5d %1d [%-29s] [%-29s]\n", sensor->id, sensor->intervalo,
           sensor->active, sensor->local, sensor->tipo);
}

static void print_sensor_list(const char *tag, Sensor *list)
{
    printf("%s:\n", tag);
    while (list != 0)
    {
        print_sensor(list);
        list = list->proximo;
    }
}

static void free_sensor_list(Sensor *list)
{
    while (list != 0)
    {
        Sensor *next = list->proximo;
        free(list);
        list = next;
    }
}

int main(void)
{
    Sensor *list = 0;

    print_sensor_list("Empty", list);

    insereSensorFim(&list, novoSensor(10231, 23, "abc123-bothersome",  "d92-x41-ccj-92436x"));
    insereSensorFim(&list, novoSensor(20920, 25, "def456-troublesome", "e81-p42-ggk-81366x"));
    insereSensorFim(&list, novoSensor(30476, 83, "ghi789-wearisome",   "f70-q43-omm-70296x"));

    print_sensor_list("After insertion", list);
    gravaLista(list);
    free_sensor_list(list);
    list = 0;
    print_sensor_list("Emptied", list);

    CarregaTodos(&list);
    print_sensor_list("After rereading", list);

    insereSensorFim(&list, novoSensor(231,  325, "jkl012 blank laden stream",       "minimum mess or cleaning"));
    insereSensorFim(&list, novoSensor(6812, -11, "mno345 longer than was expected", "maximum type of untidiness at work"));
    print_sensor_list("After extending", list);

    free_sensor_list(list);

    return 0;
}

运行时,它会产生输出:

Empty:
After insertion:
10231    23 1 [abc123-bothersome            ] [d92-x41-ccj-92436x           ]
20920    25 1 [def456-troublesome           ] [e81-p42-ggk-81366x           ]
30476    83 1 [ghi789-wearisome             ] [f70-q43-omm-70296x           ]
Emptied:
After rereading:
10231    23 1 [abc123-bothersome            ] [d92-x41-ccj-92436x           ]
20920    25 1 [def456-troublesome           ] [e81-p42-ggk-81366x           ]
30476    83 1 [ghi789-wearisome             ] [f70-q43-omm-70296x           ]
After extending:
10231    23 1 [abc123-bothersome            ] [d92-x41-ccj-92436x           ]
20920    25 1 [def456-troublesome           ] [e81-p42-ggk-81366x           ]
30476    83 1 [ghi789-wearisome             ] [f70-q43-omm-70296x           ]
  231   325 1 [jkl012 blank laden stream    ] [minimum mess or cleaning     ]
 6812   -11 1 [mno345 longer than was expect] [maximum type of untidiness at]

输出文件sensores.txt如下所示:

      10231         23          1abc123-bothersome            d92-x41-ccj-92436x                 20920         25          1def456-troublesome           e81-p42-ggk-81366x                 30476         83          1ghi789-wearisome             f70-q43-omm-70296x           

分成记录时,即:

      10231         23          1abc123-bothersome            d92-x41-ccj-92436x           
      20920         25          1def456-troublesome           e81-p42-ggk-81366x           
      30476         83          1ghi789-wearisome             f70-q43-omm-70296x           

11的整数宽度允许前两列中的每一列都有负32位数。如果您知道数字较小,则可以减少使用的空间。在scanf()中,您可以省略整数字段的长度;它会工作相同,因为数字格式自动跳过空格。 printf()可以添加换行符;扫描代码根本不需要更改,因为scanf()在预期数字时(或仅限字符串%c%[…]扫描集时不关心新行) ,%n不要跳过前导空格。

您还可以安排一些不会出现在字符串中的字符(可能是Control-A,'\1')来分隔字符串。然后扫描代码可以查找,你可以有可变长度输出。

留给我自己的设备,我可能会使用带有换行符的换行符的可变长度记录,以及两个字符串的合适字段分隔符,以及不太严格的scanf()格式。我用fgets()或POSIX读取了这些行 getline()然后使用扫描线条 sscanf()。除非你的字符串中有换行符,否则这样可以很好地工作。

正如我最近在另一篇answer中所说的那样 - 轻描淡写:

  

阅读printf()scanf()的POSIX规范以获取完整详细信息。它们在标准C printf()scanf()上有一些(明显标记的)扩展名,但它们同时适用于POSIX和标准C.然后重新阅读它们。并重新阅读它们。每天做一个星期,然后每周做一个月,然后每月做一年,然后每年做一次。它将回报努力。

答案 1 :(得分:0)

在写入文件时你必须将你的整数分开,否则它们看起来就像阅读函数的一个大数字。

您可以尝试将 for(int i = 0; i < threads; i++) { tasks.Add(new Task( () => DownloadFunc(i) )); } WATCH.Reset(); WATCH.Start(); Parallel.ForEach(tasks, t => t.Start()); 替换为fprintf(ficheiro, "%d%d%d%30s%30s", ...);(29而不是30,因为您没有写入终止fprintf(ficheiro, "%d;%d;%d;%29s;%29s", ...);的字符串)然后应该能够使用{{1 }}

编辑:

在编写了较小的测试代码和一些调试之后,我发现如果你想在'\0'的格式中使用fscanf(ficheiro, "%d;%d;%d;%29s;%29s", ...);,那么白色空间就会被剥去字符串的结尾他们%s为你终止,然后这会有效:

fscanf()

此测试程序的输出:

\0

正如本测试程序所写的文件所示,每条记录都写成一行:

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

void test_save(void)
{
    FILE *ficheiro;
    ficheiro = fopen("sensores.txt", "r+t");
    if (ficheiro == NULL) {
        ficheiro = fopen("sensores.txt", "w+t");
    }
        fprintf(ficheiro, "%d;%d;%d;%-29s ;%-29s\n", 12, 138, 131,
        "Local_test", "Tipo_test");
        fprintf(ficheiro, "%d;%d;%d;%-29s ;%-29s\n", 21, 218, 213,
        "Local_test_2", "Second_tipo_test");
    fclose(ficheiro);
}

void test_read(void)
{
    FILE *ficheiro;
    ficheiro = fopen("sensores.txt", "r+t");
    if (ficheiro == NULL) {
        printf("no file %s\n", "sensores.txt");
        return;
    }
    int id, intervalo, active;
    char local[30], tipo[30];

    while (fscanf(ficheiro, "%d;%d;%d;%29s ;%29s\n", &id, &intervalo, &active,
            local, tipo) == 5) {
        printf("id: %d intervalo: %d active: %d\tlocal: [%s]\ttipo: [%s]\n",
                id, intervalo, active, local, tipo);
    }
    fclose(ficheiro);
}

int main(void)
{
    test_save();
    test_read();
}

答案 2 :(得分:0)

  1. fprintf(ficheiro, "%d%d%d%30s%30s" ...我建议你点一个分隔符,说昏迷或#。想象一下,你的id是11,intervalo是10,当保存时,它是1110.你怎么知道,当从文件中读取时,id是11而不是1或111?

  2. insereSensorFim(&l,更改为insereSensorFim(l,

  3. 在insereSensorFim中,您使用while循环来查找尾部,它效率不高。让* Lista始终指向尾部并跳过循环。例如,

  4. 
    
        void insereSensorFim(Sensor** tail, Sensor* novo) {
            if (*tail != NULL)
            {
                (*tail)->proximo = novo;
                novo->anterior = (*tail);
            }
            *tail = nova;
        }