I have read the strings from two files and stored them into two separate arrays, I then tried sorting them and writing them into another file...This part works the way it is supposed to but what I really need to do is combine the strings from the two arrays into one so that i can sort them in alphabetical order and then write to a file but the problem is that when i try to store these two arrays into one array I get segmentation fault as the error message..I am really not sure how to store these two arrays into another one so that I can sort it in order...I know how to sort it I am just not sure how to read these into an another array... i cant use #DEFINE because I am going to write all of this into a function for a tester code...
I have tried something like
new[i] = str;
and also:
strcpy(new[i],str)
but neither of these work...any help would be greatly appreciated...
Here is my code:
#include<stdio.h>
#include<string.h>
main (void)
{
char str[200];
char str2[300];
char new[300];
char temp [300];
int linenumber=0;
FILE *fa = fopen ("book1.dat", "r");
FILE *fb = fopen ("book2.dat", "r");
FILE *fc = fopen ("fixed.txt", "w");
int i=0;
int j=0;
int k;
/*read the strings into an array while it is not the end of file*/
while(!feof(fa)&& !feof(fb))
{
linenumber++;
fscanf(fa,"%[^
]
",str);
fscanf(fb,"%[^
]
",str2);
/*compare strings in array and write to array*/
if(strcmp(str2, str)<0)
{
fprintf(fc, "%s
", str2);
fprintf(fc, "%s
", str);
}
if (strcmp(str2,str)>0)
{
fprintf(fc, "%s
", str);
fprintf(fc, "%s
", str2)
}
/*print out the results of str */
printf("%s", str);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…