I want to readfile and merge them all to the arraytotal array ,
It works fine for individual file, don't know why, it printed out some weird character for the final
Thanks

#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std; 
#include <iostream>
void fnSortHeap(char array[], int arr_ubound);

int main() {
  FILE *file;
  int i;
  int j=0;
  char fileparam[200] ;
  // char fileparam1[16] ;
  char arraytotal [1000];
  int start =0 ;
  int k = 0 ;
  

            if(sprintf(fileparam, "in0.dat",i) )
                {
                     file = fopen(fileparam,"r");
                    if(file==NULL)
                    {
        				 printf("Error: %s does not exist\n",fileparam);
                         
                    }
                    else         
                    {
            	       printf("%s was opened successfully.\n", fileparam);	      
            	      
            	       printf("Good\n");
                        while(!feof(file))
            	       {
                           arraytotal[k] = (char)fgetc(file);
                           printf("%c",(char)arraytotal[k]);
                           start++;
                       } // end for
        
                    }
                
                } 
               printf("%d\n" ,start);
        if(sprintf(fileparam, "in1.dat",i) )
                {
                     file = fopen(fileparam,"r");
                    if(file==NULL)
                    {
        				 printf("Error: %s does not exist\n",fileparam);
                         
                    }
                    else         
                    {
            	       printf("%s was opened successfully.\n", fileparam);	      
            	      
            	       printf("Good\n");
            	          while(!feof(file))
            	       {
                           arraytotal[k] = (char)fgetc(file);
                           printf("%c",arraytotal[k]);
                           start++;
                       } // end for
            	 
        
                    }
                
                } // end if 
          // printf("%d\n%d", sizeof fileparam,start);
            
          
        if(sprintf(fileparam, "in.dat",i) )
                {
                     file = fopen(fileparam,"r");
                    if(file==NULL)
                    {
        				 printf("Error: %s does not exist\n",fileparam);
                         
                    }
                    else         
                    {
            	       printf("%s was opened successfully.\n", fileparam);	      
            	      
            	       printf("Good\n");
            	           while(!feof(file))
            	       {
                           arraytotal[k] = (char)fgetc(file);
                           printf("%c",arraytotal[k]);
                           start++;
                       } // end for
            	 
        
                    }
                
                } // end if 
     
      printf("Final Result is \n");
                   for ( k = 0; k <sizeof arraytotal; k++)
        	       {
                      
                       printf("%c",(char)arraytotal[k]);
                      
                   } // good code for open file
        for(k=sizeof arraytotal;k>1;k--)
        fnSortHeap(arraytotal,k-1);
        // for ( k = 0; k <sizeof arraytotal; k++)
//        	       {
//                      
//                       printf("%c",arraytotal[k]);
//                      
//                   } // good code for open file
  
// the rest of process related to the file are trim
	
 fclose(file);
 
   system("pause");
return 0;
  
}

void fnSortHeap(char array[], int arr_ubound){
  int i, o;
  int lChild, rChild, mChild, root, temp;
  root = (arr_ubound-1)/2;

  for(o = root; o >= 0; o--){
  for(i=root;i>=0;i--){
  lChild = (2*i)+1;
  rChild = (2*i)+2;
  if((lChild <= arr_ubound) && (rChild <= arr_ubound)){
  if(array[rChild] >= array[lChild])
  mChild = rChild;
  else
  mChild = lChild;
  }
  else{
  if(rChild > arr_ubound)
  mChild = lChild;
  else
  mChild = rChild;
  }

  if(array[i] < array[mChild]){
  temp = array[i];
  array[i] = array[mChild];
  array[mChild] = temp;
  }
  }
  }
  temp = array[0];
  array[0] = array[arr_ubound];
  array[arr_ubound] = temp;
  return;
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.