简体   繁体   中英

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

i have a problem with my c program, it should read words/strings from txt file, then count length of them. when i run my program, it doesnt response

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

it should do this: first i open my file, then i will read every char from this file+storing this chars in array word, then when blank space occurs(' '), it should write chars to next index of array, so the words will be created on different indexes of array then it should count the lenght of words, but that should be easy to implement, thx a sorry for my english

They are ALOT of errors with the code you shared :

  • J is not declared, so you need to add int j = 0 ; I'm assuming than j is the number on whitespace on your doc.

  • word[b]=word[b]+c; get changed into word[b]= c;

  • You add an incremntation on b in your loop then, so you wont write only on word[0].

  • Your printing is bad aswell, you would only show the first letter over and over.

This is the final code, corrected. It shows the entire file if the file is less than 200 caracters. J is the number of whitespace.

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

By the way, next time. try to compile at least. It's clear that you put no effort into it before submitting a question here on 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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