Copying a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sohil2520
    New Member
    • Mar 2007
    • 2

    Copying a file

    I have just started programming in c
    i started to make a program to copy contents from one file to another charcter by character. i made the prog but it is not working..
    can u plz tell me the error in the code..?
    my code is
    Code:
    #include <stdio.h>
    
    main()
    
    {
    
    char a;
    
    
    FILE *fi, *fo;
    
    
    fi= fopen ("sohil", "r");
    
    fo= fopen ("ab.c", "w");
    
    while (1)
    	
    	{
    
    	fscanf (fi, "&c", &a);
    	
    	if (a==EOF)
    
    			{
    	
    		 break;
    		
    			}
    
    	else
    		
    		{	
    		
    		fprintf (fo, "%c", a);
    	
    		}
    
    	}
    
    fclose (fi);
    
    fclose (fo);
    
    }
    Last edited by bartonc; Mar 4 '07, 10:42 PM. Reason: added [code][/code] tags
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi. this is the introductions forum. Welcome to TSDN. You will have a much better chance at getting a reply to your question if you post it in the relevant forum which you will see to the right of this message. Just click on c++/c and copy your message there.

    Comment

    • bluesteel
      New Member
      • Mar 2007
      • 84

      #3
      Originally posted by sohil2520
      I have just started programming in c
      i started to make a program to copy contents from one file to another charcter by character. i made the prog but it is not working..
      can u plz tell me the error in the code..?
      my code is
      Code:
      #include <stdio.h>
      
      main()
      
      {
      
      char a;
      
      
      FILE *fi, *fo;
      
      
      fi= fopen ("sohil", "r");
      
      fo= fopen ("ab.c", "w");
      
      while (1)
      	
      	{
      
      	fscanf (fi, "&c", &a);
      	
      	if (a==EOF)
      
      			{
      	
      		 break;
      		
      			}
      
      	else
      		
      		{	
      		
      		fprintf (fo, "%c", a);
      	
      		}
      
      	}
      
      fclose (fi);
      
      fclose (fo);
      
      }
      I would recommend doing it this way:
      1- Open Files. The original and the copy.
      2- Declare a character var.
      3- Using fgetc() and fwrite() copy content from one to the other
      4- close files and you are done.

      If this does not get banned, i will post my idea.

      #include libraries //don't remember which ones

      main(){
      char a;
      FILE *first,*second;
      first=fopen("Fi rst.txt","r");
      second=fopen("S econd.txt","w") ;
      while(!feof(fir st)){
      a=fgetc(first);
      fwrite(&a,1,1,s econd);
      }
      }

      Comment

      • sohil2520
        New Member
        • Mar 2007
        • 2

        #4
        got ur idea dude...thnx...t here's still 1 doubt tho...
        what do the two 1's in fwrite() imply..??

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Changed title.
          Please use a more problem specific title.

          Comment

          • bluesteel
            New Member
            • Mar 2007
            • 84

            #6
            Originally posted by sohil2520
            got ur idea dude...thnx...t here's still 1 doubt tho...
            what do the two 1's in fwrite() imply..??
            fwrite(&a,1,1,f ile);
            The first parameter is what to be written
            The second One is how many bytes are needed for it. Remember a character is a byte, so if you want to write "hello" you have to write 5, otherwise it will split your word. If you write a higher number you will see unexpected characters. The third parameter is how many times you want to write the specified text. The last is the file you want to write in

            Comment

            • bluesteel
              New Member
              • Mar 2007
              • 84

              #7
              Originally posted by sohil2520
              got ur idea dude...thnx...t here's still 1 doubt tho...
              what do the two 1's in fwrite() imply..??
              Remember to write & before your variable or string.
              If you want to write a fixed string like "hello" all the times, you have
              to write the word between quotes (").
              fwrite(&filecon tent,5,1,file);
              fwrite("Hello World",11,1,fil e);

              Hope this helps!

              Comment

              Working...