Thread: file transfer problem - Help!

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    file transfer problem - Help!

    I have this problem with my code, I can transfer text files from one directory to another directory. But when i change the file name to a jpeg file, the program just refuse to transfer my picture... I really cannot understand why? Can anyone help please? Thanks!

    Code:
    #include <stdio.h>
    
    int main()
    {
     	int i,j;
     	int success;
     	char buffer;
     	char *pbuffer = &buffer;
     	
    	FILE *source_fptr;             //file pointer
    	FILE *dest_fptr;
    			
    	source_fptr = fopen("../a/drama.jpg", "r");
    	if (source_fptr == NULL)
    		{
    		fprintf(stderr,"Error - Cannot open the source file\n");
    		exit(1);		//exits the program
    		}
    	dest_fptr = fopen("../b/copied_drama.jpg", "w");
    	if (dest_fptr == NULL)
    		{
    		fprintf(stderr,"Error - Cannot open the destination file\n");
    		exit(1);		//exits the program
    		}
    				
    	i=0;
    	while(1)
    	{
    		fread(pbuffer, 1, 1, source_fptr);	
    		if(feof(source_fptr))
    		break;
    		fwrite(pbuffer,1, 1, dest_fptr);
    		if(feof(source_fptr))
    	        break;
    		i++;
    	}	
    	//printf("\nThe number of loop iterations: %i\n", i);
    		
    	fclose(source_fptr);
    	fclose(dest_fptr);	
    	
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Use "rb" and "wb" for the file open modes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    feof() may not work right for non-text files. Why not just check the return value from fread().

    I would also suggest that you use a slightly bigger amount of data than 1 byte at a time, to make the copy more efficient.

    It is also entirely meaningless to check if source_fptr is eof after you've already checked the same thing - just because you wrote to the destination another file doesn't make the source file any shorter. Checking for eof on the ouput file is also quite meaningless - if you created a new, empty, file and just wrote the first byte to it, you are, by definition standing at the end of the file, so it would be true, if it wasn't for the fact that eof is only valid when you have done a read of the file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. read from file problem
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 08:33 AM
  3. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM