View the 1's and 0's that make up a file!

Discussion in 'Programming General' started by Govind, Dec 21, 2010.

View the 1's and 0's that make up a file!
  1. Unread #1 - Dec 21, 2010 at 9:23 PM
  2. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    View the 1's and 0's that make up a file!

    Bored and experiencing "programmer's block" with a less trivial project, I've made this little app that prints out the actual 1s and 0s that make up a file you pass as the first command line argument to it :)
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	char buf[1000000];
    	int i;
    	FILE *fp; 
    	short curBit;
    	int size;
    	
    	if(argc!=2)
    	{
    		printf("Invalid usage");
    		return 0;
    	}
    	fp = fopen(argv[1], "rb");
    	if(!fp)
    	{
    		printf("Could not open %s", argv[1]);
    		return 0;
    	}
    	fread(buf, sizeof(buf), 1, fp);
    	
    	size = filelength(fileno(fp));
    	
    	for(i = 0; i < 1000000; i++)
    	{
    		char temp = buf[i];
    		int i2;
    		for(i2 = 0; i2 < 8; i2++)
    		{
    			curBit = ((temp >> 7) & 0x1);
    			printf("%d", curBit);
    			temp = temp << 1;
    		}
    		if((size < 1000000) && (i == size))
    		{
    			break;
    		}
    		
    	}
    	return 0;
    }
     
< Need help editing something | AutoIt script To bring back Free Trade/Wilderness >

Users viewing this thread
1 guest


 
 
Adblock breaks this site