在 C 中解析文件时遇到问题
Having trouble parsing a file in C
我正在尝试编写一个程序,自动打开 class 的 google class 房间 link 我还有两分钟开始。
到目前为止,我已经决定拥有一个包含三列的 tsv 文件:分别是时间、主题、Gmeet link。
有点像这样:
[1]
11:15 CD https://en.wikipedia.org/wiki/Inotify
14:00 SGD https://en.wikipedia.org/wiki/Inotify
15:05 SGD https://en.wikipedia.org/wiki/Inotify
[2]
09:00 AI https://en.wikipedia.org/wiki/Inotify
方框中的数字对应星期几。
所以我打算做的是将一天的时间表存储在一个大小为 9 的数组中,因为我可以在任何给定的一天在 9 个不同的时间段有 classes,每个 [=25] 的 link =] 在索引 [小时 - 9]。因此,上午 9 点的第一个 class 将处于索引零,空的时隙将具有 null 作为它们的值。
代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
const char FILE_NAME[] = "schedule.txt";
const int CLASSES = 9;
int main()
{
time_t now = time(NULL);
struct tm *local = localtime(&now);
FILE *file = fopen(FILE_NAME, "r"); /* Input file */
char line[100];
int hour;
char *min;
char *sub;
char *link;
/* char current_day = local->tm_wday; */
char current_day = 1;
char *sched[CLASSES];
if (file == NULL) {
printf("Cannot open file: \"%s\"\n", FILE_NAME);
exit(8);
}
for (int i = 0; i < CLASSES; ++i)
sched[i] = NULL;
char dayfound = 0;
while(fgets(line, sizeof(line), file)) {
if ((line[0] == '[' && atoi(&line[1]) == current_day)) {
dayfound = 1;
continue;
}
if (dayfound && line[0] == '\n') {
dayfound = 0;
break;
}
if (dayfound) {
hour = atoi(strtok(line, ":"));
min = strtok(NULL, "\t");
sub = strtok(NULL, "\t");
link = strtok(NULL, "\t");
sched[hour - 9] = link;
}
}
for (int i = 0; i < CLASSES; ++i) {
if (sched[i] != NULL)
printf("%s", sched[i]);
}
fclose(file);
return 0;
}
出于某种原因,我那天第一个 class 的值被空字符串覆盖,而我只得到另外两个 class 的 link ]es.
这是输出:
https://en.wikipedia.org/wiki/Inotify
https://en.wikipedia.org/wiki/Inotify
如您所见,缺少 CD 的第一个 link。
谁能指出为什么会这样?
您正在为文件的每一行读入相同的 line
字符串。 strtok()
returns 指向该字符串的指针。所以你所有的 link
值都指向同一个字符串。
将 link
分配给 sched[hour-9]
时,您应该复制它:
sched[hour - 9] = strdup(link);
或者您可以将 sched
设为二维数组并使用 strcpy()
char sched[CLASSES][100];
...
strcpy(sched[hour - 9], link);
我正在尝试编写一个程序,自动打开 class 的 google class 房间 link 我还有两分钟开始。 到目前为止,我已经决定拥有一个包含三列的 tsv 文件:分别是时间、主题、Gmeet link。 有点像这样:
[1]
11:15 CD https://en.wikipedia.org/wiki/Inotify
14:00 SGD https://en.wikipedia.org/wiki/Inotify
15:05 SGD https://en.wikipedia.org/wiki/Inotify
[2]
09:00 AI https://en.wikipedia.org/wiki/Inotify
方框中的数字对应星期几。 所以我打算做的是将一天的时间表存储在一个大小为 9 的数组中,因为我可以在任何给定的一天在 9 个不同的时间段有 classes,每个 [=25] 的 link =] 在索引 [小时 - 9]。因此,上午 9 点的第一个 class 将处于索引零,空的时隙将具有 null 作为它们的值。
代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
const char FILE_NAME[] = "schedule.txt";
const int CLASSES = 9;
int main()
{
time_t now = time(NULL);
struct tm *local = localtime(&now);
FILE *file = fopen(FILE_NAME, "r"); /* Input file */
char line[100];
int hour;
char *min;
char *sub;
char *link;
/* char current_day = local->tm_wday; */
char current_day = 1;
char *sched[CLASSES];
if (file == NULL) {
printf("Cannot open file: \"%s\"\n", FILE_NAME);
exit(8);
}
for (int i = 0; i < CLASSES; ++i)
sched[i] = NULL;
char dayfound = 0;
while(fgets(line, sizeof(line), file)) {
if ((line[0] == '[' && atoi(&line[1]) == current_day)) {
dayfound = 1;
continue;
}
if (dayfound && line[0] == '\n') {
dayfound = 0;
break;
}
if (dayfound) {
hour = atoi(strtok(line, ":"));
min = strtok(NULL, "\t");
sub = strtok(NULL, "\t");
link = strtok(NULL, "\t");
sched[hour - 9] = link;
}
}
for (int i = 0; i < CLASSES; ++i) {
if (sched[i] != NULL)
printf("%s", sched[i]);
}
fclose(file);
return 0;
}
出于某种原因,我那天第一个 class 的值被空字符串覆盖,而我只得到另外两个 class 的 link ]es.
这是输出:
https://en.wikipedia.org/wiki/Inotify
https://en.wikipedia.org/wiki/Inotify
如您所见,缺少 CD 的第一个 link。 谁能指出为什么会这样?
您正在为文件的每一行读入相同的 line
字符串。 strtok()
returns 指向该字符串的指针。所以你所有的 link
值都指向同一个字符串。
将 link
分配给 sched[hour-9]
时,您应该复制它:
sched[hour - 9] = strdup(link);
或者您可以将 sched
设为二维数组并使用 strcpy()
char sched[CLASSES][100];
...
strcpy(sched[hour - 9], link);