Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Reading Words from File

Reply
Thread Tools

Reading Words from File

 
 
Mabden
Guest
Posts: n/a
 
      10-04-2005
"Michael Mair" <> wrote in message
news:...
> 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:


Interesting. No one has ever thought of doing that before. Where did you
come up with such a great idea for a program? It's unlike anything I've
ever heard of...

> > 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.

>
> realloc() helps you do that.
> Have a look at the comp.lang.c archives to see how to use it.



That would be like studying. If he wanted to study he would go to
school.

>
> If you do not need the words in context, you also use getc() which
> may be clearer:
>


<Homework answers snipped>

Nice job you get him an A-.

--
Mabden


 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      10-05-2005
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>>
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      10-05-2005
Mabden wrote:
> "Michael Mair" <> wrote in message
> news:...
>

[snip]

>>If you do not need the words in context, you also use getc() which
>>may be clearer:

>
> <Homework answers snipped>
>
> Nice job you get him an A-.


The original message was not too obviously a homework question
to me and contained a first shot at the problem, so I decided
to give the OP the benefit of doubt. If "dough" posts something
like that again or does not respond to the answer he or she got
in this thread, I won't.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Words and non-words, according to Microsoft et al Steve B NZ Computing 11 03-21-2008 11:52 PM
Replace stop words (remove words from a string) BerlinBrown Python 6 01-17-2008 02:37 PM
Words Words utab C++ 6 02-16-2006 07:00 PM
Non-noise words are incorrectly recognised as noise words. Peter Strĝiman ASP .Net 1 08-23-2005 01:26 PM
Re: A little bit of help regarding my linked list program required. - "words.c" - "words.c" Richard Heathfield C Programming 7 10-05-2003 02:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57