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

  • Home
  • SEARCH
  • 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 7432067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:23:51+00:00 2026-05-29T09:23:51+00:00

When I try to read integers from a file, the code ends up reading

  • 0

When I try to read integers from a file, the code ends up reading different numbers than expected.

The data file contains the following numbers:

1111 111 222 333 444 555 666 777 888 -1

When I read the file, it returns the following numbers:

825307441   825307424   842150432   858993440   875836448
892679456   909522464   926365472   943208480   170994976

Why is this happening?

Relevant code:

/* A file named data contains some integer numbers.write a program to
   read these numbers and store the odd numbers in one file and store 
   the even numbers in file.*/

  int main()
  {
    FILE *fp1,*fp2,*fp3;
    int number;

    fp1=fopen("DATA","r");
    fp2=fopen("even","w");
    fp3=fopen("odd","w");

    printf("numbers in  file are\n");
    while((number=getw(fp1))!=EOF)
    {
      printf("%d\t",number);
    }
    fclose(fp1);
    fp1=fopen("DATA","r");


    while((number=getw(fp1))!=EOF)
    {
      if((number%2)==0)
      {
        putw(number,fp2);
      }
      else
      {
        putw(number,fp3);
      }
    }
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);

    fp2=fopen("even","r");
    fp3=fopen("odd","r");

    printf("\nnumbers in even file are\n");
    while((number=getw(fp2))!=EOF)
    {
      printf("%d\t",number);
    }

    printf("\nnumbers in odd file are\n");
    while((number=getw(fp3))!=EOF)
    {
      printf("%d\t",number);
    }
    fclose(fp2);
    fclose(fp3);
    return 0;

  }
  • 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-29T09:23:52+00:00Added an answer on May 29, 2026 at 9:23 am

    You are reading numbers represented in plain text.

    getw reads a machine word (say 8 bytes on my 64-bit box for example), and when you write it out, it writes that out. This is not what you want.

    Use fscanf instead of getw to read formatted values.

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

Sidebar

Related Questions

In the following code, I try to read data from a plist: -(void)readPreferences {
I try to read file /proc/'pid'/status, using c program. The code is as follows,
I'm using the following code to try to read the results of a df
I'm reading lines from a file, and I believe that once I've read all
Im trying to read in image file from a server , with the code
So far I have: //read in data from CSV file and separate it into
I get a warning in MSVC++ when I try to read an integer from
Did anyone try to read programmatically an Alibre Design CAD file? I see that
fopen is failing when I try to read in a very moderately sized file
I have a very big file 4GB and when I try to read it

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.