0

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

urgot
  • 5
  • 5
  • Is this compiling? I see you use j++, but I don’t see the variable j being declared/defined. – Joe Nov 25 '14 at 16:39
  • A duplicate question with an answer here - http://stackoverflow.com/questions/13039023/words-counting-in-file-like-linux-wc-command-in-c Check if this works for you – Joe Nov 25 '14 at 16:42

3 Answers3

0

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.

Mekap
  • 2,065
  • 14
  • 26
  • im sorry, my mistake, that should be b++, now its corrected, that was increasing index of array word, so every word would be written on different index of array, so later i will be able to use this words from this array – urgot Nov 25 '14 at 17:04
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;
}
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • while (c != EOF){ c = fscanf(f,"%s",word); then just printf word/it works, but if i want to store this words in array like: array[5][20]=word; this doesnt work – urgot Nov 25 '14 at 17:21
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;
}
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • One thing you will want to try and do is answer the question that was asked, and help the OP understand where he went wrong, instead of just slapping down a completely different piece of code and saying `the following compiles and meets your description of what needs to be done`. Just something to consider. – David C. Rankin Nov 26 '14 at 01:32