Unable to figure out this issue that is causing my program to throw this error, i'm reading from 3 files and have to make a dynamic array to store all the values of the text file(which are INTs). Then sort and print out to a new file.
This is my updated code
#include <stdio.h>
#include <stdlib.h>
//Jeffrey Hennen
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
void main(){
//printf("hello\n");
int counter = 0;
FILE *fp;
fp = fopen("list0.txt", "r");
int ch;
while((ch = fgetc(fp)) != EOF){
//counter++;
if(ch == '\n'){
counter++;
}
}
//printf("%d\n", counter);
//printf("hello1\n");
fclose(fp);
fp = fopen("list1.txt", "r");
while((ch = fgetc(fp)) != EOF){
//counter++;
if(ch == '\n'){
counter++;
}
}
//printf("%d\n", counter);
//printf("hello1\n");
fclose(fp);
fp = fopen("list2.txt", "r");
while((ch = fgetc(fp)) != EOF){
//counter++;
if(ch == '\n'){
counter++;
}
}
//printf("%d\n", counter);
fclose(fp);
//printf("hello2\n");
int size = counter;
int * sizeOfFile = (int*) malloc(size * sizeof(int));
//int z = 0;
//for (z = 1; z<= size; z++){
// printf("%d %d\n", z, sizeOfFile[z]);
//}
char ch1[sizeof(int)];
int counter1 = 0;
int y = 0;
fp = fopen("list0.txt", "r");
while ((ch = fgetc(fp)) != EOF){
fgets(ch1, sizeof(int), fp);
y = atoi(ch1);
printf("%s\n", ch1);
sizeOfFile[counter1] = y;
counter1++;
}
fclose(fp);
fp = fopen("list1.txt", "r");
while ((ch = fgetc(fp)) != EOF){
fgets(ch1, sizeof(int), fp);
y = atoi(ch1);
printf("%s\n", ch1);
sizeOfFile[counter1] = y;
counter1++;
}
fclose(fp);
fp = fopen("list1.txt", "r");
while ((ch = fgetc(fp)) != EOF){
fgets(ch1, sizeof(int), fp);
y = atoi(ch1);
printf("%s\n", ch1);
sizeOfFile[counter1] = y;
counter1++;
}
fclose(fp);
qsort(sizeOfFile, size, sizeof(int), cmpfunc);
fp = fopen("Hw3.out", "w");
int x = 0;
for(x; x < size; x = x + 1){
fprintf(fp, "%d\n", sizeOfFile[x]);
}
fclose(fp);
free(sizeOfFile);
}