Answer to Question #306476 in UNIX/Linux Programming for panda

Question #306476
  1. Write a shell script or C program to read all txt files (.txt) in the current directory and merge them all to one txt file and returns a file descriptor for the new file.
1
Expert's answer
2022-03-08T07:11:14-0500

#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;

}


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