Thread: Read File in Binary format

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    7

    Read File in Binary format

    Hello!

    Problem : I need to get the content from a file and then split it in binary blocks to use in a RSA Encryptation. And that block should use one specific number of bits that were got from the user.


    Question :
    I would like to know how can I read a file(from any format : .txt / .jpg / etc ) get the content and split it in binary blocks (like : 10101001011 )to send to the RSA encryptation function.


    Until now I have been using something like this but without success:

    For get the bits number from the user:
    Code:
    	
    while(1)
    	{
    		printf("Insert one integer between 8 and 128 that is multiple of 8: \n");
    		gmp_scanf("%Zd", bitNumber);
    		x = mpz_get_ui(bitNumber);
    		if( (x % 8) == 0 )
    		{
    			printf("This is one multiple of 8.\n");
    			break;
    		}
    		printf("Error , not 8 multiple , press any key to digit another number.\n");
    	}
    for get the file content:
    Code:
    	if(( fp = fopen(inFile , "rb"))==NULL)
    	{
    		printf("Impossible to open file");
    		exit(1);
    	}
    	fclose(fp);
    How can I get the correct information from this fp?

    Thank you in advance for your help

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    (1) What's `gmp_scanf()` and `mpz_get_ui()`? You'll struggle to get answers to questions involving the use of non-standard libraries, unless someone is familiar with them, which would be a rather poor bet. If these are yours, show us how they are defined, if not, seek help from the particular library's community.
    (2) `fread()` and `fseek()` mostly.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    7
    GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface.
    However this will not interpose with the code in this area.

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Are you sure you need those for getting a value whose upper range requirements can be met by `char`?

    Processing a binary file would look something like this. Not sure if this will compile as I don't have a compiler handy, and there are bound to be bugs, but it should give you a rough idea on how it works.
    Code:
    size_t block_size = x / CHAR_BITS;  // read this many bytes
    FILE *fp = fopen(input_file, "rb");
    if (!fp) {
            fprintf(stderr, "FATAL: Could not open file %s\n", input_file);
            exit(EXIT_FAILURE);
    }
    
    unsigned char *block = malloc(block_size);
    if (!block) {
            fprintf(stderr, "FATAL: Memory allocation failed\n");
            exit(EXIT_FAILURE);
    }
    
    size_t bytes_read;
    while (1) {
            bytes_read = fread(block, 1, block_size, fp);
            // process the block here, it's binary data
            if (bytes_read != block_size) {
                    if (feof(fp))
                            break;  // files has been fully read
                    if (ferror(fp)) {
                            fprintf(stderr, "FATAL: Error reading file %s\n", input_file);
                            // release resources
                            exit(EXIT_FAILURE);
                    }
            }
    }
    // rewind the file with `fseek()` if you intend to do any other processing on it
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    Well, why couldnt you just use c=fgetc(fp); (where c is a char). A char is commonly 8-bits in total depending on the compiler i guess. Now, when you have that character (or all of them) you can extract the number of bits you want thereafter by just combining them in a sequential order.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by c coder View Post
    Well, why couldnt you just use c=fgetc(fp); (where c is a char).
    because fgetc returns an int for a reason. putting return value to the char before checking will prevent you from distinguishing EOF indicating error from valid read of 0xff
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM
  2. Read binary file
    By Ken JS in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2007, 11:12 AM
  3. How to read/write 8xp file format...
    By supertoad in forum Tech Board
    Replies: 2
    Last Post: 04-29-2005, 04:18 AM
  4. Read a binary file
    By Sue Paterniti in forum C Programming
    Replies: 8
    Last Post: 04-29-2002, 02:36 AM