943,788 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2317
  • C RSS
Apr 19th, 2009
0

Reading words from file

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bemo55 is offline Offline
23 posts
since Oct 2008
Apr 19th, 2009
0

Re: Reading words from file

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Apr 19th, 2009
0

Re: Reading words from file

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.
Reputation Points: 11
Solved Threads: 6
Junior Poster
jen140 is offline Offline
116 posts
since Jan 2009
Apr 19th, 2009
0

Re: Reading words from file

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Apr 19th, 2009
0

Re: Reading words from file

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bemo55 is offline Offline
23 posts
since Oct 2008
Apr 19th, 2009
0

Re: Reading words from file

p.s. My file has 63 strings.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bemo55 is offline Offline
23 posts
since Oct 2008
Apr 19th, 2009
0

Re: Reading words from file

Nevermind. I got the tokenizing function to work..Now ive just got to implement my palindrone function. Thanks for the help guys.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bemo55 is offline Offline
23 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Basic Prob in C
Next Thread in C Forum Timeline: You'd think I would know how to use strings by now, but...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC