By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Inserting lines when copying file.

P: 64
Hello, I am trying to write a code to copy a file which I can do as you can see below.

Expand|Select|Wrap|Line Numbers
  1. void  copy  ()
  2. {
  3. //    Local Definitions 
  4.     int c;
  5.     int closeStatus;
  6.     FILE* spProverbs ;
  7.     FILE* spProverbsCopy;
  8. //    Statements 
  9.      printf("Begin file copy\n");
  10.  
  11.     if (!(spProverbs = fopen ("Proverbs.TXT", "r")))
  12.         {
  13.          printf("Error opening Proverbs.TXT for reading");
  14.          exit (101);
  15.         } // if open input  
  16.     if (!(spProverbsCopy = fopen ("ProverbsCopy.TXT", "w")))
  17.         {
  18.          printf("Error opening ProverbsCopy for writing");
  19.          exit(201);
  20.         } // if open output 
  21.  
  22.         while ((c = fgetc(spProverbs)) != EOF)
  23.         fputc(c, spProverbsCopy);
  24.  
  25.     fclose(spProverbs);
  26.     closeStatus = fclose(spProverbsCopy);
  27.     if (closeStatus == EOF)
  28.        {
  29.         printf("File close error.\a\n");
  30.         exit(200);
  31.        } // if close error 
  32.     printf("File successfully created\n");
  33.  
  34.     return;
  35.  
  36. }// copy
  37.  
  38.  
My question is do I insert numbers. Let me do an example

Expand|Select|Wrap|Line Numbers
  1. PROVERBS.TXT 
  2.  
  3. Between the devil and the deep sea:  To choose between 
  4. two equally  bad  alternatives in  a  serious  dilemma.
  5.  
  6. Where there's a will there's a way:  When a person 
  7. really wants to  do something,  he will  find a way of 
  8. doing  it.
  9.  
  10. A friend in need is a friend indeed:  A friend 
  11. who  helps when one is in  trouble is a real friend.
  12.  
  13. Discretion is the better part of valor:  If you say
  14. discretion is the better part of valor,  you  mean 
  15. that avoiding a dangerous or   unpleasant situation 
  16. is   sometimes the most  sensible thing to do.
  17.  
  18. Great talkers are little doers: Those people who 
  19. talk a lot and are always  teaching others usually 
  20. do   not   do   much   work.
  21.  
  22.  
  23. Text after processing:
  24.  
  25.  1. Between the devil and the deep sea:  To choose between 
  26.  2. two equally  bad  alternatives in  a  serious  dilemma.
  27.  3. 
  28.  4. Where there's a will there's a way:  When a person 
  29.  5. really wants to  do something,  he will  find a way of    
  30.  6. doing  it.
  31.  7. 
  32.  8. A friend in need is a friend indeed:  A friend 
  33.  9. who  helps when one is in  trouble is a real friend.
  34. 10.
  35. 11. Discretion is the better part of valor:  If you say 
  36. 12. discretion is the better part of valor,  you  mean 
  37. 13. that avoiding a dangerous or   unpleasant situation 
  38. 14. is   sometimes the most  sensible thing to do.
  39. 15.
  40. 16. Great talkers are little doers: Those people who 
  41. 17. talk a lot and are always  teaching others usually 
  42. 18. do   not   do   much   work.
  43.  
  44.  
I can copy the file fine, but really have no clue how to insert numbers in the first of each line. If anyone could give me a clue or example that would be great. Thanks in advance.
Mar 9 '07 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
Well, you can check if a character is a newline. If it is, after you output it to the copied file, you can output a number plus ".)". You will have to keep track of how many lines you have printed with a variable - starting at 2 (do you know why?). Also, you will have to manually output "1.)" to the file before anything else is output.
Mar 9 '07 #2

P: 64
Well, you can check if a character is a newline. If it is, after you output it to the copied file, you can output a number plus ".)". You will have to keep track of how many lines you have printed with a variable - starting at 2 (do you know why?). Also, you will have to manually output "1.)" to the file before anything else is output.
Hello, I'm sorta comfused. Anyway you could provide me with an example? Thanks again.

new line would "c == \n" that would tell me if it would be a new line or not? Sorry, I'm really confused on this problem. Been stuck on it for a few hours now.
Mar 9 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Well, c == '\n' rather than c == \n, but yes, that would tell you if the character was a newline character.
Mar 10 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.