Thread: extracting words from a file

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    extracting words from a file

    hi,I got a text file,sample of it is below:

    dog cat art
    cat dog
    art


    I would like to write a program,to read the file and print out the words,one by one.So the output would be:

    dog
    cat
    art
    cat
    dog
    art

    What command to use?I tried to use fgets and fscanf without much sucess.Any guide provided would be much appreciated.
    Thanks-Rahul

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by rahulsk1947
    What command to use?I tried to use fgets and fscanf without much sucess.Any guide provided would be much appreciated.
    Post what you tried.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    Code:
    int main(int argc, char* argv[]){
    	
    	FILE *fp;
    	char a[80];
    	fp=fopen("z.txt","r");
    
    	while(a!=NULL){
    		fscanf(fp,"%s",a);
    		printf("%s-",a);
    	}
    
    }
    it prints the last word endlessly.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by rahulsk1947
    Code:
    int main(int argc, char* argv[]){
    	
    	FILE *fp;
    	char a[80];
    	fp=fopen("z.txt","r");
    
    	while(a!=NULL){
    		fscanf(fp,"%s",a);
    		printf("%s-",a);
    	}
    
    }
    it prints the last word endlessly.
    Understand the function you are calling.

    How about checking for success?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          char word[80];
          while ( fscanf(file, "%79s", word) == 1 )
          {
             puts(word);
          }
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    Quote Originally Posted by Dave_Sinkula
    Code:
    while ( fscanf(file, "%79s", word) == 1 )
    The "%79s", whats the use of it? (string composed of 79 characters?)
    (Excuse me if I replied with a question, im just curious. Hope u'll understand)
    Last edited by Xero; 08-24-2006 at 10:06 AM.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    It's actually much simpler to use a FSM-style program for this:

    Code:
    1. Read CHAR from INPUT file
    2. If CHAR -> [non-whitespace], write CHAR to OUTPUT file and set IS_WORD to TRUE
    3. If CHAR -> [whitespace]
    4.    If IS_WORD is TRUE, write newline to OUTPUT file and set IS_WORD to FALSE
    5.    If IS_WORD is FALSE, do nothing
    6. Go back to (1)
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Xero, please don't hijack threads. And did you do a online search? There are a lot of reference pages for scanf() and its variants.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    Quote Originally Posted by jafet
    Xero, please don't hijack threads. And did you do a online search? There are a lot of reference pages for scanf() and its variants.
    I apologise.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Xero
    The "%79s", whats the use of it? (string composed of 79 characters?)
    (Excuse me if I replied with a question, im just curious. Hope u'll understand)
    It limits the string read to 79 characters. (Excluding the NULL.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM