Advertisement
Guest User

Solution#2

a guest
Mar 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<fstream>
  4. #include<string>
  5. #include<sys/types.h>
  6. #include<sys/wait.h>
  7. using namespace std;
  8.  
  9. void create_dir()//Function to create two dir with some files
  10. {
  11. system("mkdir Folder1");//create dir1
  12. //insert some files in dir1
  13. system("ls > Folder1/File1.txt");
  14. system("> Folder1/Music.mp3");
  15. system("> Folder1/Pic1.JPG");
  16.  
  17. system("mkdir Folder2");//create dir2
  18. //insert some files in dir2
  19. system("ls -ld > Folder2/File1.txt");
  20. system("ls -a > Folder2/File2.txt");
  21. system("> Folder2/PicX1.gif");
  22. system("> Folder2/Music.mp3");
  23. system("> Folder2/Movie1.mp4");
  24. }
  25. void create_tem()//Function to create temp files
  26. {
  27. system("ls -l Folder1 > temp1.txt");//make temp1 txt file for Folder1
  28. system("ls -l Folder2 > temp2.txt");//make temp2 txt file for Folder2
  29. }
  30. void print()//Fuction to print file names in FOLDER1 & FOLDER2
  31. {
  32. ifstream infile1; infile1.open("temp1.txt");
  33. ifstream infile2; infile2.open("temp2.txt");
  34. int size=0;
  35. string temp;
  36. //To get number of files
  37. while(getline(infile1,temp)){size++;}
  38. while(getline(infile2,temp)){size++;}
  39. infile1.close();
  40. infile2.close();
  41. infile1.open("temp1.txt");
  42. infile2.open("temp2.txt");
  43. //Create string arraies to remove same names of files
  44. string fileName1[size];
  45. string fileName2[size];
  46. int i=0; int j=0;
  47. bool found;
  48. while(getline(infile1,temp)){fileName1[i]=temp; i++;}
  49. while(getline(infile2,temp)){
  50. found=false;
  51. for(int n=1; n<i; n++){//To check all names in fileName1 without first line
  52. if (temp==fileName1[n]){found=true; break;}
  53. }
  54. //if (found == false) store the temp string in fileName2
  55. if(found==false){fileName2[j]=temp; j++;}
  56. }
  57. cout<<"\n";
  58. cout<<"List of files in Folder1 & Folder2:\n\n";
  59. for (int n=1; n<i; n++){cout<<fileName1[n]<<endl;}//print names of folder1
  60. for (int n=1; n<j; n++){cout<<fileName2[n]<<endl;}//print names of folder2
  61. infile1.close();
  62. infile2.close();
  63. cout<<"\n";
  64. }
  65. void del()//Function to delete termporary files
  66. {
  67. system("rm temp1.txt");
  68. system("rm temp2.txt");
  69. }
  70. int main(){
  71. create_dir();//call function of create 2 Folders
  72. int a=fork();//create child
  73. if(a==0){//--------------For child process
  74. create_tem();//call function of create 2 temp file
  75. exit(0);
  76. }else if(a>0){//---------For parent porcess
  77. }else{cerr<<"NO Fork1"<<endl;}
  78. wait(NULL);
  79. int b=fork();//create child2
  80. if(b==0){//-------------For child2 process
  81. print();//call function of print file names
  82. exit(0);
  83. }else if(b>0){//--------For parent2 process
  84. }else{cerr<<"NO Fork2";};
  85. wait(NULL);
  86. del();//call function of delete every things
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement