Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7839071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:19:01+00:00 2026-06-02T15:19:01+00:00

I am creating a program that takes two .txt (data1.txt, data2.txt) files that contain

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:19:02+00:00Added an answer on June 2, 2026 at 3:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a program that takes some time creating pdf files i would like
When creating DLL files for a program that already exists, is it customary to
I am creating a program (In C++) that takes an ASCII file and reads
I am creating a program that filters the contents of a JTable using two
I am creating a program that takes a series of numbers and adds the
I have a program that takes multiple files as input. What I'm trying to
I'm creating a program that authenticates from a before it runs. I also want
I am currently creating a program that dynamically adds Image to a dockPanel every
I am creating a program that communicates with a PHP script on a web
I am creating a client program that talks to a server (which I programmed).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.