Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include<fstream>
- #include<string>
- #include<sys/types.h>
- #include<sys/wait.h>
- using namespace std;
- void create_dir()//Function to create two dir with some files
- {
- system("mkdir Folder1");//create dir1
- //insert some files in dir1
- system("ls > Folder1/File1.txt");
- system("> Folder1/Music.mp3");
- system("> Folder1/Pic1.JPG");
- system("mkdir Folder2");//create dir2
- //insert some files in dir2
- system("ls -ld > Folder2/File1.txt");
- system("ls -a > Folder2/File2.txt");
- system("> Folder2/PicX1.gif");
- system("> Folder2/Music.mp3");
- system("> Folder2/Movie1.mp4");
- }
- void create_tem()//Function to create temp files
- {
- system("ls -l Folder1 > temp1.txt");//make temp1 txt file for Folder1
- system("ls -l Folder2 > temp2.txt");//make temp2 txt file for Folder2
- }
- void print()//Fuction to print file names in FOLDER1 & FOLDER2
- {
- ifstream infile1; infile1.open("temp1.txt");
- ifstream infile2; infile2.open("temp2.txt");
- int size=0;
- string temp;
- //To get number of files
- while(getline(infile1,temp)){size++;}
- while(getline(infile2,temp)){size++;}
- infile1.close();
- infile2.close();
- infile1.open("temp1.txt");
- infile2.open("temp2.txt");
- //Create string arraies to remove same names of files
- string fileName1[size];
- string fileName2[size];
- int i=0; int j=0;
- bool found;
- while(getline(infile1,temp)){fileName1[i]=temp; i++;}
- while(getline(infile2,temp)){
- found=false;
- for(int n=1; n<i; n++){//To check all names in fileName1 without first line
- if (temp==fileName1[n]){found=true; break;}
- }
- //if (found == false) store the temp string in fileName2
- if(found==false){fileName2[j]=temp; j++;}
- }
- cout<<"\n";
- cout<<"List of files in Folder1 & Folder2:\n\n";
- for (int n=1; n<i; n++){cout<<fileName1[n]<<endl;}//print names of folder1
- for (int n=1; n<j; n++){cout<<fileName2[n]<<endl;}//print names of folder2
- infile1.close();
- infile2.close();
- cout<<"\n";
- }
- void del()//Function to delete termporary files
- {
- system("rm temp1.txt");
- system("rm temp2.txt");
- }
- int main(){
- create_dir();//call function of create 2 Folders
- int a=fork();//create child
- if(a==0){//--------------For child process
- create_tem();//call function of create 2 temp file
- exit(0);
- }else if(a>0){//---------For parent porcess
- }else{cerr<<"NO Fork1"<<endl;}
- wait(NULL);
- int b=fork();//create child2
- if(b==0){//-------------For child2 process
- print();//call function of print file names
- exit(0);
- }else if(b>0){//--------For parent2 process
- }else{cerr<<"NO Fork2";};
- wait(NULL);
- del();//call function of delete every things
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement