-1

I am supoosed write a function that reads two text files line by line, compare them, delete the duplicates and but them into a third file in alphabetical order...I have been working on this for over a month and I am still stuck I have tried several ways to do this and come up with nothing...I was in formed that i have to use strcmp to do this and I cant use any other predefined sorting function...I have also looked around on this site and cannot find much that helps with this...any help would be greatly appreciated..Here is what I have so far:

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


main (void)
{
    char str [200];
    char str2 [200];
    char new [100];
    char temp [100];
    int row = 10;
    FILE *fa = fopen ("book1.dat", "r");
    FILE *fb = fopen ("book2.dat", "r");
    FILE *fc = fopen ("fixed.txt", "w");

    int i;
    int j;
    int k;

    while (fgets (str, 200, fa) !=NULL && fgets (str2, 200, fb) !=NULL)
    {
        puts(str);
        puts(str2);

        if (strcmp( str, str2) ==0 )
        {
            strcpy (str , new);
        } else {
            strcpy (new, str);
            strcpy (new, str2);
        }
    }
    for ( i = 0; i < row; i++)
    {
        for (j = i+1; j< row; j++)
        {
            if(strcmp(new[i], new [j]) > 0)
            {
                strcpy (temp, new);
                strcpy(new, new);
                strcpy(new, temp);
            }
        }
    }
    for (i = 0; i < length; i ++)
    {
        fputs(new, fc);
    }
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
user1385602
  • 1
  • 1
  • 1
  • 5
  • Try indenting your code. It helps visualizing the program flow. – pmg May 09 '12 at 20:57
  • If he had indented, it would have taken him another month. – wildplasser May 09 '12 at 20:58
  • 1
    Wouldn't it be easier to [suggest an] edit [to the] code, instead of complaining about how it looks? Anyway...which portion of your code isn't working? Decompose each block that you have here and determine what it's supposed to do, then trace out why it's *not* doing what it's supposed to. – Makoto May 09 '12 at 21:01
  • the files copy to the arrays but it does not sort it alpabetically, it only adds junk to the new file...i am not sure what I am doing wrong – user1385602 May 09 '12 at 21:02
  • I broken down the code and opening the two files and copying them works but when i get to the part where i have to compare them and write them to a new file...this does not work.. – user1385602 May 09 '12 at 21:03
  • DO you have a debugger available? – Paul Sasik May 09 '12 at 21:06
  • I dont have a debugger...I know that is a problem... – user1385602 May 09 '12 at 21:08
  • `char new[100]` is an array of 100 characters, not 100 lines of characters. Start by thinking about your data structures - what data are you going to need at each stage of your programs operation, and how will you need to manipulate it. – Roddy May 09 '12 at 21:49

3 Answers3

1

Your use of strcpy() is peculiar. Recall its signature:

char *strcpy(char *dest, const char *src)

Here's a usage that doesn't make immediate sense to me:

strcpy (new, str); // new now has str
strcpy (new, str2); // new now has str2

You've effectively overwritten something there. I would start from there, and see what else may not be working as you intend. Furthermore, if you can use gcc, look into using gdb as well to debug your code. (You would need to compile with the -g flag.)

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Thank you very much for your answer...I took a look at that and it is overwriting the string but the problem is that if I try strcpy(new[i], str) or even strcpy(new[i], str[i]) it will not work at all..I get a lot of pointer warnings... – user1385602 May 09 '12 at 21:18
  • @user1385602: As I said, it's a start. There's a whole mess of pointer issues with the code, but as I said in an earlier comment, determine what each block of code is supposed to do, and determine why it *doesn't* do what you want it to. – Makoto May 09 '12 at 21:20
1

First off, can you assume the duplicates from book1 and book2 line up nicely?

Think about how you would detect if the first entry in book1 is identical to the last entry in book2.

Secondly, you have to sort your output alphabetically. Sorting algorithms is kind of one of those common things that students are forced to do all the time. It builds character. For bonus kudos, implement quick sort.

Philip
  • 1,539
  • 14
  • 23
0

sample a way.
error handling is omitted.
since we are using the sort function of the library sqort, implement your own.

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

#define LINE_MAX_SIZE 256

typedef struct filePos {
    FILE *fp;
    long pos;
} FilePos;

typedef struct myfile {
    int lines;
    int capacity;
    FILE *fp;
    FilePos *filePoss;
} MyFile;

MyFile *myfopen(const char *filepath){
    char buff[LINE_MAX_SIZE];
    MyFile *mfp;
    mfp = (MyFile*)malloc(sizeof(MyFile));
    mfp->lines = 0;
    mfp->capacity=16;
    mfp->filePoss=NULL;
    mfp->filePoss=(FilePos*)realloc(mfp->filePoss, sizeof(FilePos)*(mfp->capacity *= 2));
    mfp->fp = fopen(filepath, "r");
    do{
        mfp->filePoss[mfp->lines].fp = mfp->fp;
        mfp->filePoss[mfp->lines].pos = ftell(mfp->fp);
        if(++mfp->lines == mfp->capacity){
            mfp->filePoss=(FilePos*)realloc(mfp->filePoss, sizeof(FilePos)*(mfp->capacity *= 2));
        }
    }while(NULL!=fgets(buff, LINE_MAX_SIZE, mfp->fp));
    --mfp->lines;
    return mfp;
}

void myfclose(MyFile *mfp){
    free(mfp->filePoss);
    fclose(mfp->fp);
    free(mfp);
}

char *myfgets(FilePos *p, char *buff){
    fseek(p->fp, p->pos, SEEK_SET);
    return fgets(buff, LINE_MAX_SIZE, p->fp);
}

int myfcomp(const void *a, const void *b){
    char buff_a[LINE_MAX_SIZE];
    char buff_b[LINE_MAX_SIZE];
    FilePos *fpa,*fpb;
    fpa=(FilePos*)a;
    fpb=(FilePos*)b;
    myfgets(fpa, buff_a);
    myfgets(fpb, buff_b);
    return strcmp(buff_a, buff_b);
}

void myfsort(MyFile *mfp){
    qsort(mfp->filePoss, mfp->lines, sizeof(FilePos), myfcomp);
}

void myfprint(MyFile *mfp){
    char buff[LINE_MAX_SIZE];
    int i;

    for(i=0;i<mfp->lines ;++i)
        printf("%s", myfgets(mfp->filePoss + i, buff));
}

void merge(const char *inpfile1, const char *inpfile2, const char *outfile){
    FILE *fo;
    MyFile *fi1, *fi2;
    char buff_f1[LINE_MAX_SIZE];
    char buff_f2[LINE_MAX_SIZE];
    char buff_fo[LINE_MAX_SIZE];
    char *outbuff=NULL;
    int fi1_line, fi2_line;
    int eof1, eof2;

    fo=fopen(outfile, "w");
    fi1=myfopen(inpfile1);
    fi2=myfopen(inpfile2);
    myfsort(fi1);
    myfsort(fi2);
    fi1_line=fi2_line=0;
    eof1=eof2=0;
    *buff_fo='\0';
    while(1){
        if(!eof1 && outbuff != buff_f2){
            myfgets(&(fi1->filePoss[fi1_line]), buff_f1);
        }
        if(!eof2 && outbuff != buff_f1){
            myfgets(&(fi2->filePoss[fi2_line]), buff_f2);
        }
        if(!eof1 && !eof2){
           if(strcmp(buff_f1, buff_f2) <= 0){
                outbuff=buff_f1;
                ++fi1_line;
            } else {
                outbuff=buff_f2;
                ++fi2_line;
            }
        } else if(!eof1 && eof2){
            outbuff=buff_f1;
            ++fi1_line;
        } else if(eof1 && !eof2){
            outbuff=buff_f2;
            ++fi2_line;
        } else {
            break;
        }
        if(strcmp(outbuff, buff_fo) != 0){//duplicate check
            strcpy(buff_fo, outbuff);
            fputs(buff_fo, fo);
        }
        if(fi1->lines == fi1_line)
            eof1 = !0;
        if(fi2->lines == fi2_line)
            eof2 = !0;
    }
    myfclose(fi2);
    myfclose(fi1);
    fclose(fo);
}

int main(){
    merge("book1.txt", "book2.txt", "fixed.txt");
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70