【问题标题】:Reading WORDS from a text file into an array in C将文本文件中的WORDS读取到C中的数组中
【发布时间】:2012-01-24 20:42:15
【问题描述】:

如何从格式如下的文件中读取整个单词直到换行符:

苹果

葡萄

香蕉

我希望从文件中将每个单词作为单独的值存储在数组中。我该怎么做?

谢谢大家

【问题讨论】:

  • 到目前为止你做了什么?
  • 我尝试了很多不同的东西,但没有任何效果?
  • “直到换行”是什么意思?换行在哪里?在每个单词之后还是结尾?
  • 这不是一个“请为我工作”的网站。如果你想这样做,你应该聘请一名顾问。你应该展示你迄今为止尝试过的东西,并解释它如何不像你期望的那样工作,然后有人可以帮助你解决它。我们试图在这里提供帮助,但您应该自己付出一些努力,并说“我尝试了很多不同的东西,但没有任何效果?”没有表现出任何努力。 “我多次尝试成为亿万富翁,但无济于事”,如果我的尝试都只是坐在那里许愿,希望它们会成真,那这一切都毫无意义。 :)

标签: c file io


【解决方案1】:

你可以像这样使用fgets函数

char word[100][100];
int i = 0;

while( fgets( word[i++], 80, file) )
  ;

【讨论】:

    【解决方案2】:

    试试这个

    char *my_strchr(char * string_ptr, char find)
    {
        while (*string_ptr != find) {
    
           /* Check for end */
    
           if (*string_ptr == '\0')
               return (NULL);       /* not found */
    
            ++string_ptr;
        }
        return (string_ptr);        /* Found */
    }
    
    int words_count(char * string_ptr, char find)
    {
        int i = 0;
        while (*string_ptr != '\0') {
    
           /* Check for end */
    
           if (*string_ptr == find)
               ++i;       /* not found */
    
            ++string_ptr;
        }
        return (i+1);        /* Found */
    }
    
    int main()
    {
        char line[80];      /* The input line */
        char *first_ptr;    /* pointer to the first name */
        char *last_ptr;     /* pointer to the last name */
        // char words[3][80];
        char **words;
        int cnt;
    
        // fgets(line, sizeof(line), stdin);
        static const char filename[] = "C:\\Diginity-Works\\LineReader\\debug\\data.txt";
        FILE *file = fopen ( filename, "r" );
    
        while ( fgets ( line, sizeof line, file ) != NULL ) // read a line 
        {   
            /* Get rid of trailing newline */
            line[strlen(line)-1] = '\0';        
            cnt = words_count(line, ' ');
    
            // for the "first level"
            words = (char **) malloc(cnt * sizeof *words);
    
            // In the second level
            for(int i=0;i<cnt;++i)
                words[i]=(char*)malloc(80);// i assume that max word length is 80
    
    
            last_ptr = line;    /* last name is at beginning of line */
            for(int i=0; i < cnt; ++i){
                first_ptr = my_strchr(line, ' ');      /* Find space */
    
                /* Check for an error */
                if (first_ptr == NULL) {
                    strcpy(words[i], last_ptr);
                    break;
                }
    
                *first_ptr = '\0';  /* Zero out the slash */
    
                ++first_ptr;        /* Move to first character of name */
                strcpy(words[i], last_ptr);
                strcpy(line, first_ptr);
            }
    
            for(int i=0; i < cnt; ++i)
                printf("%d : %s \n", i, words[i]);
        }
    
        return (0);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 2020-01-20
      • 2010-09-29
      • 1970-01-01
      相关资源
      最近更新 更多