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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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