8 Replies - 1446 Views - Last Post: 26 September 2005 - 06:16 AM Rate Topic: -----

#1 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Binary Reads in C

Posted 21 September 2005 - 01:52 PM

Hi

I need to do binary reads from a file. This means I need to read one boolean at a time from a file. Since all files are actually stored as binary, this should be easy. My first problem is that C has no boolean variable type. So the first thing I did is to use the normal declaration for a boolean in C:

typedef enum _boolean { FALSE, TRUE } boolean;

#define FALSE 0
#define TRUE 1



Will that be sufficient?

I am working with very large input files (close to 5mb), so this process will propably take way too long if I'm reading one boolean at a time. So to speed up the process, I guess I need to read from the file into a buffer, and then read from the buffer?

And how do I now do the actual reading? I am thinking something close to the following?

 FILE *fp;
   char buffer[513];
   int  i, ch;

   /* Open file to read line from: */
   if( (stream = fopen( "c:\\a.doc", "rb" )) == NULL )
      exit( 0 );

   /* Read in first 80 characters and place them in "buffer": */
   ch = fgetc( stream );
   for( i=0; (i < 513 ); i++ )
   {
      buffer[i] = (char)ch;
      ch = fgetc( fp );
       printf("\n%d %d",i,ch);
   }

   /* Add null to end string */
   buffer[i] = '\0';
   printf( "%s\n", buffer );
   fclose( fp );



But this doesn't work for binary files...

Please go easy on me, I really don't know C that well. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Binary Reads in C

#2 Nova Dragoon  Icon User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 36
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Re: Binary Reads in C

Posted 21 September 2005 - 02:33 PM

is all your input consisting of is bit values (on or off)

if so the best way to get at each bit would be to access the input 1 byte at a time, which can be accessed via a char.

From here you can do bit shifting, or a mask, to get at the specific bits you want.
Was This Post Helpful? 0
  • +
  • -

#3 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Binary Reads in C

Posted 22 September 2005 - 03:38 AM

Nova Dragoon, on Sep 21 2005, 03:30 PM, said:

is all your input consisting of is bit values (on or off)

if so the best way to get at each bit would be to access the input 1 byte at a time,  which can be accessed via a char.

From here you can do bit shifting,  or a mask,  to get at the specific bits you want.

Yes, all my input will be bit values. And even if they aren't I need to interpret them as bit values. Yes, I thought the shifting would be my best option. But still, the code I give here doesn't work for binary files...
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Binary Reads in C

Posted 22 September 2005 - 05:10 AM

Check here and here for some explanations of reading from and writing to binary files.
Was This Post Helpful? 0
  • +
  • -

#5 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Binary Reads in C

Posted 23 September 2005 - 05:07 AM

Ok, so using the above references, I think I have a grasp on the idea. I am trying to read a block of the data into a buffer in memory, and then return one boolean (i.e. one bit) at a time. However, I am having problems with the pointer. I am trying to get a pointer to point to the start of the buffer, and then move one bit everytime I read a boolean. This is what I have come up with, but I'm not too good with pointers, so I think I may be doing it all wrong.

typedef enum _boolean { FALSE, TRUE } boolean;

#define FALSE 0;
#define TRUE 1;

// Global variables
FILE *fp;  // The file with the random numbers
int N;  // To remember where we are in the file
char buffer[513];  // The buffer with the random numbers
char *ptr;


/* This function simply returns the next boolean from the file.  To speed up the
   process, the numbers are first read into a buffer (in memory) and then sent
   to the necessary test, one boolean at a time.  */

boolean getNextBoolean() {

        int i, ch;
        boolean toReturn;

        if (N == 0) {
                // The buffer is empty, fill it
                ch = fgetc(fp);
                for (i = 0; i < 513; i++) {
                        buffer[i] = (char)ch;
                        ch = fgetc(fp);
                }
                ptr = &buffer[0];
                N = 512*sizeof(char);
        }

        toReturn = *ptr;
        ptr++;
        N--;
        return toReturn;

};

/* Main function.  Takes 2 files as parameters.  The first file is a text file specifying
   all the different test parameters.  The second file is the binary file containing
   the random numbers.  */

