我的记忆有些问题

时间:2019-12-28 16:16:55

标签: c arrays pointers struct

大家好,对不起我的英语! 首先,我可以在这个论坛上向我的错误寻求帮助,以防万一我找不到答案的时候 是我自己的问题吗?

因此,首先,我有一个文件,其中包含一些结构如下的日志:

2015-07-22 09:07:00 1346144 13.02 25.36 6.606 4.012 0.845 26.3
2015-07-22 09:08:00 1346145 13.02 25.38 6.656 4.057 0.848 26.4
2015-07-22 09:09:00 1346146 13.02 25.43 6.667 4.086 0.849 26.45
2015-07-22 09:10:00 1346147 13.02 25.46 6.663 4.109 0.851 26.44
2015-07-22 09:11:00 1346148 13.02 25.5 6.657 4.131 0.856 26.51
2015-07-22 09:12:00 1346149 13.02 25.53 6.693 4.17 0.862 26.53
2015-07-22 09:13:00 1346150 13.02 25.56 6.723 4.205 0.865 26.71
2015-07-22 09:14:00 1346151 13.02 25.6 6.734 4.233 0.866 26.64

所以我设法将每一行传递给一个看起来像这样的Log结构:

typedef struct Log {
    char    field1[11]  ;
    char    field2[9]   ;
    int     field3      ;
    float   field4      ;
    float   field5      ;
    float   field6      ;
    float   field7      ;
    float   field8      ;
    float   field9      ;
}log_t, *plog;

我设法通过了一条线,但是当我尝试通过更多条线时遇到了麻烦。 我尝试使指针指向此结构,因为我知道数组是指针。 这是我的代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

typedef struct Log {
    char    field1[11]  ;
    char    field2[9]   ;
    int     field3      ;
    float   field4      ;
    float   field5      ;
    float   field6      ;
    float   field7      ;
    float   field8      ;
    float   field9      ;
}log_t, *plog;

FILE* OpenFile(const char* path) 
{
    FILE* file = NULL;
    file = fopen(path, "r");
    if (file == NULL)
        printf("File cant be opened\n");
    else
        printf("File is opened\n");
    return file;
}

plog CreateLog(FILE* file)
{
    plog log = (plog)malloc(sizeof(log));
    fscanf(file, "%s", log->field1);
    fscanf(file, "%s", log->field2);
    fscanf(file, "%d", &(log->field3));
    fscanf(file, "%f", &(log->field4));
    fscanf(file, "%f", &(log->field5));
    fscanf(file, "%f", &(log->field6));
    fscanf(file, "%f", &(log->field7));
    fscanf(file, "%f", &(log->field8));
    fscanf(file, "%f", &(log->field9));
    return log;
}

void PrintLog(log_t log)
{
    printf("%s", log.field1);
    printf("%s", log.field2);
    printf("%d", log.field3);
    printf("%f", log.field4);
    printf("%f", log.field5);
    printf("%f", log.field6);
    printf("%f", log.field7);
    printf("%f", log.field8);
    printf("%f", log.field9);
}

int main()
{
    FILE* file;
    file = OpenFile("DataMeteoE4.txt");
    //plog log = CreateLog(file);
    plog* logs;
    logs = (plog*)malloc(sizeof(log_t) * 10);
    for (int i = 0; i < 10; i++)
    {
        logs[i] = CreateLog(file);
    }
    //PrintLog(*log);
    fclose(file);
    return 0;
}

它崩溃,但异常:“ .exe触发了断点” 谁能帮我

1 个答案:

答案 0 :(得分:-2)

我更改了代码,该代码现在可以正常工作了!请查看评论以查看已更改的内容。

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

//////////////////////////////////////////
// I don't know what was the use of plog,
// so removed it.
//////////////////////////////////////////
typedef struct Log {
    char    field1[11]  ;
    char    field2[9]   ;
    int     field3      ;
    float   field4      ;
    float   field5      ;
    float   field6      ;
    float   field7      ;
    float   field8      ;
    float   field9      ;
} log_t;

FILE* OpenFile(const char* path) 
{
    FILE* file = fopen(path, "r");
    return file;
}

log_t CreateLog(FILE* file)
{
    //////////////////////////////////////////
    // Change in format for reading string
    // Reading into local variable log and returning it
    //////////////////////////////////////////
    log_t log;
    memset(&log, 0, sizeof(log_t));
    fscanf(file, "%11s", log.field1);
    fscanf(file, "%8s", log.field2);
    fscanf(file, "%d", &(log.field3));
    fscanf(file, "%f", &(log.field4));
    fscanf(file, "%f", &(log.field5));
    fscanf(file, "%f", &(log.field6));
    fscanf(file, "%f", &(log.field7));
    fscanf(file, "%f", &(log.field8));
    fscanf(file, "%f", &(log.field9));
    return log;
}

void PrintLog(log_t log)
{
    //////////////////////////////////////////
    // Added spaces in between fields
    //////////////////////////////////////////
    printf("%.11s ", log.field1);
    printf("%.8s ", log.field2);
    printf("%d ", log.field3);
    printf("%f ", log.field4);
    printf("%f ", log.field5);
    printf("%f ", log.field6);
    printf("%f ", log.field7);
    printf("%f ", log.field8);
    printf("%f\n", log.field9);
}

int main()
{
    FILE* file = OpenFile("DataMeteoE4.txt");
    //////////////////////////////////////////
    // Removed this from OpenFile function and
    // put it here. Also exit from program if
    // open fails.
    //////////////////////////////////////////
    if (file == NULL) {
        printf("File can't be opened\n");
        return 1;
    }
    else
        printf("File is opened\n");

    //////////////////////////////////////////
    // Change of array size from 10 to 8 in 
    // both malloc and loop condition, since 
    // there were 8 lines in sample input
    //////////////////////////////////////////
    log_t* logs = (log_t*) malloc(sizeof(log_t) * 8);
    for (int i = 0; i < 8; i++)
    {
        logs[i] = CreateLog(file);
        PrintLog(logs[i]);
    }
    fclose(file);
    return 0;
}
相关问题