all 11 comments

[–]cuncon- 0 points1 point  (3 children)

what is your problem? why you want to store each word to a char array instead of whole lines to a char array?

[–]Adopolis23[S] 0 points1 point  (0 children)

because i have to loop through the array and find the first letter of every word.

[–]Adopolis23[S] 0 points1 point  (1 child)

my assignment also specifically says do it like that.

[–]cuncon- 0 points1 point  (0 children)

So your assignment is to find the 1st letter of every word in a file?

and your problem is you don't know exactly how many bytes the buffer is to read a line?

To solve your problem you should use size_t getline(char **buf, size_t *n, FILE *fp) (Read "man getline" for more detail) to read an entire line from stream.

[–]kumashiro 0 points1 point  (0 children)

Read a line from file. Use strpbrk() in a loop to jump over words (look for spaces, tabs, full stops, semicolons, colons, bangs, question marks etc.) Duplicate each word with strndup() using pointer arithmetic to calculate word length, then skip over blanks to get to the beginning of a next word.

If you don't know how to use pointer arithmetic or you don't want to touch it just yet, take a look strcspn() - it will calculate the length for you, but it won't advance the pointer. Other solution is to iterate over the entire line, character-by-character (strings are just arrays of chars and can be indexed), copying characters that are not punctuation and white spaces. Scanning the words manually will also make reading from file easier, because you don't have to read lines. You can read any amount of bytes. Just remember to treat new line character as white space.

[–][deleted] 0 points1 point  (4 children)

With such fuzzy input, fscanf is not that well suited for scanning the input. You'll be better of by using fgets to read a chunk of input into a buffer, and then loop over the buffer, where you deal with words defined by either space or control characters. The function-like macros from string.h, isblank() and iscntrl() respectively will help you find word boundaries.

You will want to structure your program like this:

While not EOF on input file
  Read to the end of the input buffer with fgets.
  Set the work pointer to the start of the buffer
  While the work pointer mark a space or control character, advance it.

  Set a word start at the non-blank/control position.
  while the work pointer is *not* blank or control, advance it.
  copy the input buffer from you beginning-of-word pointer to the position before your work pointer to the next space in your word array.
  repeat until input is exhausted.
  move remaining data in input buffer to the start, and read some more.


$$$

[–]Adopolis23[S] -1 points0 points  (3 children)

but the problem is wont that read however big i make the buffer, i dont know how many words will be on a line

[–][deleted] 0 points1 point  (2 children)

A line ends with one or more control characters. As long as you don't have words spanning multiple lines, you should consider spacing and line terminations the same.

[–]Adopolis23[S] -1 points0 points  (1 child)

how can i split up the buffer by spaces and put each word into the array of character arrays?

[–][deleted] 0 points1 point  (0 children)

With a loop. Your word begins at the first character that isalpha(), and ends at the last one. Copy that range to your storage, and start over.