Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我的记忆力有些问题_C_Arrays_Pointers_Struct - Fatal编程技术网

C 我的记忆力有些问题

C 我的记忆力有些问题,c,arrays,pointers,struct,C,Arrays,Pointers,Struct,大家好,我的英语很抱歉! 首先,我可以在这个论坛上寻求帮助,以防我弄不清楚我的bug吗 我自己的问题 首先,我有一个文件,其中包含一些如下结构的日志: 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.

大家好,我的英语很抱歉! 首先,我可以在这个论坛上寻求帮助,以防我弄不清楚我的bug吗 我自己的问题

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

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
因此,我设法将每一行传递给一个日志结构,该结构如下所示:

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已触发断点
有人能帮我吗?请

我已经更改了代码,现在应该可以用了!请查看评论以查看已更改的内容

#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;
}

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

#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;
}

*logs+i*sizeoflog\t是不必要的复杂,也是错误的。就像使用数组日志一样使用它[i]=它也不起作用,我得到了相同的错误,但你是对的。这真的很简单,ploglog=plogmallocsizeoflog;也应该是plog log=mallocsizeof*log;我必须为所有结构分配内存,所以它可能是sizeoflog\t我猜,不?sizeof*log和sizeoflog\t的计算结果是相同的,选择您认为可读性更好的一个*logs+I*sizeoflog\t是不必要的复杂,也是错误的。就像使用数组日志一样使用它[i]=它也不起作用,我得到了相同的错误,但你是对的。这真的很简单,ploglog=plogmallocsizeoflog;也应该是plog log=mallocsizeof*log;我必须为所有结构分配内存,所以它可能是sizeoflog\t我猜,不是吗?sizeof*log和sizeoflog\t的计算结果相同,请选择您认为可读性更好的一个