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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:41:36+00:00 2026-05-13T07:41:36+00:00

i have a really big array of numbers with double precision…i tried to write

  • 0

i have a really big array of numbers with double precision…i tried to write it into a file using fprintf()…i need to write these numbers one in each line so i have done something like this.

if((fp2 = fopen("temp", "w")) == NULL) { perror("File cannot be opened"); exit(1); }

for(int k = 0; k < j; k++ )
{
fprintf(fp2, "%0.3lf\n", diff[k]);  
}

However, there is a problem that it writes the data upto certain number of lines after which i gives all zeroes. for example

3.040
0.700
-2.740
0.000
0.000
0.000
0.000
0.000
0.000

i really can’t understand what could be the problem. why does it write all values as 0.000 when there are values in the array.

here is how diff was implemented if it helps.

            diff = (double *)malloc(fileSize);

        diff[0] = data[0];


        for(j = 1; j < n; j++ )
        {
            diff[j] = data[j] - data[j-1];
        }

the values from a file were stored in data[]. i then calculated the difference of adjacent values in data[] into diff[] and write it back into another file. fileSize was the size of the original file. and i know for sure that all the values in diff[] are populated correctly.

  • 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-13T07:41:36+00:00Added an answer on May 13, 2026 at 7:41 am

    Adding as an official answer after the comments:

    You have the following line:

    
    diff = (double *)malloc(fileSize);
    

    Note that when you malloc(fileSize) you are allocating fileSize bytes in memory. Each double takes 8 bytes (more or less depending on platform) so you will have fileSize / 8 elements in your array. Are you SURE that n never exceeds the number of elements in your array?

    The reason why your program may work correctly with printf and not with fprintf is that memory bugs are very very subtle. If you start using memory that you haven’t allocated then things may be fine for a short period but go haywire when another part of the program quite legitimately decides to start using that piece of memory you’re naughtily using already.

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

Sidebar

Related Questions

I have a really big database (running on PostgreSQL) containing a lot of tables
I have a database full of customer data. It's so big that it's really
What should i use to code Classic ASP under Linux. I have really tried
We have some really old code that calls WebServices using behaviours (webservice.htc), and we
So I have an array that holds several numbers. As my script runs, more
Let's say I have an array of floating point numbers, in sorted (let's say
I'm using this Big Integer library for Javascript: http://www.leemon.com/crypto/BigInt.js and I need to be
We’ve found that the unit tests we’ve written for our C#/C++ code have really
I have some really complicated legacy code I've been working on that crashes when
I have never really found the design view in Visual Studio useful when developing

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.