Thread: trying to read numbers from a file - only getting zeros

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    3

    trying to read numbers from a file - only getting zeros

    Hi! I'm a little rusty on C but I have been working on a program that needs to read from two files and compare values/do math. I'm trying to make a struct for each file, skipping the first few lines because they contain strings. However, whenever I try to do this I only get zeros in the struct. I'm not sure why this keeps happening. I think it might be something with malloc and the pointers. Thanks for your help.

    also I will attach an example of the file I'm reading.
    trying to read numbers from a file - only getting zeros-screen-shot-2020-07-28-2-58-57-pm-jpg

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct fcat_s{
        float x;
        float y;
        float a_j2000;
        float b_j2000;
        float mag;
    } fcat_s;
    
    typedef struct cat_s{
        float num;
        float x;
        float y;
        float xworld;
        float yworld;
        float flux_auto;
        float mag_auto;
        float awin;
        float bwin;
    } cat_s;
    
    void readFCAT(FILE *fcat, fcat_s *f, int fcatcount){
        int i;
        for (i=0;i<(fcatcount);i++){
            if (i>5){
                fscanf(fcat, "%f", &f[i-5].x);
                fscanf(fcat, "%f", &f[i-5].y);
                fscanf(fcat, "%f", &f[i-5].a_j2000);
                fscanf(fcat, "%f", &f[i-5].b_j2000);
                fscanf(fcat, "%f", &f[i-5].mag);
            }
        }
    }
    
    void readCAT(FILE *cat, cat_s *c, int catcount){
        int j;
        for (j=0;j<(catcount);j++){
            if (j>9){
                fscanf(cat, "%f", &c[j-9].num);
                fscanf(cat, "%f", &c[j-9].x);
                fscanf(cat, "%f", &c[j-9].y);
                fscanf(cat, "%f", &c[j-9].xworld);
                fscanf(cat, "%f", &c[j-9].yworld);
                fscanf(cat, "%f", &c[j-9].flux_auto);
                fscanf(cat, "%f", &c[j-9].mag_auto);
                fscanf(cat, "%f", &c[j-9].awin);
                fscanf(cat, "%f", &c[j-9].bwin);
            }
        }
    }
    
    void printFCAT(fcat_s *f, int fcatcount){
        int i=0;
        for(i=0;(i<(fcatcount-5));i++){
            printf("%lf\t %lf\t %lf\t %lf\t %lf\n", f[i].x, f[i].y, f[i].a_j2000, f[i].b_j2000, f[i].mag);
        }
    }
    
    void printCAT(cat_s *c, int catcount){
        int i=0;
        for(i=0;(i<(catcount-9));i++){
            printf("%lf\t %lf\t %lf\t %lf\t %lf\t %lf\t %lf\t %lf\t %lf\n", c[i].num, c[i].x, c[i].y, c[i].xworld, c[i].yworld, c[i].flux_auto, c[i].mag_auto, c[i].awin, c[i].bwin);
        }
    }
    
    int main(void) {
    
        float exptime = 0;
        float F = 0;
        float Mi = 0;
        float Mcat = 0;
        float FLUX_AUTO = 0;
        float ZP = 0;
        char fcatname[50];
        char catname[50];
        int fcatcount = 0;
        int catcount = 0;
        char fcat_c;
        char cat_c;
        fcat_s *f;
        cat_s *c;
    
        printf("Please input the .fcat file name:\n");
        scanf("%str", fcatname);
        
        printf("Please input the .cat file name:\n");
        scanf("%str", catname);
    
        printf("Please input the exposure time:\n");
        scanf("%f", &exptime);
        
        FILE* fcat;
        fcat = fopen(fcatname, "r");
    
        if (fcat == NULL) {
            printf("The input file does not exist\n");
        }
        else {
            for (fcat_c = getc(fcat); fcat_c != EOF; fcat_c = getc(fcat)){
                    if (fcat_c == '\n')
                        fcatcount++;
            }
        }
        
        FILE* cat;
           cat = fopen(catname, "r");
        
        if (cat == NULL) {
            printf("The input file does not exist\n");
        }
        else {
            for (cat_c = getc(cat); cat_c != EOF; cat_c = getc(cat)) {
                    if (cat_c == '\n')
                        catcount++;
              //  printf("%c", cat_c);
            }
        }
        
        printf("\n");
        printf("The .fcat file has %d lines. \n", fcatcount);
        printf("The .cat file has %d lines. \n", catcount);
        
        printf("\n\n");
        
        f = (fcat_s*)malloc(fcatcount*sizeof(fcat_s));
        c = (cat_s*)malloc(catcount*sizeof(cat_s));
        
        rewind(fcat);
        rewind(cat);
        
        readFCAT(fcat, f, fcatcount);
        readCAT(cat, c, catcount);
        
        printf("FCAT CONTENTS\n");
        printFCAT(f, fcatcount);
        
        printf("\n\n");
        
        printf("CAT CONTENTS\n");
        printCAT(c, catcount);
        
        fclose(fcat);
        fclose(cat);
        
    
        return 0;
    }
    Attached Images Attached Images trying to read numbers from a file - only getting zeros-screen-shot-2020-07-28-3-03-26-pm-jpg trying to read numbers from a file - only getting zeros-screen-shot-2020-07-28-3-03-16-pm-jpg trying to read numbers from a file - only getting zeros-screen-shot-2020-07-28-3-03-21-pm-jpg 
    Last edited by halfgracexx; 07-28-2020 at 04:07 PM.

  2. #2
    Registered User
    Join Date
    Jul 2020
    Posts
    3
    It reads from the file if I use fgets, but I need it to be numbers in order to do math. It prints the correct number of lines, just all the values are zeros.
    The .fcat file has 214 lines.
    The .cat file has 1874 lines.




    FCAT CONTENTS
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How many times are you trying to read the files? It appears to me that you are trying to read each file more than once, without resetting the file flags and FILE*.

    You really should be checking that your read operations actually succeeded every time.

  4. #4
    Registered User
    Join Date
    Jul 2020
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    How many times are you trying to read the files? It appears to me that you are trying to read each file more than once, without resetting the file flags and FILE*.

    You really should be checking that your read operations actually succeeded every time.
    I am trying to read each twice. The second time I am trying to put all the lines into a struct.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'd suggest reading in a single pass instead. Do this by reading line by line into a sufficiently long string. Check if the line begins with a '#', and if so, you can skip it. Otherwise, parse it for the numeric values.

    For saving the numeric values, you can start with a dynamic array of a small capacity (number of elements allocated) with zero size (number of elements in use). As you read the numeric values into struct objects and populate the dynamic array with these struct objects, the size will eventually reach the capacity. At that point, to add more numeric value struct objects, you expand the capacity of the dynamic array by a factor (typically 1.5 or twice). So, by keeping track of both size and capacity, you can read in a single pass rather than having a first pass to find the size and then a second pass to do the actual reading and extraction.
    Last edited by laserlight; 07-28-2020 at 05:53 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2013, 09:47 PM
  2. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  3. How to read numbers out of a file in c
    By muskateer1 in forum C Programming
    Replies: 3
    Last Post: 04-05-2012, 02:29 PM
  4. read file? zeros
    By Question in forum C++ Programming
    Replies: 8
    Last Post: 03-12-2003, 08:19 PM
  5. data read zeros?
    By Question in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2003, 01:15 PM

Tags for this Thread