Question

I am creating a program that takes two .txt (data1.txt, data2.txt) files that contain integers that are stored in ascending order and merging them in a output.txt (data3.txt) in ascending order. I am using the fgets function to read each int from the .txt file and comparing the fgets from each of the two input.txt files using a if function (which isn't working at all). So my question is what is the proper way to compare the two fgets function to sort the integers in ascending order?

data1.txt contains: 5 15 25 35 45

data2.txt contains: 10 20 30 40 50

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#define LINE_LENGTH 80


int main()
{
FILE *in1, *in2, *out;
char buffer1[LINE_LENGTH+1], buffer2[LINE_LENGTH+1];
int ch1, ch2;


in1 = fopen("data1.txt", "r");
in2 = fopen("data2.txt", "r");
out = fopen("data3.txt", "w");

if(in1 == NULL || in2 == NULL)
{
    fprintf(stderr, "Cannot open input file - exiting!\n");
    exit(1);
}
if(out == NULL)
{
    fprintf(stderr, "Cannot open output file - exiting!\n");
    exit(1);
}

while( ! feof(in1) ) #Checking for end of file
{
    fscanf(in1, "%d", &ch1);
    fscanf(in1, "%d", &ch2);
    if (ch1 <=  ch2) fputs(ch1, out);
    else fputs(ch2, out);
}


while( ! feof(in2) ) #Checking for end of file
{
    fscanf(in1, "%d", &ch1);
    fscanf(in2, "%d", &ch2);
    if (ch2 <=  ch1) fputs(ch2, out);
    else fputs(ch1, out);
}

fclose(in1);
fclose(in2);
fclose(out);

return 0;

}

Hope I covered it all, let me know if you need more information

Thanks!

--------------EDIT------------

I am trying to implementing the while loop using fscanf, however gcc throws the error: "passing argument 1 of ‘fputs’ makes pointer from integer without a cast" for every line containing fputs function. Should I not be using fputs with fscanf or should ch1/ch2 be a different format?

Was it helpful?

Solution

I don't think you really know what fgets does. It reads a line (if buffer is big enough) and stores it into the buffer. It then returns a pointer to the same buffer, which is meaningless to compare. You need to read each integer and compare them, you might want to use fscanf(in1, "%d", &n); to read an integer into n, this will take care of ignoring any whitespace and will work great if you know your file contains just numbers. Also you're reading beyond the end of file in in1 and possibly also in2.

Think about it some more, because it seems you don't really understand what's going on.

OTHER TIPS

fgets is used to get a string from stream. You'd better user fscanf for reading integers. My advice is first read these files into two separate arrays, then sort them. If files get bigger, keeping a file open might end up with an error. Here is a sample code to read a file into an integer array.

int* readFile(int *array, char *file) {
    FILE *fp = fopen(file, "r");
    int x;
    int i = 0;

    while (fscanf(fp, "%d", &x) == 1) {
        *(array+i) = x;
        i++;
    }
    fclose(fp);

    return array;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top