Take the 2-minute tour ×

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");
if(out == NULL)
    fprintf(stderr, "Cannot open output file - exiting!\n");

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(in2, "%d", &ch2);
    if (ch2 <=  ch1) fputs(ch2, out);
    else fputs(ch1, out);


return 0;


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



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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
I was going back and forth between fgets and fscanf - I'll try implementing fscanf in the method. –  Dice719 Apr 23 '12 at 22:51

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;

    return array;
share|improve this answer

Your Answer


