Code:
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <time.h>
# define LIMIT 1000
void create_file(FILE *fp1, int N)
{
int i, random_number;
if ( fp1 == NULL )
{
printf ( "The file can not be created." ) ;
getch() ;
exit (0) ;
}
srand ( time(NULL) );
fprintf(fp1, "%d ", N);
printf("%d ", N);
for ( i = 1; i < N; i++ )
{
random_number = rand()%LIMIT;
fprintf(fp1, "%d ",random_number);
printf( "%d ", random_number) ;
}
}
void take_from_file(FILE *fp1, int *Array, int N )//This function takes everything from fp1 file and puts them in Array[]
{
int i;
if ( fp1 == NULL )
{
printf ( "The file can not be created." ) ;
getch() ;
exit (0) ;
}
for(i=0; i<N; i++)
{
fscanf(fp1, "%d ", Array[i]);
printf("%d ", Array[i]);
}
}
void heap_create(int *Array, int N)
{
int i,j, parent,child,temp;
for(i = 2; i <= N; i++)
{
child = i;
parent = child/2;
while(child > 1 && Array[child] > Array[parent])
{
temp = Array[child];
Array[child] = Array[parent];
Array[parent] = temp;
child = parent;
parent = child/2;
if(parent < 1)
{
parent = 1;
}
}
}
printf("\n The Array in heap form is: ");
for(j = 1; j <= N; i++)
{
printf(" %d", Array[i]);
}
}
void sort_Array(FILE *fp2, int *Array, int N)
{
int current,parent,child,i,maxnodes;
for(maxnodes = N; maxnodes >= 2; maxnodes--)
{
current = Array[maxnodes];
Array[maxnodes] = Array[1];
parent = 1;
if ((maxnodes-1) >= 3 && Array[3] > Array[2])
{
child = 3;
}
else
{
child = 2;
}
while (child <= (maxnodes-1) && Array[child] >= current)
{
Array[parent] = Array[child];
parent = child;
child = child*2;
if((child+1) <= (maxnodes-1) && Array[child+1] > Array[child])
{
child = child + 1;
}
}
Array[parent] = current;
}
printf("\n The Array sorted is: ");
for(i = 1;i <= N; i++)
{
fprintf(fp2, "%d", Array[i]); //I am not sure if i have to put the sorted Array from this point or the way i have done it in the other function copy_file(). Please tell me wich way is better or if they are both wrong or wright.
printf("%d ",Array[i]);
}
getch();
}
void copy_file(FILE *fp1, FILE *fp2)//In this function i am trying to put the data of fp1 to fp2, but i want first to bring everything into an Array and then sort this Array. Here is the step i have stuck.
{
char ch, int counter;
if ( fp1 == NULL )
{
printf ( "The file can't open." ) ;
getch() ;
exit (0) ;
}
if ( fp2 == NULL )
{
printf ( "The file can't open." ) ;
getch() ;
exit (0) ;
}
heap_create();
take_from_file();//Here i call this function to take the elements from fp1 file and store it into the Array[]
sort_Array();//And here i am sorting the Array[]
while(!feof(fp1))
{
fputc(Array[i], fp2); //Here i am lost ... :P I don't know how to say to take the sorted Array and put it into fp2.
counter++;
}
}
int main()
{
char file_name1[50], file_name2[50];
int N;
FILE *fp1, *fp2;
int i;
printf("Please give the name of the file you want to create: ");
scanf("%s", file_name1);
printf("Please give the number of numbers you want in the file: ");
scanf("%d", &N);
int Array[N];
fp1 = fopen(file_name1, "w");
create_file(fp1, N);
printf("Please give the name of the source file: ");
scanf("%s", file_name1);
printf("Please give the name of the destination file: ");
scanf("%s", file_name2);
fp1 = fopen(file_name1, "r"); //I open the fp1 file for reading it
take_from_file(fp1, Array, N);
heap_create(Array, N);
sort_Array(fp2, Array, N);
copy_file(fp1, fp2);
system("pause");
return 0;
}
I except every small detail or help from you. Thank you although for your before help and advice.