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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:10:26+00:00 2026-05-20T15:10:26+00:00

I have a file numbers.dat containing about 300 numbers(floating point,negative positive)in column format. The

  • 0

I have a file numbers.dat containing about 300 numbers(floating point,negative positive)in column format. The objective is to first fill in numbers.dat with 300 numbers and then extract 100 numbers each time into another file say n1.dat. The second file n2.dat will have the next 100 numbers from numbers.dat and so on for 3 subsets of files obtained from number.dat. I am unable to understand how the location of the last read 100th number is taken into account so that the file read and fetching for the next block occurs after the previos fetched number.

Trying out the Solution provided by Gunner :

FILE *fp = fopen("numbers.dat","r"); 
FILE *outFile1,*outFile2,*outFile3; 
int index=100; 

char anum[100]; 
while( fscanf(fp,"%s",anum) == 1 ) 
    {
 if(index==100)
     {
// select proper output file based on index.
 fprintf(outFile1,"%s",anum);
     index++; }
     if(index >101)
     {
        fprintf(outFile2,"%s",anum);
     index++; }
}

The problem is only one data is being written. What should be the correct process?

  • 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-05-20T15:10:26+00:00Added an answer on May 20, 2026 at 3:10 pm

    I’d write a program for that as

    read data from input file line-by-line
    keep a line count
    based on the current line count copy the line to a specific output file
    

    something like this

    #include <stdio.h>
    #include <stdlib.h>
    
    #define INPUTFILENAME "numbers.dat"
    #define MAXLINELEN 1000
    #define NFILES 3
    #define LINESPERFILE 100
    #define OUTPUTFILENAMETEMPLATE "n%d.dat" /* n1.dat, n2.dat, ... */
    
    int main(void) {
        FILE *in, *out = NULL;
        char line[MAXLINELEN];
        int linecount = 0;
    
        in = fopen(INPUTFILENAME, "r");
        if (!in) { perror("open input file"); exit(EXIT_FAILURE); }
        do {
            if (fgets(line, sizeof line, in)) {
                if (linecount % LINESPERFILE == 0) {
                    char outname[100];
                    if (out) fclose(out);
                    sprintf(outname, OUTPUTFILENAMETEMPLATE, 1 + linecount / LINESPERFILE);
                    out = fopen(outname, "w");
                    if (!out) { perror("create output file"); exit(EXIT_FAILURE); }
                }
                fputs(line, out);
                linecount++;
            } else break;
        } while (linecount < NFILES * LINESPERFILE);
        fclose(in);
        if (out) fclose(out);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.