从输入文件中读取并将单词存储到数组中
Reading from an input file and storing words into an array
最终目标是输出一个文本文件,其中重复的单词被编码为单个数字。我目前遇到的问题是读取单词并将它们存储到数组中。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_CODE 120
void main() {
FILE *inputfile = fopen("input.txt","rw");
char buffer[128];
char *token;
char *words[MAX_CODE];
int i = 0;
while(fgets(buffer, 128, inputfile)){
token = strtok(buffer," ");
printf("Token %d was %s",i,token);
while(token != NULL) {
words[i] = malloc(strlen(token)+1);
strcpy(words[i], token);
i++;
token = strtok(buffer," ");
}
}
for(int i = 0; i<3; i++) printf("%d\n%s\n",i,words[i]);
printf("End");
}
我得到的是分段错误,或者什么都没有。我想要的是让单词成为一个字符串数组。我正在为每个字符串分配内存,所以哪里出错了?
您对 strtok
的第二次调用应该为第一个参数传递 NULL
。否则,strtok
将一遍又一遍地解析第一个标记。
token = strtok(buffer," ");
printf("Token %d was %s\n",i,token);
while(i < MAX_CODE && token != NULL) {
words[i] = malloc(strlen(token)+1);
strcpy(words[i], token);
i++;
token = strtok(NULL," ");
}
检查 MAX_CODE
是为了安全起见,以防您增加 buffer
的大小或减小 MAX_CODE
的值。在您当前的代码中,您可以在 128 字节缓冲区中容纳的 space 分隔标记的最大数量是 64。
来自 cppreference:
- If
str != NULL
, the call is treated as the first call to strtok
for this particular string. ...
- If
str == NULL
, the call is treated as a subsequent calls to strtok
: the function continues from where it left in previous invocation. ...
最终目标是输出一个文本文件,其中重复的单词被编码为单个数字。我目前遇到的问题是读取单词并将它们存储到数组中。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_CODE 120
void main() {
FILE *inputfile = fopen("input.txt","rw");
char buffer[128];
char *token;
char *words[MAX_CODE];
int i = 0;
while(fgets(buffer, 128, inputfile)){
token = strtok(buffer," ");
printf("Token %d was %s",i,token);
while(token != NULL) {
words[i] = malloc(strlen(token)+1);
strcpy(words[i], token);
i++;
token = strtok(buffer," ");
}
}
for(int i = 0; i<3; i++) printf("%d\n%s\n",i,words[i]);
printf("End");
}
我得到的是分段错误,或者什么都没有。我想要的是让单词成为一个字符串数组。我正在为每个字符串分配内存,所以哪里出错了?
您对 strtok
的第二次调用应该为第一个参数传递 NULL
。否则,strtok
将一遍又一遍地解析第一个标记。
token = strtok(buffer," ");
printf("Token %d was %s\n",i,token);
while(i < MAX_CODE && token != NULL) {
words[i] = malloc(strlen(token)+1);
strcpy(words[i], token);
i++;
token = strtok(NULL," ");
}
检查 MAX_CODE
是为了安全起见,以防您增加 buffer
的大小或减小 MAX_CODE
的值。在您当前的代码中,您可以在 128 字节缓冲区中容纳的 space 分隔标记的最大数量是 64。
来自 cppreference:
- If
str != NULL
, the call is treated as the first call tostrtok
for this particular string. ...- If
str == NULL
, the call is treated as a subsequent calls tostrtok
: the function continues from where it left in previous invocation. ...