我无法更新结构指针中的值

问题描述 投票:0回答:1

这是我的代码

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

/*
    Q: Read a log logfile, and parse each line into its constituent parts.
        Each line contains a timestamp, log level, and message. The timestamp is enclosed in square brackets,
        the log level is a single word, and the message may contain spaces.

    input:  3 => size of log logfile
            [2023-04-23 12:34:56] INFO Server started.
            [2023-04-23 12:35:01] WARNING Request from 192.168.0.1 denied.
            [2023-04-23 12:36:12] ERROR Database connection failed: Connection refused.

    output: Timestamp: 2023-04-23 12:34:56
            Log level: INFO
            Message: Server started.

            Timestamp: 2023-04-23 12:35:01
            Log level: WARNING
            Message: Request from 192.168.0.1 denied.

            Timestamp: 2023-04-23 12:36:12
            Log level: ERROR
            Message: Database connection failed: Connection refused.
*/
typedef struct logfile
{
    char *timestamp;
    char *logLevel;
    char *message;
    struct logfile *next;
} logfile;

logfile *insertLoglogfile(logfile *info, char *timestamp, char *logLevel, char *message)
{
    logfile *temp = malloc(sizeof(logfile));
    temp->timestamp = timestamp;
    temp->logLevel = logLevel;
    temp->message = message;
    temp->next = NULL;

    if (info == NULL)
    {
        info = temp;
        return info;
    }

    logfile *current = info;
    while (current->next != NULL)
    {
        current = current->next;
    }
    current->next = temp;
    return info;
}

void displayLoglogfile(logfile *info)
{
    logfile *current = info;
    while (current != NULL)
    {
        printf("\nTimestamp: %s\nLog level: %s\nMessage: %s\n", current->timestamp, current->logLevel, current->message);
        current = current->next;
    }
}

int main()
{
    logfile *info = NULL;
    char timestamp[20];
    char logLevel[20];
    char message[100];
    int size;
    scanf("%d", &size);
    for (int i = 0; i < size; i++)
    {
        scanf("\n");
        scanf("[%[^\]] %*c %s %[^\n]", timestamp, logLevel, message);
        info = insertLoglogfile(info, timestamp, logLevel, message);
    }
    displayLoglogfile(info);

    return 0;
}

我得到输出为

`时间戳:2023-04-23 12:36:12 日志级别:错误 消息:数据库连接失败:连接被拒绝。

时间戳:2023-04-23 12:36:12 日志级别:错误 消息:数据库连接失败:连接被拒绝。

时间戳:2023-04-23 12:36:12 日志级别:错误 消息:数据库连接失败:连接被拒绝。`

它在每个块中存储最后一个值,请帮助 谢谢

c pointers structure
1个回答
0
投票

您从主函数中引用了

message
变量地址,因此您将永远不会有一个副本。

同样适用于

timestamp
level
以及您从 main 传递到 insert 并且需要副本而不是参考的任何内容。

之前需要复制它的内容:

int n = strlen(message);
// allocate space for message
temp->message = malloc((n + 1) * sizeof(char));
// copy source message to target, limited to its length
strncpy(temp->message, message, n);
temp->message[n] = 0;

此代码中可能有错误,尤其是在

strncpy
中,但这是要点。

您可以在这里获得更多信息:Using strncpy() to copy const char *

© www.soinside.com 2019 - 2024. All rights reserved.