Exercise 24 Harshkumar 23
Exercise 24 Harshkumar 23
Output:
Unable to open file.
Please check whether file exists and you have read privilege.
2) Write a C program to copy the contents of one file to
another
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
fclose(fptr1);
fclose(fptr2);
return 0;
}
Output:
Enter the filename to open for reading
Harsh
Cannot open file Harsh
___________________________________________________________________
if (fp == NULL)
{
printf("Unable to open file\n");
return;
}
char buf[100];
int a[MAX], s = 0, c = 0, l;
while (!feof(fp))
{
fgets(buf, sizeof(buf), fp);
l = strlen(buf);
a = s += l;
}
rewind(fp);
c -= 1;
while (c >= 0)
{
fseek(fp, a, 0);
fgets(buf, sizeof(buf), fp);
printf("%s", buf);
c--;
}
return;
}
int main()
{
char x[] = "newfile.txt";
reverseContent(x);
return 0;
}
Output:
My
name is
HARSH KUMAR
HARSH KUMAR
name is
My
4) Two files DATA1 and DATA2 contain sorted lists of integers. Write a C program
to merge the contents of two files into a third file DATA i.e., the contents of the
first file followed by those of the second are put in the third file.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
FILE *fp3 = fopen("file3.txt", "w");
char c;
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open files");
exit(0);
}
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
printf("Merged DATA1.txt and DATA2.txt into DATA.txt");
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
Output:
Merged DATA1.txt and DATA2.txt into DATA.txt
5) Write a program that merges lines alternately from two files and writes the
results to new file. If one file has less number of lines than the other, the
remaining lines from the larger file should be simply copied into the target file.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f1, *f2, *fp;
char ch1 = 'a', ch2 = 'a';
f1 = fopen("File (e)1.txt", "r");
f2 = fopen("File (e)2.txt", "r");
fp = fopen("File (e)3.txt", "w");
if (f1 == NULL)
{
printf("Can't open the file1\n");
exit(1);
}
if (f2 == NULL)
{
printf("Can't open the file1\n");
exit(2);
}
puts("\nWork on progress\n.\n.\n.\n.\n");
while (1)
{
if (ch1 != EOF)
{
ch1 = fgetc(f1);
/*A line is ends when a . is encounter*/
while (ch1 != '.')
{
if (ch1 == EOF)
break;
fputc(ch1, fp);
ch1 = fgetc(f1);
}
if (ch1 != EOF)
fputc('.', fp);
}
if (ch2 != EOF)
{
ch2 = fgetc(f2);
while (ch2 != '.')
{
if (ch2 == EOF)
break;
fputc(ch2, fp);
ch2 = fgetc(f2);
}
if (ch2 != EOF)
fputc('.', fp);
}
if (ch1 == EOF && ch2 == EOF)
break;
}
printf("\nTask completed.\nExiting . . . \n");
return 0;
}
PROGRAM:
#include <stdio.h>
void main()
{
char ch;
int count = 0;
FILE *fptr;
fptr = fopen("text.txt", "w");
if (fptr == NULL)
{
printf("File can't be created\a");
exit(0);
}
printf("Enter some text and press enter key:\n");
while ((ch = getche()) != '\r')
{
fputc(ch, fptr);
}
fclose(fptr);
fptr = fopen("text.txt", "r");
printf("\nContents of the File is:");
while ((ch = fgetc(fptr)) != EOF)
{
count++;
printf("%c", ch);
}
fclose(fptr);
printf("\nThe number of characters present in file is: %d", count);
}
int main()
{
FILE *fPtr1;
FILE *fPtr2;
char path1[100];
char path2[100];
int diff;
int line, col;
if (diff == 0)
{
printf("\nBoth files are equal.");
}
else
{
printf("\nFiles are not equal.\n");
printf("Line: %d, col: %d\n", line, col);
}
fclose(fPtr1);
fclose(fPtr2);
return 0;
}
int compareFile(FILE *fPtr1, FILE *fPtr2, int *line, int *col)
{
char ch1, ch2;
*line = 1;
*col = 0;
do
{
ch1 = fgetc(fPtr1);
ch2 = fgetc(fPtr2);
if (ch1 == '\n')
{
*line += 1;
*col = 0;
}
if (ch1 != ch2)
return -1;
*col += 1;
} while (ch1 != EOF && ch2 != EOF);
int main()
{
FILE *fPtr;
char ch;
if (fPtr == NULL)
{
printf("Unable to open file.\n");
printf("Please check whether file exists and you have read privilege.\
n");
exit(EXIT_FAILURE);
}
putchar(ch);
return 0;
}
Output:
File opened successfully. Reading file contents character by character.
___________________________________________________________________
___________________________________________________________________