繁体   English   中英

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

[英]reading words/strings from file+length of them - c

我的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;
}

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

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

  • 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的问题之前不费吹灰之力。

#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;
}
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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM