Answer to Question #244976 in C for harry

Question #244976

Write a C program the `cp’ program that takes arguments as source and destination

filenames and copies the source to the destination. You need to use the functions fread

and fwrite. Note that the file may or may not be in text format.

Using of the function fread and fwrite is must, please solve using these two functions.


1
Expert's answer
2021-09-30T14:23:22-0400
#include<stdio.h>
#include<string.h>

#define SIZE 1
#define NUMELEM 5

int main(void) {
    FILE* fd = NULL;
    char buff[100];
    memset(buff,0,sizeof(buff));
    fd = fopen("test.txt","rw+");
    if(NULL == fd) {
        printf("\n fopen() Error!!!\n");
        return 1;
    }
    printf("\n File opened successfully through fopen()\n");
    if(SIZE*NUMELEM != fread(buff,SIZE,NUMELEM,fd)) {
        printf("\n fread() failed\n");
        return 1;
    }
    printf("\n Some bytes successfully read through fread()\n");
    printf("\n The bytes read are [%s]\n",buff);
    if(0 != fseek(fd,11,SEEK_CUR)) {
        printf("\n fseek() failed\n");
        return 1;
    }
    printf("\n fseek() successful\n");
    if(SIZE*NUMELEM != fwrite(buff,SIZE,strlen(buff),fd)) {
        printf("\n fwrite() failed\n");
        return 1;
    }
    printf("\n fwrite() successful, data written to text file\n");
    fclose(fd);
    printf("\n File stream closed through fclose()\n");
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS