【问题标题】:Issues with 2D array and strings二维数组和字符串的问题
【发布时间】: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


    【解决方案1】:

    我非常想从组合中删除问候功能,但是……

    这编译干净,不像原来的。它还允许在字符串中最多包含 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;
    }
    

    【讨论】:

    • 好吧,但是对于这段代码,拥有 word_array 的第二维的目的是什么?基本没用吗?
    • 第二个维度为单词提供存储空间。这很关键。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-06
      • 2021-06-12
      • 2019-01-23
      • 2022-11-12
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 2019-09-14
      相关资源
      最近更新 更多