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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:12:21+00:00 2026-06-17T14:12:21+00:00

Can someone tell me where I’m going wrong here? basically I’ve written some code

  • 0

Can someone tell me where I’m going wrong here? basically I’ve written some code that should take values from a file and then print the first value from the file as output along with the number of values in the file.

Although I get the correct number of values, my first value printed in the output isn’t the same as the first value in my file. here’s the code:

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

int main()
{
    const char Project_Data[] = "filedata.dat";
    FILE *input = fopen(Project_Data, "r");

    int t = 0;
    int N = 0;
    float *a;

    a = (float*)malloc(N*sizeof(float));

    if(input != (FILE*) NULL)
    {
        while(fscanf(input, "%e", &a[t]) == 1)
        {
            N++;
            if(a[t] == EOF)
                break;
        }

        printf("first value in file: %e\n",a[0]);
        printf("number of values in file: %d\n", N);
    }
    else
        printf("coudlnt read input file.\n");

    return(0);
}

I feel it’s got something to do with how I’m using the malloc function. If so, how do I use it correctly? I want to read my values from the file into an array size N, but how can I do that if I don’t know the value of N beforehand?


EDIT:

Here’s some of the values from the file. The values are ordered one after the other with a single space between them. Because the number of values is quite large, they kind of form a several diagonals across the page (if you’ve it before).

   9.0100000e+00   8.9663752e-01
   9.0200000e+00   1.5041077e+00
   9.0300000e+00   2.5992505e+00
   9.0400000e+00   1.5242828e+00
   9.0500000e+00   3.6815660e-01
   9.0600000e+00   5.4889676e-01
   9.0700000e+00   1.2371257e+00
   9.0800000e+00   1.2163317e+00
   9.0900000e+00   5.4318479e-01
   9.1000000e+00   1.5906641e+00
   9.1100000e+00   2.6775285e+00
   9.1200000e+00   1.1608307e+00
   9.1300000e+00   1.2084299e+00
   9.1400000e+00  -7.8752191e-01
   9.1500000e+00   6.4048690e-01
   9.1600000e+00   2.2727416e-02
   9.1700000e+00   1.0307653e+00
   9.1800000e+00   1.9435864e+00
   9.1900000e+00   2.9422693e+00
   9.2000000e+00   3.2184945e+00
   9.2100000e+00   1.3041157e+00
   9.2200000e+00   1.1018038e+00

The values on the left aren’t orders; they are genuine values, it’s just the other data is meant to be the ‘noise’ in the data i think.

  • 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-17T14:12:23+00:00Added an answer on June 17, 2026 at 2:12 pm

    You can allocate an array with, say, 10 elements at first. While reading the file, remember to increase the index variable (t in your example). Should t become greater than your allocated size, use realloc to double the size of your array. This is how most dynamic vector implementations work as far as I know.

    Something like this:

    int capacity = 10;
    float* a = (float*)malloc(capacity*sizeof(float));
    int t = 0;    
    
    while(fscanf(input, "%e", &a[t]) == 1) {
       t++;
       if (t >= capacity) {
          capacity *= 2;
          void* temp = realloc(a, capacity*sizeof(float));
          if (temp == 0) exit(1); // or handle allocation failure properly, a is still valid
          a = (float*)temp;
       }
    }
    

    At this point a should contain your items and t the number of items read.

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

Sidebar

Related Questions

Can someone tell me whats going wrong in this code! testit :- read(Term1), read(Term2),
Can someone tell me what's wrong with this code? I'm trying to upload a
can someone tell me what i am doing wrong here.. i am not getting
Can someone tell me what I'm doing wrong here? I want my background image
Can someone tell me what is wrong with the following code? Set<String> cmds =
Can someone tell me the code to convert image into byte array and that
Can someone tell me what's wrong in the below code, I am getting syntax
Can someone tell me what I am doing wrong here: function onAjaxDone(data) { var
Can someone tell me what I'm doing wrong here and how to make the
Can someone tell me what are the maximum and minimum int values that the

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.