int main(int argc, char** argv) {

        if (argc != 3) {
                printf("Usage:  HypothesisTesting TestParameters.dat RandomNumbers.bin\n");
                exit(0);
        }
        printf("Using ");  printf("%s",argv[2]); printf(" as input file.\n");

        // Open file to read from
        if ((fp = fopen(argv[2],"rb")) == NULL) {
                printf("Error opening input file.\n");
                exit(0);
        }
        N = 0;  // Reset the buffer

        // Close the file
        fclose(fp);

};




It gives me all kinds of weird errors...
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Binary Reads in C

Posted 23 September 2005 - 05:17 AM

Do you mean compilation errors, or errors that occur as the program is running? If the former, can you post them? Sorry, I'm not near a compiler at the moment.
Was This Post Helpful? 0
  • +
  • -

#7 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: Binary Reads in C

Posted 24 September 2005 - 07:57 AM

Wait...I may be being stupid (it is early in the morning :)), but do you even call your getNextBoolean function?

If this is intentional, then (to Amadeus) I assume the errors are compilational.

This post has been edited by Videege: 24 September 2005 - 07:58 AM

Was This Post Helpful? 0
  • +
  • -

#8 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Binary Reads in C

Posted 26 September 2005 - 04:39 AM

I was only displaying a portion of my total code. I am using a few other functions to implement statistical tests which all use this function to get bits from an input file. The errors I am getting are compilation errors. I copied the whole thing into a new file and rewrote it to test my program. So here's the code, along with the errors I get.


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "StatisticalFunctions.h"

typedef enum _boolean { FALSE, TRUE } boolean;

#define FALSE 0;
#define TRUE 1;

// Global variables
FILE *fp;  // The file with the random numbers
int N;  // To remember where we are in the file
long fileSize;  // The size of the file, in bits
char buffer[513];  // The buffer with the random numbers
char *ptr;




/* This function simply returns the next boolean from the file.  To speed up the
   process, the numbers are first read into a buffer (in memory) and then sent
   to the necessary test, one boolean at a time.  */

boolean getNextBoolean() {
	
	int i, ch;
	boolean toReturn;
	
	if (N == 0) {
  // The buffer is empty, fill it
  ch = fgetc(fp);
  for (i = 0; i < 513; i++) {
 	 buffer[i] = (char)ch;
 	 ch = fgetc(fp);  
  }
  ptr = &buffer[0];
  N = 512*sizeof(char);
	}
	
	toReturn = *ptr;
	ptr++;
	N--;
	return toReturn;
	
};



/* Main function.  Takes 2 files as parameters.  The first file is a text file specifying
   all the different test parameters.  The second file is the binary file containing
   the random numbers.  */

int main(int argc, char** argv) {

	char c;

	if (argc != 3) {
  printf("Usage:  HypothesisTesting TestParameters.dat RandomNumbers.bin\n");
  exit(0);
	}
	printf("Using ");  printf("%s",argv[2]); printf(" as input file.\n");

	// First open the file to know how long it is
	if ((fp = fopen(argv[2],"r")) == NULL) {
  printf("Error opening input file.\n");
  exit(0);
	}
	
	fileSize = 0;
	while (c != EOF) {
  c = getc(fp);
  fileSize++;
	}
	fclose(fp);

	// Open file to read from
	if ((fp = fopen(argv[2],"rb")) == NULL) {
  printf("Error opening input file.\n");
  exit(0);
	}
	N = 0;  // Reset the buffer

	while (fileSize != 0) {
  if (getNextBoolean() == TRUE) {   // <------------ This is line 85
 	 printf("1");
  } else {
 	 printf("0");
  }
  fileSize--;  // <------------ This is line 90
	}  
	
	// Close the file
	fclose(fp);  // <------------ This is line 94

};   // <------------- This is line 96




The errors I get are

temp.c: In function `main':
temp.c:85: error: syntax error before ';' token
temp.c: At top level:
temp.c:90: error: syntax error before '--' token
temp.c:94: warning: parameter names (without types) in function declaration
temp.c:94: warning: data definition has no type or storage class
temp.c:96: error: syntax error before '}' token

You gotta love how C tells you error before ';' when there is no ';'

Thx for all your help.
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Binary Reads in C

Posted 26 September 2005 - 06:16 AM

I'll have to compile it later, but the first thing that jumps to mind is that you've got a semi-colon after your final closing brace. I expect the problems around line 85 have to do with the return value of the function...try changing the line to:
if (getNextBoolean() == 1)
//or even
if (getNextBoolean())


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1