0

I am scanning in an array of doubles from a file. I can print this array to retrieve the correct values for each line in the file.

My problem occurs when I try to scan in another file into another array during this process.

I have:

// // #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main (int argc, char **argv) { double c[13], d[13]; char filename[20]; double x1, x2, y1, y2, z1, z2, d1, d2, au, aux, auy; double dx, dy, dz, nnid, chk, nn; int n, b; char *in; in=argv[1]; FILE* infile; FILE* inbfile; infile = fopen(in, "r"); inbfile = fopen("copy.bt", "r"); while (!feof(infile)) { fscanf(infile, "%f %f %e %e %e %e %e %e %e %e %e %e %e %f",c,c+1,c+2,c+3,c+4,c+5,c+6,c+7,c+8,c+9,c+10,c+11,c+12,c+13); printf("Selected Particle A: %f\n",c[0]); n=0; while (!feof(infile)) { printf("%f\n",c[0]); fscanf(infile, "%f %f %e %e %e %e %e %e %e %e %e %e %e %f",d,d+1,d+2,d+3,d+4,d+5,d+6,d+7,d+8,d+9,d+10,d+11,d+12,d+13); printf("%f\n",c[0]); printf("Selected Particle B: %f\n",d[0]); /**/ printf("%f = %f ?\n",c[0],d[0]); if (c[0]==d[0]) { printf("Same Particle SKIP...\n"); } else { dx = (d[4])-(c[4]); dy = (d[5])-(c[5]); dz = (d[6])-(c[6]); printf("dx dy dz %e %e %e\n",d[4],d[5],d[6]); /**/ if (n == 0) { au=pow(((dx*dx*dx)+(dy*dy*dy)+(dz*dz*dz)),(1.0/3.0)); printf("%f is %e from %f\n",c[0],au,d[0]); } else { aux=pow(((dx*dx)+(dy*dy)+(dz*dz)),(1.0/3.0)); printf("%f is %e from %f\n",c[0],aux,d[0]); if (aux < au) { au = aux; nnid = d[0]; } } /**/ } n++; nn=d[1]; /**/ } printf("%f Is Particle %f At %e\n", c[1], nnid, au); } fclose(infile); fclose(inbfile); return 0; } 

So why does the value change? All I've done is scan in the first line of another file into a separate array.

9
  • 3
    You need to show your code more precisely. We can't see what you're doing from that pseudo-code. You should aim for an SSCCE (a Short, Self-Contained, Complete Example) so we can see what you're doing that gives the unexpected (to you) behaviour. In this case, the devil is going to be in the details of the variable declarations and the fscanf() argument lists. Commented Dec 2, 2012 at 16:38
  • 3
    Plus you seem to be using feof() in a wrong way. Commented Dec 2, 2012 at 16:39
  • 2
    You also seem to be using code comments in a wrong way. Commented Dec 2, 2012 at 16:40
  • 1
    C is not Pascal. The correct use of feof() in C is after a routine reports failure and you need to distinguish between EOF and a formal error. You should be using while (fscanf(infilea, "...", ...) == 4) { ... while (fscanf(infileb, "...", ...) == 4) { ... } ... }. The 4 is the number of conversions you expect (the number of %e and %f listed in the format strings). Any shortfall indicates a problem; it might be a format error (an alphabetic character where a number was expected), or it might be EOF. After the loop, it would be legitimate to use feof() to distinguish the cases. Commented Dec 2, 2012 at 16:45
  • 2
    Yes, I could guess that myself, but how are they defined? Commented Dec 2, 2012 at 16:48

2 Answers 2

3

You are attempting to read 14 values into an array with 13 elements, resulting in undefined behavior. Declare the arrays as float c[14], d[14] instead.

When you declare d as float d[13], d+12 points to the last element in the array, and d+13 is past the end of the array so reading into it is not allowed.

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

1 Comment

If I could curse on here, I would. Thank you interjay!
1

To add to Interjay's answer: you're invoking undefined behavior at an other place. You're trying to scan doubles using the %f format specifier, whereas the correct format specifier for double is %lf.

Edit: it seems you were giving us the wrong code, so the statement above is not quite right. (However, I'll keep this as a reference for future visitors.)

6 Comments

But then I get: warning: format ‘%lf’ expects type ‘double *’, but argument 3 has type ‘float *’
@Protoplanet: Then what you've shown is not the code you're compiling.
In a comment above he said the arrays are defined float c[13], d[13], I guess that's the version he's actually using...
I changed the arrays to [14] but I'm running what you see above.
@Protoplanet so are the arrays double or float, finally?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.