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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:59:55+00:00 2026-06-17T18:59:55+00:00

I posted a problem yesterday regarding a certain segment of my code. The aim

  • 0

I posted a problem yesterday regarding a certain segment of my code. The aim was to basically scan in data values from a .dat file into an array, print the values whilst also counting how many values were in the file.

Sounds pretty simple, but my program only seemed to print a certain number of the values. More specifically, of a data file containing over 300000 values, it would only print the last 20000 and nothing else.

So I left it, finished the rest of my code and now it’s the last part I have to sort. I’ve made a few changes and tried actually printing an output .dat file now so I can see what I’m getting. The code is below by the way.

Initally I assumed perhaps it was something related to the memory allocation of my array (was getting a segmentation error? when putting the whole code together) so I created an external function that counted the number of values instead (that works).

My only problem now is that it still only chooses to print 20000 values and then the rest are 0s. I was thinking perhaps it had something to do with the type but they all contain 7 dps in scientific notation. Here’s a sample of some of the values:

   8.4730000e+01   1.0024256e+01
   8.4740000e+01   8.2065599e+00
   8.4750000e+01   8.3354644e+00
   8.4760000e+01   8.3379525e+00
   8.4770000e+01   9.8741315e+00
   8.4780000e+01   9.0966478e+00
   8.4790000e+01   9.4760274e+00
   8.4800000e+01   7.1199807e+00
   8.4810000e+01   7.1990172e+00

Anyone see where I’m going wrong? I’m sorry for the long question, it’s just been bugging me for the last day or so and no matter what I change nothing seems to help. Any kind of input would be greatly appreciated.

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

int count(int);

const char df[]="data_file.dat";
const char of[]="output_file.dat";

int main(int argc, char *argv[])
{
    FILE *input, *output;
    int   i, N;
    float *array;

    N = count(i);

    input = fopen(df, "r");
    output = fopen(of, "w");

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

    if((input != (FILE*) NULL) && (output != (FILE*) NULL))
    {
        for(i = 0; i < N; i++)
        {
            fscanf(input, "%e", &array[i]);
            fprintf(output, "%d %e\n", i, array[i]);
        }

        fclose(input);
        fclose(output);
    }
    else
        printf("Input file could not be opened\n");

    return(0);
}

int count(int i)
{
    FILE *input;
    input = fopen(df, "r");

    int N = 0;

    while (1)
    {
        i = fgetc(input);
        if (i == EOF)
            break;
        ++N;
    }

    fclose(input);

    return(N);
}
  • 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-17T18:59:56+00:00Added an answer on June 17, 2026 at 6:59 pm

    Your biggest problem is that count() doesn’t count float values; it counts how many characters are in the file. Then you try to loop and call fscanf() more times than there are values in the file. The first times, fscanf() finds a float value and scans it; but once the loop reaches the end of file, fscanf() will be returning an EOF status. It seems possible that fscanf() will set the float value to 0.0 when it returns EOF.

    I suggest you rewrite so that you don’t try to pre-count the float values. Write a loop that just repeatedly calls fscanf() until it returns an EOF result, then break out of the loop and close the files.

    P.S. If you are going to write a function like count(), you should pass in the filename as an argument instead of hard-coding it. And your version of count() takes an integer argument but just ignores the value; instead, just declare a temp variable inside of count().

    EDIT: Okay, here is a complete working program to solve this problem.

    #include <stdio.h>
    
    int
    main(int argc, char **argv)
    {
        FILE *in_file, *out_file;
        unsigned int i;
    
        if (argc != 3)
        {
            fprintf(stderr, "Usage: this_program_name <input_file> <output_file>\n");
            return 1; // error exit with status 1
        }
    
        in_file = fopen(argv[1], "r");
        if (!in_file)
        {
            fprintf(stderr, "unable to open input file '%s'\n", argv[1]);
            return 1; // error exit with status 1
        }
    
        out_file = fopen(argv[2], "w");
        if (!out_file)
        {
            fprintf(stderr, "unable to open output file '%s'\n", argv[2]);
            return 1; // error exit with status 1
        }
    
    
        for (i = 0; ; ++i)
        {
            int result;
            float x;
    
            result = fscanf(in_file, "%e", &x);
            if (1 != result)
                break;
    
            fprintf(out_file, "%d %e\n", i, x);
        }
    
        return 0; // successful exit
    }
    

    Note that this version doesn’t need to allocate a large array; it just needs a single temporary float variable. Maybe your program will need to store all the float values. In that case, write a count() function that uses a loop similar to the above loop, using fscanf() to count float values.

    Also note that this program checks for errors after calling fopen() and fscanf().

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

Sidebar

Related Questions

Following on from a question I posted yesterday about GUIs, I have another problem
I posted some code here which correctly solved a problem the poster had. OP
I have posted this problem yesterday..but unfortunately, no one has given me the solution
I have made some progress on the problem I posted about yesterday, so I
Yesterday I posted a similar question on why my code started on my content
I posted a problem and got an answer here . Here is the code
I posted a question yesterday here: Finding and adding to a .kml file using
This question follows on from the problem posted here when i run explain I
Somebody posted me this code to me with a problem I had: $('#txtWeight').each( function()
Similar to the question I posted yesterday , I have this problem that I

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.