Reading words from file

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 23
Reputation: bemo55 is an unknown quantity at this point 
Solved Threads: 0
bemo55 bemo55 is offline Offline
Newbie Poster

Reading words from file

 
0
  #1
Apr 19th, 2009
Hello. I was wondering how to read strings from a file word by word and store the words into an array in c. I am having to write a program that tests words to see of they are palindromes from a little paragraph i wrote in a file. I've been trying to figure it out all afternoon with no luck.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Reading words from file

 
0
  #2
Apr 19th, 2009
fgets and strtok would be good places to start. Do you know how many strings you will be reading? If you don't it will make the array part a little more difficult.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 106
Reputation: jen140 is an unknown quantity at this point 
Solved Threads: 5
jen140 jen140 is offline Offline
Junior Poster

Re: Reading words from file

 
0
  #3
Apr 19th, 2009
Here you have a small example :
  1. #include <stdio.h>
  2.  
  3. main()
  4. {
  5. FILE *fp;
  6.  
  7. fp = fopen("test.txt", "w");
  8. fscanf(fp, "%s", buffer);
  9. printf("Read Buffer: %s\n", %buffer );
  10. flcose(fp;);
  11.  
  12. }
To better understand fscanf try viewing : http://irc.essex.ac.uk/www.iota-six....nf_fprintf.asp
Last edited by jen140; Apr 19th, 2009 at 9:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Reading words from file

 
0
  #4
Apr 19th, 2009
I hate to sound like a prick but... using fscanf is generally a bad idea. There are plenty of reasons, but a big one is that most people don't really understand what its doing. Therefore it usually only works with carefully formatted input. Or by accident.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 23
Reputation: bemo55 is an unknown quantity at this point 
Solved Threads: 0
bemo55 bemo55 is offline Offline
Newbie Poster

Re: Reading words from file

 
0
  #5
Apr 19th, 2009
I have looked at strtok but I didnt think that it would work with data i am reading in. All the examples i seen were declaring the array as a certain string like
char array1=" your string here"
and then using strtok. I didn't really understand how to use strtok.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 23
Reputation: bemo55 is an unknown quantity at this point 
Solved Threads: 0
bemo55 bemo55 is offline Offline
Newbie Poster

Re: Reading words from file

 
0
  #6
Apr 19th, 2009
p.s. My file has 63 strings.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 23
Reputation: bemo55 is an unknown quantity at this point 
Solved Threads: 0
bemo55 bemo55 is offline Offline
Newbie Poster

Re: Reading words from file

 
0
  #7
Apr 19th, 2009
Nevermind. I got the tokenizing function to work..Now ive just got to implement my palindrone function. Thanks for the help guys.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC