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 8259257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:49:02+00:00 2026-06-08T02:49:02+00:00

Trying to work with C I/O currently. I have a file that only holds

  • 0

Trying to work with C I/O currently. I have a file that only holds integers and there is only one per line.. not commas, etc.. what is the best way to read them in:

//WHILE NOT EOF
    // Read a number
    // Add to collection

I am creating 2 files which is working fine.. but ultimately, I want to read them both in, join them into one collection, sort them and then print them out to a new file. There’s no need for you to do all that for me, but please help with the above.. here is my effort so far:

void simpleCopyInputToOutput(void);
void getSortSave(void);

int main()
{
    //simpleCopyInputToOutput();
    getSortSave();

    system("PAUSE");
    return 0;
}

void getSortSave(void)
{
    FILE *fp1;
    FILE *fp2;
    FILE *fpMerged;

    printf("Welcome. You need to input 2 sets of numbers.\n");
    printf("Please input the first sequence. Press 0 to stop.\n");

    if ((fp1 = fopen("C:\\seq1.txt", "w")) == NULL)
    {
        printf("Cannot open or create first file!\n");
        exit(1);
    }

    int num;
    int i = 1;
    while (num != 0)
    {
        printf("Please input value # %d\n", i);
        scanf("%d", &num);

        if (num == 0)
        {
            break;
        }

        fprintf(fp1, "%d\n", num);
        i++;
    }

    printf("Please input the second sequence. Press 0 to stop.\n");
    if ((fp2 = fopen("C:\\seq2.txt", "w")) == NULL)
    {
        printf("Cannot open or create second file!\n");
        exit(1);
    }

    num = -1;
    i = 1;
    while (num != 0)
    {
        printf("Please input value # %d\n", i);
        scanf("%d", &num);

        if (num == 0)
        {
            break;
        }

        fprintf(fp2, "%d\n", num);
        i++;
    }

    fclose(fp1);
    fclose(fp2);

    if ((fp1 = fopen("C:\\seq1.txt", "r")) == NULL)
    {
        printf("Cannot open first file!\n");
        exit(1);
    }

    //WHILE NOT EOF
    // Read a number
    // Add to collection

    //TODO: merge ints from both files, sort and output to new file
}
  • 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-08T02:49:03+00:00Added an answer on June 8, 2026 at 2:49 am

    I would suggest you use fgets:

    char buffer[16];
    while (fgets(buffer, sizeof(buffer), fp1))
    {
        long value = strtol(buffer, NULL, 10);
    
        /* Use the value... */
    }
    
    /* fgets failed ro read, check why */
    if (!feof(fp1))
        printf("Error: %s\n", strerror(errno));
    

    Edit: How to get the number of entries in the file: If you don’t keep track of it any other way (like e.g. having the number of items being the first line), the only solution may be to read the file twice. Once to count the number of lines, and once to read the actual numbers. Use fseek or rewind after the counting to “rewind” the read pointer to the beginning of the file.

    I would personally put the counting in a separate function, and also the actual reading. This way you don’t have to duplicate code if you want to read from multiple files.

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

Sidebar

Related Questions

I'm currently trying to make cx_freeze to work on a Solaris workstation I have
My webpage i am currently working on only has one main content div that
I have been trying to get this to work correctly and I think I
I ask this because I am currently trying to work out my own technique,
Hey all, here is the issue I am currently trying to work through. We
I'm currently trying to get ActiveAdmin to work on a model of mine but
Erlang version: R13B01 Currently I'm struggling trying to make Erlang work with SSL. The
Trying to work with groupby so that I can group together files that were
When trying to work with Qt's signal/slot mechanisms over more than one level of
I am currently trying to load a slightly large xml file into a dataset.

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.