   Read binary files in C
  I''m working on a binary file reader for an organisation and I have made one in C so far.This project is highly speed orientated as the software has to run real-time with a network card running at speeds of 256kb/s, 512kb/s and 1024kb/s. The data from the network card is already being captured to a file in raw binary format.  My program has to extract the 1s and nuns from the file and display it near real-time.I was thinking of writing it in assembly which I could do.  But I''d like to explore C in more detail first.If there is a faster way of doing this then please tell me, I would greatly appreciate it.This is my code so far:FILE *fp;if((fp=fopen("1.dat","r"))==NULL) {   printf("File open failed");   exit(1);}while(!feof(fp)) {   unsigned char ch;unsigned char pat=1;int num,i;fread(&ch,1,1,fp);for(i=0;i<8;i++)    {   pat=1<That''s the basic code behind my program...  It works perfectly and I''ve tested it''s speed on Windows and Linux machines.  At the moment under Linux it can display around 300k characters per second.  I need it to do around 1024k per second.These are also big files we''re working with here.  The card''s bringing it about 60mb in 1 minute so the software has to keep up.My question is, is there a faster way of extracting the data from the binary file other than fopen(), fread(),etc?Any help with be most appreciated.Thanks   - ]Reaper[
  I/O operations are pretty expensive. Instead of reading 1 unsigned char at a time, read in a lot and then work with the array of them. That might speed things up.   Qui fut tout, et qui ne fut rienInvader''s Realm;  Just a couple of thoughts I had looking at your code.1) If it's binary, why don't you have 'fopen("1.dat", "rb")' instead of 'fopen("1.dat", "r")'?2) I don't know how much this will speed it up, but it should a little. Place ch, pat, num, and i outside of the while loop. They're not needed in there.3) Try: num = (ch & pat);pat= 0 < num;printf("%u", pat); instead of: num = (ch&pat);pat= 1 < num;if(num!=0) printf("1");else printf("0"); -UltimaX-"You wished for a white christmas... Now go shovel your wishes!"   [edited by - UltimaX on July 31, 2003 12:48:38 AM];  Ahh, the bb stuffed up my code..   The for loop is: fread(&ch,1,1,fp);for(i=0;i<8;i++) {pat=1<<i;num=(ch&pat);if(num!=0) printf("1");else printf("0");} [edited by - reapfyre on July 31, 2003 12:49:56 AM];quote:Original post by UltimaX  1) If it''s binary, why don''t you have ''fopen("1.dat", "rb&quot'' instead of ''fopen("1.dat", "r&quot''?Maybe because he''s on a POSIX system, where there is no difference between "binary" and "text" files? On such systems, the ''b'' is simply ignored and can safely be discarded.Back to the original topic. If you are on a POSIX system, you''ll have an implementation of mmap() to map a file to memory. I''m pretty sure Windows has such a function too, but it will likely be named differently.Hope this helps.;  Look into platform native I/O interfaces. They''re faster. Also look into your buffering options and make sure you''re getting maximum throughput for your situation. 