Write to a file the first word in each line from another file

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

Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Write to a file the first word in each line from another file

 
0
  #1
Oct 27th, 2009
Hey

I have a file named filetext.txt with the text:

One word
Two words
Three words
Four words

and the program should read this filetext.txt and output to another file called filecopied.txt this text:

One
Two
Three
Four

I have this so far:

  1. #include <assert.h>
  2. #include <complex.h>
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fenv.h>
  6. #include <float.h>
  7. #include <inttypes.h>
  8. #include <iso646.h>
  9. #include <limits.h>
  10. #include <locale.h>
  11. #include <math.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include <stdarg.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <wchar.h>
  23. #include <wctype.h>
  24.  
  25.  
  26.  
  27. int main()
  28. {
  29. FILE *filetext
  30. FILE *filecopied;
  31. char text[50];
  32.  
  33. filetext=fopen("filetext.txt","r");
  34. filecopied=fopen("filecopied.txt","w");
  35.  
  36. if ((filetext==NULL) || (filecopied==NULL))
  37. {
  38. printf ("A file cannot be opened");
  39. return 1;
  40. }
  41.  
  42. // fgets(text,50,filetext);
  43. //sscanf("%s ",text);
  44. sscanf("%s\n",fgets(text,50,filetext));
  45. //sscanf("%s\n",fgets(text,50,filetext));
  46. fprintf(filecopied,"%s",text);
  47. fclose(filecopied);
  48. fclose(filetext);
  49. return 0;
  50. }

It doesnt write anything to the file so how can I solve it? Id like to use fgets, fprintf, and sscanf. Thanks.
Last edited by riahc3; Oct 27th, 2009 at 3:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #2
Oct 27th, 2009
int main()
{
   FILE *filetext;
   FILE *filecopied;
   char text[50];

   filetext=fopen("filetext.txt","r");
   filecopied=fopen("filecopied.txt","w");

   if ( (filetext==NULL) || (filecopied==NULL) )
   {
      printf ("A file cannot be opened");
      return 1;
   }

   while ( fgets(text, sizeof text, filetext) )
   {
      char first[10];
      if ( sscanf(text, "%9s", first) == 1 )
      {
         fprintf(filecopied, "%s\n", first);
      }
   }
   fclose(filecopied);
   fclose(filetext);
   return 0;
}
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #3
Oct 27th, 2009
  1. #include <assert.h>
  2. #include <complex.h>
  3. #include <ctype.h>
  4. #include <errno.h>
  5. #include <fenv.h>
  6. #include <float.h>
  7. #include <inttypes.h>
  8. #include <iso646.h>
  9. #include <limits.h>
  10. #include <locale.h>
  11. #include <math.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include <stdarg.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <wchar.h>
  23. #include <wctype.h>
It is always good to learn which headers contain which declarations. Including everything under the sun when you use only a small fraction of it is rarely a good idea.

It doesnt write anything to the file so how can I solve it?
Double check the way sscanf() is supposed to be called. What you want are two strings, one for input and one for output:
  1. char line[512];
  2. char word[512];
  3.  
  4. while (fgets(line, sizeof line, filetext))
  5. {
  6. if (sscanf(line, "%511s", word) == 1)
  7. {
  8. fprintf(filecopied, "%s\n", word);
  9. }
  10. }
Last edited by Tom Gunn; Oct 27th, 2009 at 3:30 pm. Reason: Saving myself from a nitpick.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training
 
-1
  #4
Oct 28th, 2009
I think both of you just limit in another array the amount of chars in a char array when I want it to read/write the first letters then when it detects the first space (" ") and when it detects a "\0", it should just to the next line again the first space and again a "\0" until it reaches EOF...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training
 
0
  #5
Oct 29th, 2009
Maybe I didnt understand someone correctly...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #6
Oct 29th, 2009
Id like to use fgets, fprintf, and sscanf.
I want it to read/write the first letters then when it detects the first space (" ") and when it detects a "\0", it should just to the next line again the first space and again a "\0" until it reaches EOF
These wants do not jive. If you use fgets() alone, you have no choice but to provide a buffer large enough to hold the maximum expected line length. If you use sscanf() to extract a word, you have no choice but to provide a buffer large enough to hold the maximum expected line length because the whole line could be one big word.

You can skip using sscanf() entirely by searching for the first whitespace in the line and save the memory for the second buffer:
  1. char line[512];
  2.  
  3. while (fgets(line, sizeof line, filetext))
  4. {
  5. size_t x = 0;
  6.  
  7. while (line[x] && !isspace(line[x])) putc(line[x], filecopied);
  8.  
  9. putc('\n', filecopied);
  10. }
The algorithm from the second quote is a viable option, but I would need some convincing to believe that it is a better option than fgets() and sscanf(). Right now I am not sure what it is you want. Dave and I have both given you solutions that match your functional requirement of saving the first word in each line.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC