C 读取文件,但仅插入文件中的最后一个字符串

C 读取文件,但仅插入文件中的最后一个字符串,c,debugging,segmentation-fault,C,Debugging,Segmentation Fault,我正在读取3个文件并将它们合并到一个名为mergedfile的数组中,但当我尝试打印数组时,它只打印出第一个数组中的最后一个单词。不确定我是否读取了错误的文件或将字符串放入了错误的数组如果有人知道可能是什么问题,我将不胜感激。谢谢 美国文件包含我需要按字母顺序排序并插入word.txt的字符串 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { //op

我正在读取3个文件并将它们合并到一个名为mergedfile的数组中,但当我尝试打印数组时,它只打印出第一个数组中的最后一个单词。不确定我是否读取了错误的文件或将字符串放入了错误的数组如果有人知道可能是什么问题,我将不胜感激。谢谢 美国文件包含我需要按字母顺序排序并插入word.txt的字符串

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main()
{

    //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 temp[50];
    char *(*mergedFile);
    //creating variables for while and if loops
    int i =0, j=0;
    int count=0;
    char *p;
    int q=0;
    int z = 0;



    //checking to make sure files are being read

    if(fp1 == NULL || fp2 == NULL || fp3 == NULL)
    {
        printf("Could not open one or all of the files.\n");
        printf("Exiting program!");
        exit(0);
    }




    //reading the data from files

    while (fgets(temp, 50 ,fp1) != NULL)
    {
        count++;
    }
    fclose(fp1);
    while (fgets(temp, 50 ,fp2) != NULL)
    {

        count++;
    }
    fclose(fp2);
    while (fgets(temp, 50 ,fp3) != NULL)
    {

        count++;
    }
    fclose(fp3);




    //inserting data into the array
    mergedFile = (char **)malloc(sizeof(char*) *count);
    for(int i=0; i<count; i++){
        mergedFile[i]=(char*)malloc(sizeof(char)*50);

    }
    fp1 = fopen("american0.txt","r");
    fp2 = fopen("american1.txt","r");
    fp3 = fopen("american2.txt","r");

    if(fp1 == NULL || fp2 == NULL || fp3 == NULL )
    {
        printf("Could not open one or all of the files.\n");
        printf("Exiting program!");
        exit(0);
    }
    i=0;

    while (fgets(temp, 50, fp1) != NULL)
    {
         mergedFile[i++]= temp;     
    }

    while (fgets(temp, 50, fp2) != NULL)
    {
         mergedFile[i++]= temp;     
    }

    while (fgets(temp, 50, fp3) != NULL)
    {
         mergedFile[i++]= temp;     
    }
    for(z = 0; z <count; z++)
    printf("%s", mergedFile[z]);




    /*
    //sorting the array alphabetically
    for(i=1; i<count; i++)
    {
        for(j=1; j<count;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;

}
#包括
#包括
#包括
int main()
{
//打开三个文件进行合并
文件*fp1=fopen(“american0.txt”,“r”);
FILE*fp2=fopen(“american1.txt”,“r”);
文件*fp3=fopen(“american2.txt”,“r”);
//打开文件以存储结果
文件*fpm=fopen(“words.txt”,“w”);
//创建数组以保存文件数据
煤焦温度[50];
字符*(*合并文件);
//为while和if循环创建变量
int i=0,j=0;
整数计数=0;
char*p;
int q=0;
int z=0;
//检查以确保正在读取文件
如果(fp1==NULL | | fp2==NULL | | fp3==NULL)
{
printf(“无法打开一个或所有文件。\n”);
printf(“退出程序!”);
出口(0);
}
//从文件中读取数据
while(fgets(温度,50,fp1)!=NULL)
{
计数++;
}
fclose(fp1);
而(fgets(温度,50,fp2)!=NULL)
{
计数++;
}
fclose(fp2);
while(fgets(温度,50,fp3)!=NULL)
{
计数++;
}
fclose(fp3);
//将数据插入数组
mergefile=(char**)malloc(sizeof(char*)*count);

对于(int i=0;i每次执行
fgets
,它都会覆盖
temp

此外,
mergedFile
中的所有条目都被赋予指向
temp
的[相同]指针值

因此,所有条目将以第三个文件最后一行的值结束

您需要为每行保存一份单独的副本。因此,请全部更改:

mergedFile[i++]= temp;
进入:

mergedFile[i++]= strdup(temp);