簡體   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