Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
199 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

typedef char* Type;

typedef struct vector {
    size_t size;
    size_t capacity;
    Type *array;
} Vector;

Vector *vec_make(){
    Vector *v;
    v = (Vector*)malloc(sizeof(Vector));
    v->size = 0;
    v->capacity=16;
    v->array=(Type*)realloc(NULL, sizeof(Vector)*(v->capacity *= 2));
    return v;
}

void vec_add(Vector *v, Type value){
    v->array[v->size] = value;
    if(++v->size == v->capacity)
        v->array=(Type*)realloc(v->array, sizeof(Vector)*(v->capacity *= 2));
}

size_t vec_size(Vector *v){
    return v->size;
}

Type *vec_getArray(Vector *v){
    return v->array;
}

void vec_free(Vector *v){
    free(v->array);
    free(v);
}

int cmp(const void *a, const void *b){
    return strcmp(*(char**)a, *(char**)b);
}

void merge(const char *inpfile1, const char *inpfile2, const char *outfile){
    FILE *fp;
    char buff[256], **array;
    Vector *v;
    size_t c, size;

    v = vec_make();
    fp=fopen(inpfile1, "r");
    while(NULL!=fgets (buff, 256, fp))
        vec_add(v, strdup(buff));
    fclose(fp);

    fp=fopen(inpfile2, "r");
    while(NULL!=fgets (buff, 256, fp))
        vec_add(v, strdup(buff));
    fclose(fp);

    array=vec_getArray(v);
    size=vec_size(v);
//  qsort(array, size, sizeof(Type), cmp);
    fp=fopen(outfile, "w");
    for(c=0;c < size; ++c){
        fputs(array[c], fp);
        free(array[c]);
    }
    fclose(fp);
    vec_free(v);
}

int main(){
    merge("book1.txt", "book2.txt", "fixed.txt");
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...