Fread is overwriting other variables in my program

This is a discussion on Fread is overwriting other variables in my program within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am writing a program that graphs the concentrations of various phytoplankton species in the ocean. It reads ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    Fread is overwriting other variables in my program

    Hello everyone,

    I am writing a program that graphs the concentrations of various phytoplankton species in the ocean. It reads in its data from binary files. The files are organized in blocks of latitude vs longitude (xy). Each file contains several of these blocks representing different depths (z). I want to be able to switch the view so the user sees the xz plane. However, this would mean reading in the data differently. I need to read in the first line of each block of data in the file. The code that I have seems like it should do this. Unfortunately, my fread seems to be going haywire. Before the fread line, NZ (the number of depth levels) equals 23... immediately after the fread, it is set to 0. I must be having memory allocation issues, but I'm not sure how to fix them.

    Here is the code for the read function:

    Code:
    void readxz( float arr[], char filename[] ){
     int i;
     FILE* fp;
    
     fp=fopen( filename, "rb" );
     for( i=0; i<NZ; i++){
      fseek( fp, i*NX*NY*4, SEEK_SET );
      fread( &arr[NX*(i-1)], sizeof( arr[0] ), NX, fp ); 
     }
     fclose(fp);
    }
    Any suggestions would be greatly appreciated.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    NZ is not being directly changed in that function. Print the value of NZ at the beginning of that function and at the end. It should be the same.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    &arr[NX*(i-1)]
    That doesn't look good. the index into that array will become negative on i == 0.
    Kurt

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    MacGyver: NZ is definitely being changed during the fread. A printf before that line tells me NZ=23, and afterwards NZ=0.

    ZuK: That seems like it could be the problem. If it's trying to write to a negative index it could be overwriting NZ. I'll definitely play with that.

    Thanks, guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-07-2009, 08:07 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. Parse a program for functions, variables
    By Enu in forum C Programming
    Replies: 2
    Last Post: 02-15-2006, 10:08 AM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 08:01 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21