EmbLogic's Blog

a program to read all text files in current directory and merge them all into one text file (assignment13 q6) using

#include<stdio.h>
#include<fcntl.h>
#include<dirent.h>
#include<string.h>
int openf(char *);
int main()
{
DIR *i;
struct dirent *k;
int j,l,s,flag=1,fd,fd1;
char *m = “txt.”;
char ch;
fd1 = openf(“new.txt”);
i = opendir(“./test”);
printf(“\n%d\n”,i);

while(( k = readdir(i)) !=  NULL)
{
flag=1;
l=strlen(k->d_name);
for(j = l-1,s=0;j >= l-4,s < 4 ;j–,s++ )
{
if((k->d_name[j])!= m[s])
{
flag=0;
goto q;
}

}
q:         if(flag==1)
{
fd = openf(k->d_name);
while(read(fd, &ch, 1))
write(fd1,&ch,1);
close(fd);
}
}
printf(“\n”);
close(fd1);
}
int openf(char * s)
{
int fd;
fd = open(s,O_RDWR|O_CREAT,0666);
if(fd < 1)
perror(“open error”);
else
printf(“input file open succesfull fd = %d\n”,fd);
return fd;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>