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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:08:01+00:00 2026-06-06T15:08:01+00:00

I have multiple textfiles with data as follows: 0.000000E+0 0.000000E+0 1.800000E-1 0.000000E+0 4.200000E-1 0.000000E+0

  • 0

I have multiple textfiles with data as follows:

    0.000000E+0 0.000000E+0
    1.800000E-1 0.000000E+0
    4.200000E-1 0.000000E+0
    6.950000E-1 0.000000E+0
    9.450000E-1 0.000000E+0
    1.225000E+0 0.000000E+0

which was generated by a test instrument. The two columns of data are separated by the tab character. I am trying to read this data file into a program for further processing. The code which i wrote is as follows:

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

float **make_array(int size); //function to make 2D array

int main(int argc, char **argv)
{
        FILE *infile;
        infile=fopen(argv[1], "r");

//count number of lines in file
        char c;
        int i=0, numlines=0;
        while((c=fgetc(infile))!=EOF)
                if(c == '\n') numlines++;

        printf("numlines: %d\n", numlines);

        float **entry = make_array(numlines);

//read inputfile
        for(i=0; i< numlines; i++)
                fscanf(infile, " %G\t%G", &entry[0][i], &entry[1][i]);
//verify read values
        printf("\n");
        for(i=0; i<numlines; i++)
                printf("%E\t%E\n", entry[0][i], entry[1][i]);

//clean up before exiting
        free(entry[0]);
        free(entry[1]);
        free(entry);
        fclose(infile);
return 0;
}


//create a 2D array of 2 by {size}
float **make_array(int size){
        int i;
        float **entry = malloc(2*sizeof(float *));
        if(entry==NULL) exit(1);
        entry[0] = malloc(size*sizeof(float *));
        for(i=0;i<size;i++) entry[0][i] == 0.0;
        entry[1] = malloc(size*sizeof(float *));
        for(i=0;i<size;i++) entry[1][i] == 0.0;

return entry;
}

When i compile and run the program, it prints out the number of lines in the file correctly but the printf statement to verify the data read into the array entry just prints out all zeros, like below:

numlines: 252

0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00
0.000000E+00    0.000000E+00

I tried different format specifiers for fscanf like %g,%e,%f,%G etc but they all give the same result. Also, i am using Ubuntu 12.04 with GCC 4.6. Any pointers to what’s wrong would be greatly appreciated.

Khadija.

  • 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-06T15:08:02+00:00Added an answer on June 6, 2026 at 3:08 pm

    Your initial loop:

    while((c=fgetc(infile))!=EOF)
        if(c == '\n') numlines++;
    

    read the entire input file (to determine the size of the array). Once you’ve done that, your calls to fscanf all fail because you’re already at the end of the file.

    Assuming the input file is seekable (which it should be if it’s a disk file), you can use rewind() or fseek() to go back to the beginning and read it again.

    BTW, your use of %G and %E format specifiers is valid, but %g and %e are more common (and should work in exactly the same way).

    And you really should check the value of argc before referring to argv[1]; the program will likely blow up if invoked with no command-line arguments. You should also check whether fopen() succeeded, and that fscanf() returned 2 to indicate that it successfully read two data items.

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

Sidebar

Related Questions

I have an excel workbook in which there is data from multiple text files.
I have multiple text files with logged data like this: 6/23/09 17:00 0.443 6/23/09
I have multiple projects which are to be hosted together in a Tomcat container,
I have multiple Windows programs (running on Windows 2000, XP and 7), which handle
I have a data file with multiple rows, and 8 columns - I want
I have a textfile which consists of multiple words. They are all separated by
I have data in multiple text files that look like this: 1 DAEJ X
I have a program that creates multiple text files of rdf triples. I need
I have multiple textfields on my view, and each time a textfield is taped
I have multiple ajax requests with javascript code as response, and I need to

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.