1

So I have a textfile which goes like this:

zero three two one five zero zero five seven .. etc

and there is a lot of it, 9054 words to be exact

My idea was to create a char array of 9054 spaces and store it in, this is what I have done so far:

#include <stdio.h>

int main(void)
{
char tmp;
int i = 0;
int j = 0;
char array[44000];

FILE *in_file;

in_file = fopen("in.txt", "r");

// Read file in to array
while (!feof(in_file))
{
      fscanf(in_file,"%c",&tmp);
      array[i] = tmp;
      i++;
}

// Display array
while (j<i)
{
      printf("%c",array[j]);
      j++;
}


fclose(in_file);

while(1);
return 0;
}

The problem is I don't know how to store words, because from what I have done stores each character into the array so it becomes an array of around 44000. How can I make it so the array holds words instead?

Also I don't have an idea what the feof function does, especially the line

while (!feof(in_file))

what does this line exactly mean? Sorry I am still in the baby stages of learning C, I tried looking up what feof does but there is not much to find

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
anony
  • 79
  • 1
  • 2
  • 10
  • you are creating an array of chars but should create an array (or better: list) of strings. And for starters have a look at this for example: http://stackoverflow.com/questions/12337614/how-feof-works-in-c – Marged Dec 02 '15 at 22:49
  • For your code you're saying, *...this is what I've done so far*, and then you are saying, *Also I don't have any idea what the feof function does...* and are using `feof`. If you wrote the code, why are you using `feof` if you don't know what it does? – lurker Dec 04 '15 at 18:09

2 Answers2

0

Usually you may use the following steps:

  • Dump the whole text file to a char buffer.
  • Use strtok to split the char buffer to multiple tokens or words.
  • Use an array of pointer to char to store individual words.

Something along this line would do. Note, I use your question title as the text file. You will need to replace 20 as appropriately.

    int main ()
    {
        FILE *in_file;
        in_file = fopen("in.txt", "r");
        fseek( in_file, 0, SEEK_END );
        long fsize = ftell( in_file );
        fseek( in_file, 0, SEEK_SET );

        char *buf = malloc( fsize + 1 );
        fread( buf, fsize, 1, in_file ); // Dump the whole file to a char buffer.
        fclose( in_file );

        char *items[20] = { NULL };
        char *pch;

        pch = strtok (buf," \t\n");
        int i = 0;
        while (pch != NULL)
        {
            items[i++] = pch;
            pch = strtok (NULL, " \t\n");
        }

        for( i = 0; i < 20; i++ )
        {
            if( items[i] != NULL )
            {
                printf( "items[%d] = %s\n", i, items[i] );
            }
        }
        return 0;
    }

Output:

items[0] = Storing
items[1] = words
items[2] = from
items[3] = textfile
items[4] = into
items[5] = char
items[6] = array
items[7] = using
items[8] = feof?
artm
  • 17,291
  • 6
  • 38
  • 54
0
  1. Rather than check feof(), which tells you if the end-of-file occurred in the previous input operation, check the result of fscanf()

  2. Reads "words" with "%s" and limit the max numbers of char to be read.

    char buf[100];
    fscanf(in_file,"%99s",buf);
    

Putting that together:

    #define WORD_SIZE_MAX 20
    #define WORD_COUNT_MAX 10000

    char array[WORD_COUNT_MAX][WORD_SIZE_MAX];
    unsigned word_i = 0;

    for (i=0; i<WORD_COUNT_MAX; i++) {
      if (fscanf(in_file,"%19s", word_list[i]) != 1) {
        break;
      }
    }

Another approach is to use OP code nearly as is. Read the whole file into 1 array. Then on printing, skip white-space.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • hello thanks for the reply, what do you mean by reads words with %s? – anony Dec 03 '15 at 17:42
  • and why am i creating an array with rows and columns(wordcountmax and wordsizemax)? please help! – anony Dec 03 '15 at 17:43
  • @anony Each `word_list[i]` stores 1 word with up to `WORD_SIZE_MAX-1` characters. The fixed sized `array[][]` supports up to `WORD_COUNT_MAX` words. Other approaches exist involving memory management (`malloc()/free()`. I thought this would be simplest for you to code and use. – chux - Reinstate Monica Dec 03 '15 at 18:11
  • i mean, the numbers in my textfile max' characters a word could have is 5. (seven, three, eight). so would the word_size_max be 5 for me? i tried using your code but it is too confusing for me :/ which file exactly in your code does the scanning words into array business? – anony Dec 03 '15 at 18:27
  • @anony The above comment "... with up to `WORD_SIZE_MAX-1` characters." implies that if the longest word is 5 characters, then `WORD_SIZE_MAX` needs to be _at least_ 6. In this answer `infile` is the `FILE` pointer, used just like in your post. The `fscanf()` performs the scanning. – chux - Reinstate Monica Dec 03 '15 at 18:48
  • http://textuploader.com/5gde8 this is my code.. i think i confused myself with it :/ – anony Dec 03 '15 at 19:14
  • after a lot of time looking at this, i think i've finally figured it out, thanks very much for the help! – anony Dec 04 '15 at 00:18
  • @anony Ref code should be `if (fscanf(in_file,"%5s", array[i]) != 1) {` 5 not 19 as you have `char array[45000][6];` and not `char array[45000][20];` – chux - Reinstate Monica Dec 04 '15 at 03:15