【发布时间】:2013-11-10 21:25:02
【问题描述】:
我正在尝试创建一个程序来读取名为 words.dat 的文件,该文件包含由空格分隔的 20 个单词的单词列表,并告诉匹配单词的数量。但是,该单词的长度不能超过 17 个字符。 匹配的单词区分大小写,因此单词“Ninja”和“ninja”将不匹配。 但是,我很难让我的 function1() 正常工作。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WORDS 20
#define LENGTH 17
void intro_msg (void);
char function1 (char [WORDS] [LENGTH]);
void goodbye_msg (void);
int main( void )
{
char word_array [WORDS] [LENGTH];
intro_msg ( ) ;
function1 (word_array);
goodbye_msg ( ) ;
return ( 0 ) ;
}
void intro_msg (void)
{
printf( "\n Welcome user.\n");
return ;
}
char function1 (char word_array[WORDS] [LENGTH])
{
FILE *wordsfile ;
wordsfile = fopen("words.dat", "r");
if (wordsfile == NULL)
printf("\n words.dat was not properly opened.\n");
else
{
printf ("\n words.dat is opened in the read mode.\n\n");
fscanf (wordsfile, "%s", word_array );
while ( !feof (wordsfile) )
{
printf (" %s \n", word_array);
fscanf (wordsfile , "%s", word_array );
}
fclose(wordsfile);
}
return (word_array [WORDS] [LENGTH]);
}
void goodbye_msg (void)
{
printf ( "\n Thank you for using this program. GoodBye. \n\n " ) ;
return ;
}
整体方案应该是
创建一个20个字符串的数组,每个字符串最多17个字符
从名为的文件中填充字符串数组的每个元素 words.dat (function1( ) )
有条不紊地遍历数组寻找相同的单词,这些
单词必须完全匹配,包括大小写 (function2( ) )在屏幕上显示字符串数组的内容
(words.dat) 文件和相同单词对的数量
(函数3())
我可以做些什么来修复 function1 以便它完成从指定文件填充字符串数组的每个元素的任务?
当前样本输出:
Welcome user.
words.dat is opened in the read mode.
Ninja
DragonsFury
failninja
dragonsrage
leagueoflegendssux
white
black
red
green
yellow
green
leagueoflegendssux
dragonsfury
Sword
sodas
tiger
snakes
Swords
Snakes
Thank you for using this program. GoodBye.
- 请注意,列出的单词包含在 word.dat 文件中。
【问题讨论】:
标签: c string multidimensional-array