Take this file: myfile.txt
for instance.
How do I read the bits (1s and 0s) of this file and write them somewhere else in C to get the exact myfile.txt
Simply put I want to know how to regenerate files by reading and rewriting their 1s and 0s in C.
CodePudding user response:
You are asking how to read an entire file into memory. This is done by calling read
in a loop.
#include <stdio.h>
#include <stdlib.h>
#define CHUNK_SIZE ...
// Returns 0 on success.
// Returns -1 and errno set on error.
int read_file( int fd, char **buf_ptr, size_t *buf_size_ptr )
*buf_ptr = NULL;
*buf_size_ptr = 0;
char *buf = malloc( CHUNK_SIZE );
if ( !buf )
goto ERROR1;
size_t total_bytes_read = 0;
while ( 1 ) {
ssize_t bytes_read = read( fd, buf total_bytes_read, CHUNK_SIZE );
if ( bytes_read == -1 )
goto ERROR2;
if ( bytes_read == 0 )
break;
total_bytes_read = bytes_read;
char *tmp = realloc( buf, total_bytes_read CHUNK_SIZE );
if ( !tmp )
goto ERROR2;
buf = tmp;
}
{
char *tmp = realloc( buf, total_bytes_read );
if ( tmp ) {
buf = tmp;
}
*buf_ptr = buf;
*buf_size_ptr = total_bytes_read ;
return 0;
ERROR2:
free( buf );
ERROR1:
return -1;
}
int main( void ) {
int fd = ...;
char *buf;
size_t buf_size;
if ( read_file( ..., &buf, &buf_size ) == -1 ) {
perror( NULL );
exit( EXIT_FAILURE );
}
// Do something with buf...
free( buf );
return EXIT_SUCCESS;
}
Regenerating the file is simply writing the buffer to disk.
// Returns 0 on success.
// Returns -1 and errno set on error.
int write_file( int fd, const char *buf, size_t buf_size ) {
while ( buf_size > 0 ) {
ssize_t bytes_written = write( fd, buf, buf_size );
if ( bytes_written == -1 )
return -1
buf = byte_written;
buf_size -= byte_written;
}
return 0;
}
CodePudding user response:
All others have clarified that in C (and in any other programming language which is not hardware oriented) you need to cope with bytes as a minimum unit.
If you want to "play with bits", you can read bytes and then extract the single bits from them:
#include <stdio.h>
#include <stdbool.h>
int readbit(FILE *f)
{
static int buffer;
static int n = 0;
if (n == 0) {
buffer = fgetc(f);
if (buffer == EOF) {
return EOF;
}
n = 8;
}
return (buffer >> --n) & 1;
}
int main(void)
{
for (int i = 0, bit; (bit = readbit(stdin)) != EOF; i) {
printf("%d%c", bit, ((i 1) % 8 == 0)*' ');
}
return 0;
}
Here you see a simple function that "reads bits" from a file. How? It reads and buffers a single byte, then it returns a bit of that byte.
The main function uses it to read stdin
and send it in textual binary representation on standard output. If you try it with " a0" it sends to stdout
"00100000 01100001 00110000 ", i.e. the binary representation of those three bytes (the numerical code for space is 32, for a is 97, for 0 is 48).
Try it online on ideone.com.
DISCLAIMER: I'm not suggesting that this is a production ready function, nor that using static variables in functions is a good idea. But it makes everything simpler here. Don't multithread this.