Thread: binary file to an array

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    binary file to an array

    I'm trying to take a binary file and pass it into an array. The problem is, that the program will only read half of the values in the file and report them all as zero. Any help would be great.

    Code:
    double input (double array[])
    {
    	int dblsize = sizeof(double), count;
    	FILE *ifp;
    	ifp = fopen(INFILE, "rb");
    	if (ifp == NULL)
    	{
    		printf ("Error opening file.\n");
    	}
    	else
    	{
    		count = fread(array, dblsize, MAXDATA , ifp);
    	}
    	return count;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Did you create the file on the same computer? Binary files are highly non-portable. This simple test works fine for me:
    Code:
    itsme@itsme:~/C$ cat dblbin.c
    #include <stdio.h>
    
    int main(void)
    {
      double array[] = { 3.4, 7.2, 95.7, 104.9, 0.13 };
      FILE *fp;
      int count;
      int i;
    
      if(!(fp = fopen("dblbin.test", "wb")))
      {
        puts("Couldn't open dblbin.test for writing!");
        return 1;
      }
    
      count = fwrite(array, sizeof(double), 5, fp);
      fclose(fp);
      printf("Wrote %d items.\n", count);
    
      if(!(fp = fopen("dblbin.test", "rb")))
      {
        puts("Couldn't open dblbin.test for reading!");
        return 1;
      }
    
      count = fread(array, sizeof(double), 5, fp);
      fclose(fp);
      printf("Read %d items.\n", count);
    
      for(i = 0;i < count;++i)
        printf("%f ", array[i]);
      putchar('\n');
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./dblbin
    Wrote 5 items.
    Read 5 items.
    3.400000 7.200000 95.700000 104.900000 0.130000
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    I did creat the file on the same computer, and its saved under notepad, maybe I wrote the file wrong. If I bring up the file in notepad should it be a series of ones and zeros? If so, then the file has a serious problem.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    First of all, if you saved a binary file under notepad, then you are going to have problems with the file when you try and open/read it in your program.
    If I bring up the file in notepad should it be a series of ones and zeros?
    No, it should be random gibberish.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    Ok, then that explains the problem. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Write to binary file from 2D array
    By wbaker01 in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2006, 06:16 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM