944,167 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 627
  • C RSS
Oct 27th, 2009
0

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

Expand Post »
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.
Similar Threads
Reputation Points: 49
Solved Threads: 1
Posting Whiz
riahc3 is offline Offline
306 posts
since May 2008
Oct 27th, 2009
0
Re: Write to a file the first word in each line from another file
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;
}
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 27th, 2009
0
Re: Write to a file the first word in each line from another file
Quote ...
  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.

Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 28th, 2009
-1
Re: Write to a file the first word in each line from another file
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...
Reputation Points: 49
Solved Threads: 1
Posting Whiz
riahc3 is offline Offline
306 posts
since May 2008
Oct 29th, 2009
0
Re: Write to a file the first word in each line from another file
Maybe I didnt understand someone correctly...
Reputation Points: 49
Solved Threads: 1
Posting Whiz
riahc3 is offline Offline
306 posts
since May 2008
Oct 29th, 2009
0
Re: Write to a file the first word in each line from another file
Quote ...
Id like to use fgets, fprintf, and sscanf.
Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009

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: Figuring out redirection of a child process to a file
Next Thread in C Forum Timeline: while condition won't work properly against \n





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


Follow us on Twitter


© 2011 DaniWeb® LLC