Appending to a text file

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

Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Appending to a text file

 
0
  #1
Feb 14th, 2007
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. }

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?.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Appending to a text file

 
0
  #2
Feb 14th, 2007
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Appending to a text file

 
0
  #3
Feb 14th, 2007
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.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Appending to a text file

 
0
  #4
Feb 14th, 2007
Originally Posted by Aia View Post
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3281 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC