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.

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
  • 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](http://sscce.org/)) 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. – Jonathan Leffler Dec 02 '12 at 16:38
  • 3
    Plus you seem to be using [feof()](http://stackoverflow.com/questions/4299296/how-to-use-feoffile-f) in a wrong way. – axiom Dec 02 '12 at 16:39
  • 2
    You also seem to be using code comments in a wrong way. – gspr Dec 02 '12 at 16:40
  • They're not proper comments in the code is why. I'm using feof() incorrectly? – Protoplanet Dec 02 '12 at 16:42
  • 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. – Jonathan Leffler Dec 02 '12 at 16:45
  • 2
    Yes, I could guess that myself, but *how are they defined*? – interjay Dec 02 '12 at 16:48
  • 1
    @Protoplanet Why don't you just show the **actual** code? Do you have something to keep secret? –  Dec 02 '12 at 16:49
  • I have uploaded the actual code. – Protoplanet Dec 02 '12 at 16:51
  • @Protoplanet: in your comment, you say `float c[13], d[13];` but in the uploaded code you say `double c[13], d[13];`. Which is it? It matters because your format specifiers are correct for `float` and incorrect for `double`, as @H2CO3 noted in his answer. Consistency and accuracy in programming is very important; little details really matter. – Jonathan Leffler Dec 02 '12 at 17:05

2 Answers2

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.

interjay
  • 107,303
  • 21
  • 270
  • 254
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.)