gpt4 book ai didi

c - 使用fgets()和strtok()读取文本文件以分隔行中的字符串,从而产生不必要的行为

转载 作者:太空宇宙 更新时间:2023-11-04 05:33:00 26 4
gpt4 key购买 nike

我试图使用fgets()和strtok()读取以下格式的文本文件。

1082018 1200 79 Meeting with President
2012018 1200 79 Meet with John at cinema
2082018 1400 30 games with Alpha
3022018 1200 79 sports

我需要将第一个值与行的其余部分分开,例如:
key=21122019, val = 1200 79 Meeting with President

为此,我将 strchr()用于 valstrtok()用于 key,但是,从文件读取时,键值保持不变。我不明白为什么会发生这种情况,因为我在while循环中为in_键分配空间,并且每次都以不同的索引放置在数组中。
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1000 // max number of lines to be read
#define VALLEN 100
#define MAXC 1024

#define ALLOCSIZE 1000 /*size of available space*/
static char allocbuf[ALLOCSIZE]; /* storage for alloc*/
static char *allocp = allocbuf; /* next free position*/

char *alloc(int n) { /* return a pointer to n characters*/
if (allocbuf + ALLOCSIZE - allocp >= n) { /*it fits*/
allocp += n;
return allocp - n; /*old p*/
} else /*not enough room*/
return 0;
}

int main(int argc, char** argv) {
FILE *inp_cal;
inp_cal = fopen("calendar.txt", "r+");

char buf[MAXC];
char *line[1024];
char *p_line;

char *in_val_arr[100];
char *in_key_arr[100];
int count = 0;
char delimiter[] = " ";

if (inp_cal) {
printf("Processing file...\n");
while (fgets(buf, MAXC, inp_cal)) {
p_line = malloc(strlen(buf) + 1); // malloced with size of buffer.
char *in_val;
char *in_key;

strcpy(p_line, buf); //used to create a copy of input buffer
line[count] = p_line;

/* separating the line based on the first space. The words after
* the delimeter will be copied into in_val */
char *copy = strchr(p_line, ' ');
if (copy) {
if ((in_val = alloc(strlen(line[count]) + 1)) == NULL) {
return -1;
} else {
strcpy(in_val, copy + 1);
printf("arr: %s", in_val);
in_val_arr[count] = in_val;
}
} else
printf("Could not find a space\n");

/* We now need to get the first word from the input buffer*/
if ((in_key = alloc(strlen(line[count]) + 1)) == NULL) {
return -1;
}
else {
in_key = strtok(buf, delimiter);
printf("%s\n", in_key);
in_key_arr[count] = in_key; // <-- Printed out well
count++;
}
}
for (int i = 0; i < count; ++i)
printf("key=%s, val = %s", in_key_arr[i], in_val_arr[i]); //<-- in_key_arr[i] contains same values throughout, unlike above
fclose(inp_cal);
}
return 0;
}

while循环输出(正确):
Processing file...
arr: 1200 79 Meeting with President
1082018
arr: 1200 79 Meet with John at cinema
2012018
arr: 1400 30 games with Alpha
2082018
arr: 1200 79 sports
3022018

对于循环输出(不正确):
key=21122019, val = 1200 79 Meeting with President
key=21122019, val = 1200 79 Meet with John
key=21122019, val = 1400 30 games with Alpha
key=21122019, val = 1200 79 sports

有什么建议可以改进,为什么会这样?谢谢

最佳答案

您一次又一次地将同一指针存储在in_key_arr中。
你大概需要这个:

in_key = strtok(buf, delimiter);
printf("%s\n", in_key);
char *newkey = malloc(strlen(in_key) + 1); // <<<< allocate new memory
strcpy(newkey, in_key);
in_key_arr[count] = newkey; // <<<< store newkey
count++;

免责声明:
为简洁起见,不进行错误检查
完成后,需要释放malloced内存。

关于c - 使用fgets()和strtok()读取文本文件以分隔行中的字符串,从而产生不必要的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53881533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com