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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:47:21+00:00 2026-06-05T11:47:21+00:00

When writing values to a file in C under Windows, I’m getting something which

  • 0

When writing values to a file in C under Windows, I’m getting something which seems to be wrong, and the result differs from the same program run in Cygwin.

In this case, I’m writing a float to a file:

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>

int         main( void )
{
    int     fd;
    float   f;

    fd = open("file.a", O_RDWR | O_CREAT);
    f = (float)atof("-0.1352237");
    printf("%.7f\n", f);

    write(fd, (void *)&f, sizeof(float));
    close(fd);
    printf("sizeof(float): %d\n", sizeof(float));

    return (EXIT_SUCCESS);
}

I’ve compiled and run this file on both Windows and in Cygwin (without the include), and I’m not getting the same result. Since I’m writing a float, I would expect the output file to have 4 bytes written to it

However, the output when compiled with cl.exe in the command line, seems wrong:

1578 0d0a be

If I read from the file to a float, I’m not getting the correct value, obviously. And the amount of bytes written to the file is wrong to, it should only be 4 bytes, not 5.

This is what I get when running in Cygwin:

1578 0abe 

This is correct. If I read this from the file to a float I get the correct value (-0.1352237).

Am I missing something here ? Are the windows implementation of those functions flawed ?
Or maybe I’m not writing the data properly ?

  • 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-05T11:47:24+00:00Added an answer on June 5, 2026 at 11:47 am

    You have opened the file in text mode (the default), so the 0a ("\n") is being translated to 0d0a ("\r\n").

    You need to open the file in binary mode, using the O_BINARY flag.

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

Sidebar

Related Questions

I am writing a simple foreach loop to read key values from appconfig file.
i'm writing some code for getting some values including course -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
I'm writing a small function to generate values from the Normal distribution using Box-Muller
I am writing a system which requires me to fetch the values of properties
I am writing an iPhone application which requires the user to enter several values
So far all the Excel stuff revolves around opening a file, writing values to
I read a delimited text file into dataset. i am writing these dataset values
I'm having a major problem with writing values into a new CSV file. I
I am writing small process monitor script in Perl by reading values from Proc
Hi i am trying to save text values as binary file and read from

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.