Inserting lines when copying file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • td0g03
    New Member
    • Jan 2007
    • 64

    Inserting lines when copying file.

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

    Code:
    void  copy  ()
    {
    //	Local Definitions 
    	int c;
    	int closeStatus;
    	FILE* spProverbs ;
    	FILE* spProverbsCopy;
    //	Statements 
     	printf("Begin file copy\n");
    
    	if (!(spProverbs = fopen ("Proverbs.TXT", "r")))
    		{
    		 printf("Error opening Proverbs.TXT for reading");
    		 exit (101);
    		} // if open input  
    	if (!(spProverbsCopy = fopen ("ProverbsCopy.TXT", "w")))
    		{
    		 printf("Error opening ProverbsCopy for writing");
    		 exit(201);
    		} // if open output 
    
    		while ((c = fgetc(spProverbs)) != EOF)
    		fputc(c, spProverbsCopy);
    
    	fclose(spProverbs);
    	closeStatus = fclose(spProverbsCopy);
    	if (closeStatus == EOF)
    	   {
    	    printf("File close error.\a\n");
    	    exit(200);
    	   } // if close error 
    	printf("File successfully created\n");
    
    	return;
    
    }// copy
    My question is do I insert numbers. Let me do an example

    Code:
    PROVERBS.TXT 
    
    Between the devil and the deep sea:  To choose between 
    two equally  bad  alternatives in  a  serious  dilemma.
    
    Where there's a will there's a way:  When a person 
    really wants to  do something,  he will  find a way of 
    doing  it.
    
    A friend in need is a friend indeed:  A friend 
    who  helps when one is in  trouble is a real friend.
    
    Discretion is the better part of valor:  If you say
    discretion is the better part of valor,  you  mean 
    that avoiding a dangerous or   unpleasant situation 
    is   sometimes the most  sensible thing to do.
    
    Great talkers are little doers: Those people who 
    talk a lot and are always  teaching others usually 
    do   not   do   much   work.
    
    
    Text after processing:
    
     1. Between the devil and the deep sea:  To choose between 
     2. two equally  bad  alternatives in  a  serious  dilemma.
     3. 
     4. Where there's a will there's a way:  When a person 
     5. really wants to  do something,  he will  find a way of    
     6. doing  it.
     7. 
     8. A friend in need is a friend indeed:  A friend 
     9. who  helps when one is in  trouble is a real friend.
    10.
    11. Discretion is the better part of valor:  If you say 
    12. discretion is the better part of valor,  you  mean 
    13. that avoiding a dangerous or   unpleasant situation 
    14. is   sometimes the most  sensible thing to do.
    15.
    16. Great talkers are little doers: Those people who 
    17. talk a lot and are always  teaching others usually 
    18. do   not   do   much   work.
    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.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.

    Comment

    • td0g03
      New Member
      • Jan 2007
      • 64

      #3
      Originally posted by Ganon11
      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.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Well, c == '\n' rather than c == \n, but yes, that would tell you if the character was a newline character.

        Comment

        Working...