1

I have a code that reads two .txt files and puts them into structs. Then, I want to compare the x value from each file and find matches. The type of these values is floats. I keep getting incorrect matches and line numbers and I'm not sure what to do. It works for the first few but then it just says there's a match when it is only a value in one file. Everything else works fine, so if anyone has suggestions on comparing the values that would be much appreciated. Thank you.

#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #define FLT_EPSILON 1.19209290E-07F 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; 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; float x; char str[100]; int i = 0; int j = 0; float temp; int match = 0; 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); fcat_s *f = (fcat_s *)malloc(sizeof(fcat_s)); cat_s *c = (cat_s *)malloc(sizeof(cat_s)); // .fcat file 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++; if (fcatcount > 4) { fscanf(fcat, "%f", &f[fcatcount-5].x); fscanf(fcat, "%f", &f[fcatcount-5].y); fscanf(fcat, "%f", &f[fcatcount-5].a_j2000); fscanf(fcat, "%f", &f[fcatcount-5].b_j2000); fscanf(fcat, "%f", &f[fcatcount-5].mag); } } } printf("\n"); printf("The .fcat file has %d lines. \n", fcatcount); printf("\n"); printf("\n"); printf("FCAT CONTENTS\n"); 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); } printf("\n"); printf("\n"); // .cat file 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++; if (catcount > 8) { fscanf(cat, "%f", &c[catcount-9].num); fscanf(cat, "%f", &c[catcount-9].x); fscanf(cat, "%f", &c[catcount-9].y); fscanf(cat, "%f", &c[catcount-9].xworld); fscanf(cat, "%f", &c[catcount-9].yworld); fscanf(cat, "%f", &c[catcount-9].flux_auto); fscanf(cat, "%f", &c[catcount-9].mag_auto); fscanf(cat, "%f", &c[catcount-9].awin); fscanf(cat, "%f", &c[catcount-9].bwin); } } } printf("\n"); printf("The .cat file has %d lines. \n", catcount); printf("\n"); printf("\n"); printf("CAT CONTENTS\n"); 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); } printf("\n"); printf("\n"); // searching in the files for a match for (i = 0; (i <= (fcatcount-5)); i++){ for (j = 0; (j <= (catcount-9)); j++) { if (fabs(f[i].x - c[j].x) < FLT_EPSILON && fabs(f[i].y - c[j].y) < FLT_EPSILON) { printf("%f\t .fcat line: %d\t .cat line: %d\n", c[j].x, i, j); match++; } } } printf("\n"); printf("\n"); printf("The files have %d matches. \n", match); fclose(fcat); fclose(cat); free(f); free(c); return 0; } 
1
  • 1
    One problem you have is that your epsilon value is not magnitude-sensitive. See here for a generally good discussion. Commented Jul 30, 2020 at 21:19

1 Answer 1

3

You only allocated space for one fcat_s:

fcat_s *f = (fcat_s*)malloc(sizeof(fcat_s)); 

But in your first loop, you are already treating f like an array:

... fscanf(fcat, "%f", &f[fcatcount-5].x); ... 

Unless I am missing something, it seems that you are reading and writing to unallocated memory. I am surprised your program doesn't segfault.

Sign up to request clarification or add additional context in comments.

3 Comments

how can I fix this? I'm not sure how to do it because I get the fcatcount later on, and I need it beforehand.
There are many possible solutions. You could make two passes through the file, or use realloc, or use a linked list.
I fixed it using two passes, this also allowed me to fix my comparison code. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.