我已经从两个文件中读取字符串,并将它们存储到两个单独的数组中,然后尝试对它们进行排序并将它们写入另一个文件中......这部分以它应该的方式工作但我真正需要做的是将两个数组中的字符串合并为一个,以便我可以按字母顺序对它们进行排序,然后写入一个文件,但问题是,当我尝试将这两个数组存储到一个数组中时,分段错误作为错误消息..我真的不知道如何将这两个数组存储到另一个,以便我可以按顺序排序...我知道如何排序它我不知道如何将它们读入另一个数组......我不能使用#DEFINE,因为我要将所有这些写入测试代码的函数中......将字符串从文件存储到数组
我有尝试类似
new [i] = str;
也:
的strcpy(新[I],STR)
但无论这些工作...任何帮助将不胜感激......
这是我的代码:
#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,"%[^\n]\n",str);
fscanf(fb,"%[^\n]\n",str2);
/*compare strings in array and write to array*/
if(strcmp(str2, str)<0)
{
fprintf(fc, "%s\n", str2);
fprintf(fc, "%s\n", str);
}
if (strcmp(str2,str)>0)
{
fprintf(fc, "%s\n", str);
fprintf(fc, "%s\n", str2)
}
/*print out the results of str */
printf("%s", str);
}
}
你是什么意思*如何将这些读入另一个数组* *?你想合并2个字符串吗?你想要一串字符串吗? – cnicutar
是的,我正在尝试将数组str和str2中的字符串放到数组中,以便我可以按字母顺序对新数组中的所有字符串进行排序,但是我不知道如何将它们合并为一个字符串数组... – user1385602