从文件中读取单词/字符串+它们的长度 - c

时间:2014-11-25 16:28:29

标签: c arrays string

我的c程序有问题, 它应该读取txt文件中的单词/字符串,然后计算它们的长度。 当我运行我的程序时,它没有响应

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f;
char c;
char word[50];
int a,b=0;

if ((f = fopen("file.txt", "r")) == NULL)
   {
        printf("CANT OPEN THE FILE" "\n");
        return 1;
    }
while((c=fgetc(f))!=EOF){
        if (c==' ')b++;
        word[b]=word[b]+c;
    }

for (a=0;a<b;a++){
    printf("%c ",word[0]);
    }

    return 0;
}

它应该这样做:首先我打开我的文件,然后我将读取此文件中的每个字符+将此字符存储在数组字中,然后当出现空格('')时,它应该将字符写入数组的下一个索引,因此将在不同的数组索引上创建单词 然后它应该计算单词的长度,但这应该很容易实现,对我的英语很抱歉

3 个答案:

答案 0 :(得分:0)

您共享的代码有很多错误:

  • J未声明,因此您需要添加int j = 0;我假设j比你文档上的空格数字。

  • word[b]=word[b]+c;已更改为word[b]= c;

  • 你在循环中的b上添加一个增量,所以你不会只在word [0]上写。

  • 您的打印也很糟糕,您只会反复显示第一个字母。

这是最终的代码,已更正。如果文件少于200个字符,它会显示整个文件。 J是空格的数量。

#include <stdio.h>

#include <stdlib.h>
int main()
{
FILE *f;
char c;
char word[200];
int a,b=0;
int j = 0;

if ((f = fopen("file.txt", "r")) == NULL)
   {
        printf("CANT OPEN THE FILE" "\n");
        return 1;
    }
while((c=fgetc(f))!=EOF){
        if (c==' ')j++;
        word[b]= c;
        b++;
    }

for (a=0;a<b;a++){
    printf("%c",word[a]);
    printf("The file contains %d caracters, and %d whitespaces", b, j);
    }

    return 0;
}

顺便说一下,下次。尝试编译至少。很明显,在SO上提交问题之前,你没有付出任何努力。

答案 1 :(得分:0)

#include <stdio.h>
#include <stdlib.h>
#include <string.h> //for string functions
int main()
{
FILE *f;
int c; //c should be an int 
char word[50];
char *ptr; //to store each word
int a,b=0;

if ((f = fopen("file.txt", "r")) == NULL)
   {
        printf("CANT OPEN THE FILE" "\n");
        return 1;
    }
while((c=fgetc(f))!=EOF){
        word[b++]=c;
    }

for (a=0;a<b;a++){
    printf("%c ",word[a]); //word[a] not word[0]
    }
ptr=strtok(word," ");//get first word
a=0;
while(ptr!=NULL)
{
printf("Word %d which is %s is %d letters long",++a,ptr,strlen(ptr));
ptr=strtok(NULL," "); //get next word
}
    return 0;
}

答案 2 :(得分:0)

the following compiles and meets your description of what needs to be done

#include <stdio.h>
#include <stdlib.h>
#include <string.h> //  memset

#define MAX_WORD_LENGTH (50)

struct wordStruct_t
{
    char word[MAX_WORD_LENGTH];
};

int main()
{
    FILE *fp;
    int c;
    char word[50]; // assume max word length is < 50
    int i = 0; // word byte index
    int wordCount = 0; // count of words read
    struct wordStruct_t * wordArray = NULL;
    char * testArray = NULL;

    if ((fp = fopen("file.txt", "r")) == NULL)
    {
        perror( "fopen failed for read of file.txt");
        exit( EXIT_FAILURE );
    }

    // implied else open successful


    memset( word, 0x00, sizeof( word ) );
    while((c=fgetc(fp))!=EOF)
    {
        if( (c!=' ') && (c != '\n') )
        { // then letter to add to current word (should also check for word overflow)
            word[i++] = c;
        }
        else
        { // else, end of word found
            // allocate max room for new word
            if( NULL == (testArray = realloc( wordArray, sizeof(struct wordStruct_t) * (wordCount+1)) ) )
            {
                perror( "realloc failed");
                free( wordArray );
                fclose( fp );
                exit( EXIT_FAILURE );
            }

            // implied else, realloc successful

            wordArray = (struct wordStruct_t*)testArray;
            strcpy( wordArray[wordCount].word, word );
            memset( word, 0x00, sizeof(word) ); // prep for next word
        } // end if
    } // end while

    for (i = 0; i< wordCount; i++)
    {
        printf("word: %d is %s and contains %d bytes\n", 
               i, 
               wordArray[i].word, 
               (int)strlen(wordArray[i].word ) );
    }
    free( wordArray );

    return 0;
}