FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Read file as binary

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
jb



Joined: 01 Mar 2007
Posts: 1

PostPosted: Thu Mar 01, 2007 4:57 pm    Post subject: Read file as binary Reply with quote

I need to read in a file, but not as text. I need to process it as binary.
bit[] bFileContent = cast(bit[])read(sFileName); doesn't seem to work as I anticipated, it just reads in characters and declares them all true.

How could I either read in a file as a binary array or break up what I read into a binary array?

Thanks.
Back to top
View user's profile Send private message
pqnelson



Joined: 03 Jun 2007
Posts: 13
Location: Davis

PostPosted: Wed Oct 31, 2007 5:24 pm    Post subject: Reply with quote

Try using the std.c.stdio module's fread() method, I think it can be used to read in binary stuff. Ultimately this is what I would do:

Code:
import std.c.stdio;

int main(char[][] args)
{
     char[] path = "/home/pqnelson/myBinaryFile"; // the path using Unix path style
     byte[] data; //array where we will store the data read in from the file
     FILE *file; // the file descriptor pointer
     file = open(path, "rb"); // opens the file with flags indicating you want to read and it is binary
     uint nBytesRead = 25; //number bytes to read in
     if(fread(cast(void*)data, 1, nBytesRead, file) <= 0)
     {
          throw new Exception("The computer hates me! It won't read in the file or more than likely perhaps the file doesn't exist?");
     }
     // do what you want with the binary stuff...
}


Unfortunately you will have to be using bitwise operators to manipulate the bits themselves, but that's life.

It's more efficient than using a bitarray, I think, because if you have n bits you would be using an n byte sized array. That's just inefficient. Wink
Back to top
View user's profile Send private message AIM Address
Linker



Joined: 06 Nov 2006
Posts: 29
Location: Almaty, Kazakhstan

PostPosted: Wed Nov 14, 2007 11:39 am    Post subject: Reply with quote

What do you mean binary? I suppose you don't want line-endings interpreted?

Then you should do something along the lines of:
Code:

// ...some code...
auto contents = cast( ubyte[] )std.file.read( filename );
// ...more code...


Bit is just a typedef for bool, so casting to bit[] doesn't make much sense here.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group