Merges Lines Alternatively from 2 Files & Print
Result
#include
#include
Void main()
{
char file1[10], file2[10];
puts("enter the name of file 1");
scanf("%s", file1);
puts("enter the name of file 2");
scanf("%s", file2);
FILE *fptr1, *fptr2, *fptr3;
fptr1=fopen(file1, "r");
fptr2=fopen(file2, "r");
fptr3=fopen("merge2.txt", "w+");
char str1[200];
char ch1, ch2;
int n = 0, w = 0;
while (((ch1=fgetc(fptr1)) != EOF) && ((ch2 = fgetc(fptr2)) != EOF))
{
if (ch1 != EOF)
{
ungetc(ch1, fptr1);
fgets(str1, 199, fptr1);
fputs(str1, fptr3);
if (str1[0] != 'n')
n++;
}
if (ch2 != EOF)
{
ungetc(ch2, fptr2);
fgets(str1, 199, fptr2);
fputs(str1, fptr3);
if (str1[0] != 'n')
n++;
}
}
rewind(fptr3);
while ((ch1 = fgetc(fptr3)) != EOF)
{
ungetc(ch1, fptr3);
fscanf(fptr3, "%s", str1);
if (str1[0] != ' ' || str1[0] != 'n')
w++;
}
fprintf(fptr3, "\n\n number of lines = %d n number of words is = %d\n", n, w - 1);
fclose(fptr1);
fclose(fptr2);
fclose(fptr3);
}
Output
enter the name of file 1
c.txt
enter the name of file 2
a.txt
$ vi merge2.txt
All participants will be provided with 1:1 Linux Systems for Lab work. If participants want, they can bring their own laptops with Linux in it. This enable them to do lot of quality assignments even Sanfoundry internship programs are great learning opportunities.
Students with proven credentials only are enrolled for this program and the duration of these programs ranges from 2-6 months full t after the classes are over. If they have Windows, we install virtualization software and Ubuntu Linux virtual appliance on top of windows system.
ime. Student must be passionate about Technology topics. As part of Sanfoundry's biggest open learning initiative, we are looking for interns (student or working professional) in following technolog
number of lines = 4
number of words is = 114
List Files in Directory
#include
#include
#include
int main()
{
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("%s\n", dir->d_name);
}
closedir(d);
}
return(0);
}
Output
.
..
b.txt
pgm2.c
pgm5.c
1
a.out
a.txt
b.txt
pgm.c
pgm1.c
pgm10.c
pgm11.c
pgm12.c
pgm13.c
pgm14.c
pgm15.c
pgm16.c
pgm17.c
pgm18.c
pgm19.c
pgm2.c
pgm20.c
pgm21.c
pgm22.c
pgm23.c
pgm24.c
pgm25.c
pgm26.c
pgm27.c
pgm28.c
pgm29.c
pgm3.c
pgm30.c
pgm31.c
pgm32.c
pgm33.c
pgm34.c
pgm35.c
pgm36.c
pgm37.c
pgm38.c
pgm39.c
pgm4.c
pgm40.c
pgm41.c
pgm42.c
pgm43.c
pgm44.c
pgm45.c
pgm46.c
pgm47.c
pgm48.c
pgm49.c
pgm5.c
pgm50.c
pgm51.c
pgm52.c
pgm53.c
pgm54.c
pgm55.c
pgm56.c
pgm57.c
pgm58.c
pgm59.c
pgm6.c
pgm7.c
pgm8.c
pgm9.c
No comments:
Post a Comment