FY_BCA_C_SLIP 3_2





Slip no:3_2 C Program to Reverse the Contents of a File and Print it


#include<stdio.>

int main(int argc, char * argv[])
{
int i,cnt,f; char ch, ch1;

FILE *fp1, *fp2;
if (fp1 = fopen(argv[1], "r"))
{
printf("The FILE has been opened...\n"); fp2 = fopen(argv[2], "w");

cnt = fseek(fp1, -1L, 2); // makes the pointer fp1 to point at the last character of the file f = ftell(fp1);
printf("Number of characters to be copied %d\n",f);

while (cnt)
{
ch = fgetc(fp1); fputc(ch, fp2);

fseek(fp1, -2L, 1); // shifts the pointer to the previous character cnt--;
}
printf("\n**File copied successfully in reverse order**\n");
}
else
{
perror("Error occured\n");
}
fclose(fp1);
fclose(fp2);
}

No comments:

Post a Comment