944,149 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7977
  • C RSS
Feb 14th, 2007
0

Appending to a text file

Expand Post »
I'm at the point of learning how to write to a file. I created these lines as a test.
My concern is that it doesn't look right that I have to write the same sentences for displaying it to screen and to put it in file.

  1.  
  2. /*
  3.  * data.c
  4.  * test opening and appending to
  5.  * a file.
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. int read_int(int *result);
  11. char *read_string(char *string, int size);
  12.  
  13. int main(void)
  14. {
  15. int n1, n2;
  16. char name[20];
  17. FILE *file_ptr;
  18.  
  19. file_ptr = fopen("data.txt", "a");
  20. if(!file_ptr)
  21. {
  22. printf("Error opening file");
  23. return(1);
  24. }
  25. else
  26. {
  27. fprintf(file_ptr,"What's your name?: ");
  28. fprintf(stdout,"What's your name?: ");
  29. read_string(name, sizeof name / sizeof *name);
  30. fprintf(file_ptr,"%s\n", name);
  31. }
  32. printf("Enter value for first number: ");
  33. fflush(stdout);
  34. read_int(&n1);
  35. printf("Enter value for second number: ");
  36. fflush(stdout);
  37. read_int(&n2);
  38.  
  39. fprintf(file_ptr,"n1 = %d\nn2 = %d\n", n1, n2);
  40. fclose(file_ptr);
  41.  
  42. system("PAUSE");
  43.  
  44. return(0);
  45. }
  46.  
  47. int read_int(int *result)
  48. {
  49. char ch, buffer[13];
  50. return fgets(buffer, sizeof buffer, stdin) && !isspace(*buffer) &&
  51. sscanf(buffer, "%d%c", result, &ch) == 2 && (ch == '\0' || ch == '\n');
  52. }
  53.  
  54. char *read_string(char *string, int size)
  55. {
  56. if(fgets(string, size, stdin) != NULL)
  57. {
  58. int len = strlen(string)-1;
  59. if(string[len] == '\n')
  60. {
  61. string[len] = '\0';
  62. }
  63. else
  64. {
  65. int ch;
  66. do
  67. {
  68. ch = getchar();
  69. }
  70. while(ch != '\n' && ch != EOF);
  71. }
  72. }
  73. return string;
  74. }

Quote ...
fprintf(file_ptr,"What's your name?: ");
fprintf(stdout,"What's your name?: ");
Is there another better way of doing this so I don't have to repeat so much code every time?.
Similar Threads
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Feb 14th, 2007
0

Re: Appending to a text file

You asked this question already.

What you should do is create a function that does the screen and file printing; all you have to do is pass the string and the file as arguments.

I was thinking something like this:
  1. void printToFileAndScreen(FILE *filePtr, char *myString) {
  2.  
  3. if (myString) {
  4. fprintf(stdout, myString);
  5. fprintf(filePtr, myString);
  6. }
  7. }

Note that this fprintf() wrapper is not nearly as robust as the fprintf() function; it certainly doesn't accept any extra parameters. However, this should help to reduce the bulk of your code by a bit.

Additionally, do not forgot to flush the streams, either in this function, or in the main() function like you were already doing.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 14th, 2007
0

Re: Appending to a text file

Yes, I asked a question related to this topic, and this piece of code is the result of what I understood from you and some more reading.
Sorry, I didn't want to bother you. I didn't want you to write me a function, I just wanted confirmation that what I was understanding is correct code.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Feb 14th, 2007
0

Re: Appending to a text file

Click to Expand / Collapse  Quote originally posted by Aia ...
Sorry, I didn't want to bother you. I didn't want you to write me a function, I just wanted confirmation that what I was understanding is correct code.
That's OK.

Anyway, you have to keep several things in mind with C/C++: Don't assume anything. If it doesn't look like it's going to do something, don't assume it does. An example of this is memory management. Well, actually C takes care of any statically allocated variables, but as soon as you start using dynamic memory... you're on your own!

What does this mean for you? It usually means that there's no easy prewritten function for any semi-complex task you want to accomplish. Most often you have to implement it yourself, as is the case here.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

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: function calling problem
Next Thread in C Forum Timeline: turning userinput into a text file





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


Follow us on Twitter


© 2011 DaniWeb® LLC