c - 二维数组和字符串的问题

标签 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 (函数1( ) )

  • 有条不紊地遍历数组寻找相同的词,这些
    单词必须完全匹配,包括大小写 (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 文件中。

最佳答案

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

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

关于c - 二维数组和字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895378/

相关文章:

php - Mysql搜索以逗号分隔的字符串

objective-c - Swift 版本的 Objective C rstrip : index problems

php - 如何将具有父子关系的数据库表转换为多维数组

php - PHP current() 函数是否返回数组的副本或引用?

c - 结构数组的运行时和编译时初始化之间的区别

c - 错误: incompatible types when initializing type 'struct ...'

c - 释放结构体双指针的正确方法

c - 查找 char 参数的大小

java -\G 在 .split 中如何工作?

php - 关联数组(2 伙伴)