Hi guys I was having issues with reading 3 files and merging them into an array, I tried fgetc, which then let me to have issues with sorting them so I tried to change my array and allocate a counter variable to get the size of it but not sure which method would be best to read and insert into an arrat I tried a few ways which I commented below, however I get errors for both. I'm a bit lost when it comes to arrays and pointers. any help is appriciated. :)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
printf("test 1\n");
//open three files for merging
FILE *fp1 = fopen("american0.txt","r");
FILE *fp2 = fopen("american1.txt","r");
FILE *fp3 = fopen("american2.txt","r");
//open file to store the result
FILE *fpm = fopen("words.txt", "w");
//creating an array to save the files data
char *c;
int i, j;
int count;
char mergedFile[count][50];
//creating a second array as a temp value for sorting purposes
char temp[50];
printf("test 2\n");
//checking to make sure files are being read
if(fp1 == NULL || fp2 == NULL || fp3 == NULL || fpm == NULL)
{
printf("Could not open one or all of the files.\n");
printf("Exiting program!");
exit(0);
}
printf("test 3\n");
//inserting data from file into an array
while ((fgets(c, 50 ,fp1))!= EOF) //tried this failed
{
mergedFile[i++]=c;
count++;
}
while ((c = fscanf(fp2)) != EOF) //also with fscanf failed as well
{
mergedFile[i++]=c;
count++;
}
while ((c = fscanf(fp3)) != EOF)
{
mergedFile[i++]=c;
count++;
}
printf("test 4\n");
//getting size of mergedfiles array
int s = sizeof(mergedFile)/sizeof(mergedFile[0]);
//sorting the array alphabetically
for(i=1; i<s; i++)
{
for(j=1; j<s;j++)
{
if(strcmp(mergedFile[j-1], mergedFile[j]) > 0)
{
strcpy(temp, mergedFile[j-1]);
strcpy(mergedFile[j-1], mergedFile[j]);
strcpy(mergedFile[j], temp);
}
}
}
//next goal is to print the array to file word.txt
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fpm);
return 0;
}