【问题标题】:Reading and storing whole words from file to array从文件读取和存储整个单词到数组
【发布时间】:2019-05-04 15:03:36
【问题描述】:

我想分析文本文件中的单词(每个单词的长度、起始字符等)。为此,第一步是从文件中读取每个单词,然后将其存储在一个数组中。

在下面的代码中,我设法将所有字符存储在一个数组中,但没有分成单词。

#include <stdio.h>
#include <stdlib.h>


FILE *inp;
char arr[100];
int i = 0;
int word_count = 0;
char c;
int char_count = 0;

inp = fopen("string_in.txt", "r");

    while ((c = fgetc(inp)) != EOF) {
    if (c == ' ' || c == '\n') {
        printf("\n");
        arr[char_count] = c;
        word_count++;
    }
    else {
        //printf("%c", c); //print to check if file is being read correctly
        arr[char_count] = c; 
        printf("%c",arr[char_count]);
    }   
    char_count++;
}
printf("\n");
printf("Chars: %d, Words: %d\n", char_count, word_count+1);

printf("From array: \n");
for(i = 0; i <= word_count; i++) {
    printf("%c",arr[word_count]);
}

printf("\n");
fclose(inp);

return (EXIT_SUCCESS);

输入文字:

This is a test

输出:

This
is
a
test
Chars: 15, Words: 4
From array:
This is a test

我想按如下方式访问元素:

arr[0] = 'This'
arr[3] = 'Test'

但因为我是逐个字符而不是逐字:

arr[0] = 'T'
arr[3] = 's'

关于如何扩展它来存储完整的单词有什么建议吗?


编辑:

根据下面的答案:

while ((c = fgetc(inp)) != EOF) {
if (c == ' ' || c == '\n') {
    printf("\n");

    arr[word_count][char_count] = '\0'; //Terminate the string
    char_count = 0; //Reset the counter.
    word_count++;
}
else {
     arr[word_count][char_count] = c; 
     printf("%c",arr[word_count][char_count]);
}   
(char_count < 99)? (char_count++):(char_count = 0);
}


printf("From array: \n");
for(i = 0; i < word_count; i++) {
    printf("%s",arr[word_count]);
}

不打印数组的输出。

This
is
a
test
Chars: 5, Words: 4
From array:

 Press [Enter] to close the terminal ...

【问题讨论】:

  • 您没有存储完整的单词。

标签: c file text


【解决方案1】:

目前您有空间存储单个string

char arr[100]; -->char arr[100][100];

那么你的阅读变化如下。

while ((c = fgetc(inp)) != EOF) {
    if (c == ' ' || c == '\n') {
        printf("\n");

        arr[word_count][char_count] = '\0'; //Terminate the string
        char_count = 0; //Reset the counter.
        word_count++;
    }
    else {
         arr[word_count][char_count] = c; 
         printf("%c",arr[word_count][char_count]);

         if (char_count < 99)
              char_count++;
            else
              char_count = 0;
    }   
}

您的打印更改如下。

for(i = 0; i < word_count; i++) {
    printf("%s",arr[i]);
}

【讨论】:

  • 非常感谢您的回答。将对此进行测试。三元运算符是否应该以 char_count == 0 结尾,而不是 =0
  • @Rrz0 不,如果达到 100 个字符,我会将 char_count 重置为 0。这只是为了避免越界访问。实际上这个条件不应该满足。如果需要,您应该保留更多内存。
  • 出于某种原因,我还没有弄清楚我在那条线上一直收到lvalue required as left operand of assignment
  • 请将那个可怕的条件表达式改成普通的if 语句。
  • 是的,你是对的。它适用于 if-else 语句!谢谢
【解决方案2】:

正如其他用户所说,您必须使用二维数组。它们以这种方式声明和初始化:arr[10][100](10 将是要存储的字数,根据您的需要进行更改)。

while ((c = fgetc(inp)) != EOF) {
    if (c == ' ' || c == '\n') {
        printf("\n");
        arr[word_count][char_count]='\0';
        char_count=0;
        word_count++;
    }
    else {
        arr[word_count][char_count] = c;
        printf("%c",arr[word_count][char_count]);
        char_count++;
    }
    if(char_count>=100) // security in case a word is too long
        char_count=0;
}
word_count++;

printf("\n");
printf("Chars: %d, Words: %d\n", char_count, word_count);

printf("From array: \n");
for(i = 0; i < word_count; i++) {
    printf("%s ",arr[i]);
}

请记住,因为我每次有一个新单词时都会重置char_count,所以循环后它的值不会是你所期望的(它只是最后一个单词的长度)并且显示的值在printf 将是错误的。如果你真的关心它,你将不得不创建另一个变量。

【讨论】:

  • 感谢您的回答。但是,这只会从数组中打印 :This
【解决方案3】:

您可以使用 Char ** 来完全存储单词

数组[100][100]

因为在 array[100] 中存储了一个字符串,因此在访问 array[0] 时,您只能访问该字符串的第一个字母。

您需要更改代码并实现双数组而不是单个数组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2019-07-03
    • 2016-01-24
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多