Recent Posts
None | 1 min ago
C | 1 min ago
None | 1 min ago
None | 2 min ago
None | 2 min ago
C# | 3 min ago
None | 3 min ago
mIRC | 4 min ago
mIRC | 4 min ago
C++ | 6 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By JJ on the 9th of Feb 2010 09:33:36 PM Download | Raw | Embed | Report
  1. #include <fstream>
  2. #include <iostream>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8.  
  9. int main (int argc, char *argv[])
  10. {
  11.   pid_t p1;
  12.   pid_t p2;
  13.   pid_t p3;
  14.  
  15.   int pid[3];
  16.  
  17.   //char fArray1;
  18.   //char fArray2;
  19.   //char fArray3;
  20.  
  21.         if ( argc != 4) // argc should be 2 for correct execution
  22.         {
  23.                 printf("please enter 4 arguments\n");
  24.                 printf("Ex: a.out file1 file2 file3\n");
  25.                 exit(0);
  26.         }
  27.  
  28.         p1=fork();
  29.     if(p1==0)
  30.     {
  31.                
  32.                 char chArray[51] = {'a','A','b','B','c','C', 'd', 'D', 'd', 'e', 'E', 'f', 'F','g', 'G', 'h', 'H', 'i','I','j','J', 'k', 'K', 'l', 'L', 'm', 'M', 'o', 'O',
  33.                                                         'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z'} ;
  34.                 int countArray[51];
  35.                 FILE *file1=fopen(argv[1], "r");
  36.                 char fArray1;
  37.                
  38.                 for(int i=0;i<51;i++)
  39.                         countArray[i]=0;
  40.                        
  41.                 if(file1 == NULL)
  42.                 {
  43.                         printf("Cannot open file.\n");
  44.                         exit(1);
  45.                 }
  46.                 else
  47.                 {
  48.                         printf("File1 opened successfully.\n");
  49.                         do
  50.                         {
  51.                                 fArray1 = fgetc(file1);
  52.                                        
  53.                                 for(int i=0; i< sizeof(chArray); i++)
  54.                                 {
  55.                                         if(chArray[i] == fArray1)
  56.                                         {
  57.                                                 countArray[i]+=1;
  58.                                         }
  59.                                                                
  60.                                 }
  61.                                
  62.                         }while(fArray1 != EOF);
  63.                         {
  64.                                         fclose (file1);
  65.                         }
  66.                
  67.                         //printf("Number of characters in %s is %d", argv[1]);
  68.                         for(int j=0; j< sizeof(chArray); j++)
  69.                         {
  70.                                 printf("File1 contains %c", chArray[j]);
  71.                                 printf(" %d\n", countArray[j]);
  72.                                 printf("\n");
  73.                         }
  74.                         fclose(file1);
  75.                 }      
  76.                        
  77.                 exit(0);
  78.         }
  79.         else
  80.         {
  81.                         pid[0]=p1;
  82.                         p2=fork();
  83.                        
  84.                         if(p2 == 0)
  85.                         {
  86.                                 char chArray2[51] = {'a','A','b','B','c','C', 'd', 'D', 'd', 'e', 'E', 'f', 'F','g', 'G', 'h', 'H', 'i','I','j','J', 'k', 'K', 'l', 'L', 'm', 'M', 'o', 'O',
  87.                                                         'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z'} ;
  88.                                 int countArray2[51];
  89.  
  90.                                 FILE *file2=fopen(argv[2], "r");
  91.                                
  92.                                 char fArray2;
  93.          
  94.                                 for(int i=0;i<51;i++)
  95.                                         countArray2[i]=0;
  96.                                
  97.                                 if(file2 == NULL)
  98.                                 {
  99.                                         printf("Cannot open file2.\n");
  100.                                         exit(1);
  101.                                 }
  102.                                 else
  103.                                 {
  104.                                         printf("File2 opened successfully.\n");
  105.                                        
  106.                        
  107.                                                 do
  108.                                                 {
  109.                                                         fArray2 = fgetc(file2);
  110.                                                        
  111.                                                         for(int i=0; i< sizeof(chArray2); i++)
  112.                                                         {
  113.                                                          
  114.                                                                 if(chArray2[i] == fArray2)
  115.                                                                 {
  116.                                                                         countArray2[i]+=1;
  117.                                                                 }
  118.                                                                                
  119.                                                         }
  120.                                                                        
  121.                                                 }while(fArray2 != EOF);
  122.                                                 {
  123.                                                         fclose (file2);
  124.                                                 }
  125.                                         }
  126.                                         for(int j=0; j< sizeof(chArray2); j++)
  127.                                         {
  128.                                                 printf("File2 contains %c", chArray2[j]);
  129.                                                 printf(" %d\n", countArray2[j]);
  130.                                                 printf("\n");
  131.                                         }
  132.                                        
  133.                                         fclose(file2);
  134.                                        
  135.                                 }
  136.                                 exit(0);
  137.                         }
  138.                        
  139.                         else
  140.                         {
  141.                                 pid[1]=p2;
  142.                
  143.                                 p3=fork();
  144.                                 if(p3 == 0)
  145.                                 {              
  146.                                         char chArray3[51] = {'a','A','b','B','c','C', 'd', 'D', 'd', 'e', 'E', 'f', 'F','g', 'G', 'h', 'H', 'i','I','j','J', 'k', 'K', 'l', 'L', 'm', 'M', 'o', 'O',
  147.                                                         'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z'} ;
  148.                                         int countArray3[51];
  149.                                         FILE *file3=fopen(argv[3], "r");
  150.                                         char fArray3;
  151.          
  152.                                         for(int i=0;i<51;i++)
  153.                                                 countArray3[i]=0;
  154.                                                
  155.                                         if(file3 == NULL)
  156.                                         {
  157.                                                 printf("Cannot open file3.\n");
  158.                                                 exit(1);
  159.                                         }
  160.                                         else
  161.                                         {
  162.                                                 printf("File3 opened successfully.\n");
  163.                                                
  164.                                                 do
  165.                                                 {
  166.                                                         fArray3 = fgetc(file3);
  167.                                                        
  168.                                                         for(int i=0; i< sizeof(chArray3); i++)
  169.                                                         {
  170.                                                          
  171.                                                                 if(chArray3[i] == fArray3)
  172.                                                                 {
  173.                                                                         countArray3[i]+=1;
  174.                                                                 }
  175.                                                                                
  176.                                                         }
  177.                                                                        
  178.                                                 }while(fArray3 != EOF);
  179.                                                 {
  180.                                                         fclose (file3);
  181.                                                 }
  182.                                                 for(int j=0; j<sizeof(chArray3); j++)
  183.                                                 {
  184.                                                         printf("File3 contains %c", chArray3[j]);
  185.                                                         printf(" %d\n", countArray3[j]);
  186.                                                         printf("\n");
  187.                                                 }
  188.                                                 fclose(file3);
  189.                                                        
  190.                                         }
  191.                                         exit(0);
  192.                                 }      
  193.                                 else
  194.                                 {
  195.                                         pid[2]=p3;
  196.                                        
  197.  
  198.                                 }
  199.                         }      
  200.         }
  201.                
  202.        
  203.         waitpid(pid[0], NULL, 0);
  204.         waitpid(pid[1], NULL, 0);
  205.         waitpid(pid[2], NULL, 0);
  206.  
  207.         return(0);  
  208. }
Submit a correction or amendment below. [ previous version ] | [ difference ] | Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: