ogs
ogs

Reputation: 1239

C programming - copy file to an other

I implemented the following function

int copy_files(char *source, char *destination)
{
    FILE *in_fd, *out_fd;

    char *buf;
    char buffer[512];

    time_t t;
    struct tm *td;

    long file_size;
    int result;
    int value;

    int status =  STATUS_ERROR;

    /* Open file for both reading and writing */
    in_fd = fopen(source, "w+");    //
    if(in_fd == NULL)
    {
       fputs ("File error",stderr);
       exit (1);
    }
    else
    {
        printf("writing in file...\n");

        strcpy(buffer, "Test writing in USB Key File \n");
        fprintf(in_fd, buffer); 
        printf("%s\n",buffer);
        t = time(NULL);
        td = gmtime((const time_t *)&t);
        sprintf(buffer, "Date %02d/%02d/%02d Time %02d:%02d:%02d\n", 
        td->tm_mday, td->tm_mon + 1, td->tm_year + 1900,
        td->tm_hour, td->tm_min, td->tm_sec);
        fprintf(in_fd, buffer);
        printf("%s\n",buffer);
        printf("Writing OK\n");
    }

    out_fd = fopen(destination,"w+");
    if(out_fd == NULL)
    {
        fputs ("File error",stderr);
        exit (1);
    }   
    else
    {   
        // obtain file size:
        fseek (in_fd , 0 , SEEK_END);
        file_size = ftell (in_fd);
        rewind (in_fd);

        // allocate memory to contain the whole file:
        buf = (char*) malloc (sizeof(char)*file_size);  

        while (!feof(in_fd)) 
        {
            /* Read  data */
            fread(buf, 1, (sizeof buf), in_fd);
            printf("%s", buf);
        }

        /* Write  data */
        fwrite(buf, 1, (sizeof buf), out_fd);
    } 
    fclose(in_fd);
    fclose(out_fd);
    return STATUS_OK;
}

Through this function, I would like to create a file, fill it with two lines and copy this entire contain to an other file.

Howerver I not understand why :

  1. The destination file is not created contrary to the source one.
  2. printf diplay :

    writing in file... Test writing in USB Key File Date 01/01/1970 Time 00:45:46 Writing OK Testh��v wrih��vtingh��v in h��vUSB h��vKey h��vFileh��v Dah��vte 0h��v1/01h��v/197h��v0 Tih��vme 0h��v0:45h��v:46 h��v:46

How to resolve the issues ?

EDIT :

About the first question I do not understand why the following code :

strcat(temp_dest, direntp->d_name);
printf("after strcat temp_dest=%s\n", temp_dest);         
strcat(temp_src, direntp->d_name);
printf("after strcat temp_src=%s\n", temp_src); 
printf("destination:%s\n",temp_dest);
copy_files(temp_src,temp_dest);

returns :

after strcat temp_dest=/tmp/usb/test10
after strcat temp_src=/media/sda1/test10
destination:10

Why 10 ? :/

Upvotes: 1

Views: 184

Answers (2)

Saurabh Meshram
Saurabh Meshram

Reputation: 8416

Correction in your fread().

Since you already know the filesize.
No need to loop, one call to fread reading file_size bytes is sufficient.

else                                                                        
{                                                                           
    // obtain file size:                                                    
    fseek (in_fd , 0 , SEEK_END);                                           
    file_size = ftell (in_fd);                                              
    rewind (in_fd);                                                         

    // allocate memory to contain the whole file:                           
    buf = malloc (file_size);                                               

    /* Read file_size no of bytes */                                                        
    fread(buf, 1, file_size, in_fd);                                      
    buf[file_size -1] = 0;                                                  
    printf("File content after Reading :[%s]", buf);                        

    /* Write  data */                                                       
    fwrite(buf, 1, file_size, out_fd);                                      
}

Other Correction in your code.
You are missing %s (second argument) in fprintf.

...
else                                                  
{                                                                           
    printf("writing in file...\n");                                         

    strcpy(buffer, "Test writing in USB Key File \n");                      
    fprintf(in_fd, "%s", buffer); 
    ...                                 

    fprintf(in_fd, "%s", buffer);                                      
    printf("Writing OK\n");     
}

Upvotes: 2

ElderBug
ElderBug

Reputation: 6145

Since you are checking the file size before reading, you don't need to loop and check for EOF.

The real problem here is just that you read and write using sizeof(buf), but buf is just a pointer, so its size is 4 bytes (or 8). What you need to use is sizeof(char)*file_size, which is the real size of your buffer.

Upvotes: 1

Related Questions