2D数组和字符串的问题

时间:2013-11-10 21:25:02

标签: c string multidimensional-array

我正在尝试创建一个程序,该程序读取一个名为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())

  • 有条不紊地遍历数组寻找相同的单词,这些 单词必须完全匹配,包括case(function2())

  • 向屏幕显示字符串数组的内容
    (words.dat)文件和相同的单词对数量
    (function3())

如何修复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文件中。

2 个答案:

答案 0 :(得分:2)

我非常想从混音中删除问候语功能,但是......

与原版不同,编辑干净利落。它还允许最多17个字符加上字符串中的终止空值。它也使用fscanf()来强制执行该限制。请注意,它不使用feof() - 很少有程序需要。即使有人提供的文件超过20个字,它也不会溢出数组边界;如果文件的字数较少,也不会遇到问题。它不会在换行符之前放置空格。

#include <stdio.h>

#define WORDS 20
#define LENGTH 17

void intro_msg(void);
int  function1(char[WORDS][LENGTH+1]);
void goodbye_msg(void);

int main( void )
{
    char word_array[WORDS][LENGTH+1];

    intro_msg();
    int n = function1(word_array);
    printf("Found %d words\n", n);
    goodbye_msg();

    return(0);
}

void intro_msg(void)
{
    printf("\nWelcome user.\n");
}

int function1(char word_array[WORDS][LENGTH+1])
{
    FILE *wordsfile = fopen("words.dat", "r");
    int i = 0;

    if (wordsfile == NULL)
        printf("\nwords.dat was not properly opened.\n");
    else
    {
        printf("\nwords.dat is opened in the read mode.\n\n");
        for (i = 0; i < WORDS; i++)
        {
            if (fscanf(wordsfile, "%17s", word_array[i]) != 1)
                break;
            printf("   %s\n", word_array[i]);
        }
        fclose(wordsfile);
    }
    return i;
}

void goodbye_msg(void)
{
    printf("\nThank you for using this program. GoodBye.\n\n");
}

此代码不会查找重复项或任何内容。

请注意,您的示例数据包含18个字符(leagueoflegendssux)的字词。你不说那会发生什么。如果你需要读取整行并截断读取的内容以使其适合,或拒绝该行,或者......什么?

这是一些替代代码,减去问候函数,必要时会截断:

#include <stdio.h>
#include <string.h>

#define WORDS 20
#define LENGTH 17

void intro_msg(void);
int  function1(char[WORDS][LENGTH+1]);
void goodbye_msg(void);

int main(void)
{
    char word_array[WORDS][LENGTH+1];

    int n = function1(word_array);
    printf("Found %d words\n", n);

    return(0);
}

int function1(char word_array[WORDS][LENGTH+1])
{
    FILE *wordsfile = fopen("words.dat", "r");
    int i = 0;

    if (wordsfile == NULL)
        printf("\nwords.dat was not properly opened.\n");
    else
    {
        char line[4096];
        printf("\nwords.dat is opened in the read mode.\n\n");
        for (i = 0; i < WORDS; i++)
        {
            if (fgets(line, sizeof(line), wordsfile) == 0)
                break;
            size_t len = strlen(line);
            line[len-1] = '\0';  // Zap newline
            strncpy(word_array[i], line, sizeof(word_array[i])-1);
            word_array[i][sizeof(word_array[i])-1] = '\0';
            printf("   %s\n", word_array[i]);
        }
        fclose(wordsfile);
    }
    return i;
}

答案 1 :(得分:1)

你扫描到word_array,它基本上是char **类型,你希望它是word_array[index]的字符串,用于给定的运行索引。

另请注意feof在某些情况下很棘手,请点击此处了解更多详情 - Why is “while ( !feof (file) )” always wrong?

相关问题