使用feof将文本文件中的单词存储到char数组中
Storing words from text file into char array using feof
所以我有一个这样的文本文件:
零三二一五零零五七..等等
而且有很多,准确地说是 9054 个单词
我的想法是创建一个包含 9054 个空格的字符数组并将其存储在其中,这是我目前所做的:
#include <stdio.h>
int main(void)
{
char tmp;
int i = 0;
int j = 0;
char array[44000];
FILE *in_file;
in_file = fopen("in.txt", "r");
// Read file in to array
while (!feof(in_file))
{
fscanf(in_file,"%c",&tmp);
array[i] = tmp;
i++;
}
// Display array
while (j<i)
{
printf("%c",array[j]);
j++;
}
fclose(in_file);
while(1);
return 0;
}
问题是我不知道如何存储单词,因为我所做的是将每个字符存储到数组中,因此它变成了一个大约 44000 的数组。我怎样才能使数组存储单词?
我也不知道 feof 函数的作用,尤其是行
while (!feof(in_file))
这行到底是什么意思?抱歉,我仍处于学习 C 的初级阶段,我尝试查找 feof 的功能,但找不到太多东西
通常您可以使用以下步骤:
- 将整个文本文件转储到字符缓冲区。
- 使用
strtok
将字符缓冲区拆分为多个标记或单词。
- 使用指向 char 的指针数组来存储单个单词。
沿着这条线做点什么就可以了。请注意,我使用您的问题标题作为文本文件。您需要适当地替换 20
。
int main ()
{
FILE *in_file;
in_file = fopen("in.txt", "r");
fseek( in_file, 0, SEEK_END );
long fsize = ftell( in_file );
fseek( in_file, 0, SEEK_SET );
char *buf = malloc( fsize + 1 );
fread( buf, fsize, 1, in_file ); // Dump the whole file to a char buffer.
fclose( in_file );
char *items[20] = { NULL };
char *pch;
pch = strtok (buf," \t\n");
int i = 0;
while (pch != NULL)
{
items[i++] = pch;
pch = strtok (NULL, " \t\n");
}
for( i = 0; i < 20; i++ )
{
if( items[i] != NULL )
{
printf( "items[%d] = %s\n", i, items[i] );
}
}
return 0;
}
输出:
items[0] = Storing
items[1] = words
items[2] = from
items[3] = textfile
items[4] = into
items[5] = char
items[6] = array
items[7] = using
items[8] = feof?
而不是检查 feof()
,它告诉您文件结尾是否发生在 previous 输入操作中,检查结果fscanf()
用"%s"
读取"words"并限制读取的最大数量char
。
char buf[100];
fscanf(in_file,"%99s",buf);
放在一起:
#define WORD_SIZE_MAX 20
#define WORD_COUNT_MAX 10000
char array[WORD_COUNT_MAX][WORD_SIZE_MAX];
unsigned word_i = 0;
for (i=0; i<WORD_COUNT_MAX; i++) {
if (fscanf(in_file,"%19s", word_list[i]) != 1) {
break;
}
}
另一种方法是几乎按原样使用 OP 代码。将整个文件读入 1 个数组。然后在打印时,跳过 white-space.
所以我有一个这样的文本文件:
零三二一五零零五七..等等
而且有很多,准确地说是 9054 个单词
我的想法是创建一个包含 9054 个空格的字符数组并将其存储在其中,这是我目前所做的:
#include <stdio.h>
int main(void)
{
char tmp;
int i = 0;
int j = 0;
char array[44000];
FILE *in_file;
in_file = fopen("in.txt", "r");
// Read file in to array
while (!feof(in_file))
{
fscanf(in_file,"%c",&tmp);
array[i] = tmp;
i++;
}
// Display array
while (j<i)
{
printf("%c",array[j]);
j++;
}
fclose(in_file);
while(1);
return 0;
}
问题是我不知道如何存储单词,因为我所做的是将每个字符存储到数组中,因此它变成了一个大约 44000 的数组。我怎样才能使数组存储单词?
我也不知道 feof 函数的作用,尤其是行
while (!feof(in_file))
这行到底是什么意思?抱歉,我仍处于学习 C 的初级阶段,我尝试查找 feof 的功能,但找不到太多东西
通常您可以使用以下步骤:
- 将整个文本文件转储到字符缓冲区。
- 使用
strtok
将字符缓冲区拆分为多个标记或单词。 - 使用指向 char 的指针数组来存储单个单词。
沿着这条线做点什么就可以了。请注意,我使用您的问题标题作为文本文件。您需要适当地替换 20
。
int main ()
{
FILE *in_file;
in_file = fopen("in.txt", "r");
fseek( in_file, 0, SEEK_END );
long fsize = ftell( in_file );
fseek( in_file, 0, SEEK_SET );
char *buf = malloc( fsize + 1 );
fread( buf, fsize, 1, in_file ); // Dump the whole file to a char buffer.
fclose( in_file );
char *items[20] = { NULL };
char *pch;
pch = strtok (buf," \t\n");
int i = 0;
while (pch != NULL)
{
items[i++] = pch;
pch = strtok (NULL, " \t\n");
}
for( i = 0; i < 20; i++ )
{
if( items[i] != NULL )
{
printf( "items[%d] = %s\n", i, items[i] );
}
}
return 0;
}
输出:
items[0] = Storing
items[1] = words
items[2] = from
items[3] = textfile
items[4] = into
items[5] = char
items[6] = array
items[7] = using
items[8] = feof?
而不是检查
feof()
,它告诉您文件结尾是否发生在 previous 输入操作中,检查结果fscanf()
用
"%s"
读取"words"并限制读取的最大数量char
。char buf[100]; fscanf(in_file,"%99s",buf);
放在一起:
#define WORD_SIZE_MAX 20
#define WORD_COUNT_MAX 10000
char array[WORD_COUNT_MAX][WORD_SIZE_MAX];
unsigned word_i = 0;
for (i=0; i<WORD_COUNT_MAX; i++) {
if (fscanf(in_file,"%19s", word_list[i]) != 1) {
break;
}
}
另一种方法是几乎按原样使用 OP 代码。将整个文件读入 1 个数组。然后在打印时,跳过 white-space.