On 4 Oct 2005 11:39:39 -0700, "dough" <> wrote:
>I want to read in lines from a file and then seperate the words so i
>can do a process on each of the words. Say the text file "readme.txt"
>contains the following:
It would be nice if you mentioned what your problem was.
snip
>Heres what I have so far.
>
>#include <ctype.h>
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>
>void process(char *s) /* whats here is not really important *
>{
> printf("%s", s);
>}
>
>int main() {
>
>char buffer[80];
>FILE *f = fopen("readme.txt", "r");
>char *s;
>
>while( fgets(buffer, sizeof(buffer), f) != NULL ) /* reads a line */
>{
> while( sscanf(buffer, "%s", s) ) /* scans for words in line */
s doesn't point anywhere sscanf can write to. This invokes undefined
behavior.
> {
> process(s); /* do stuff to the words */
> }
>}
>
>fclose(f);
>return 0;
>
>}
>
>Also, is there anyway to adjust the size of the buffer or reallocate
>the memory so it doesn't overflow and get a seg error.
The seg error you experience has nothing to do with buffer, since you
never overflow it. It has everything to do with failing to have s
point somewhere.
<<Remove the del for email>>
|