Copying Binary Files - c

I'm trying to figure out how to copy binary files from one place to another .exe's. I can't seem to find any solutions to learn.
I'm using Windows. What's the best way to do it?

What do you mean "best way"? I think this is the most straightforward way ... hopefully that is what you meant :)
fopen the input and output files with binary mode
FILE *exein, *exeout;
exein = fopen("filein.exe", "rb");
if (exein == NULL) {
/* handle error */
perror("file open for reading");
exit(EXIT_FAILURE);
}
exeout = fopen("fileout.exe", "wb");
if (exeout == NULL) {
/* handle error */
perror("file open for writing");
exit(EXIT_FAILURE);
}
fread and fwrite
size_t n, m;
unsigned char buff[8192];
do {
n = fread(buff, 1, sizeof buff, exein);
if (n) m = fwrite(buff, 1, n, exeout);
else m = 0;
} while ((n > 0) && (n == m));
if (m) perror("copy");
and finally close the files
if (fclose(exeout)) perror("close output file");
if (fclose(exein)) perror("close input file");
Have fun!

Windows has a CopyFile API (if you don't mind being platform specific). One thing to be careful of these days is making sure you have permissions to write to the destination area.

Make sure you open the files with the O_BINARY option if you use open() and file descriptors, or with the letter "b" in the mode string if you use fopen(). Note that O_BINARY is not standard except on Windows; however, you can treat it (or define it) as 0 on Unix and all will be fine. If you are using a pure Windows API, then make sure you are doing the equivalent - make sure you specify that the file is treated as a binary file.

You can use this to copy Binary Files:
int get_file_size(char *source)
{
FILE *fichier = fopen(source,"rb");
fseek(fichier,0,SEEK_END);
int size = ftell(fichier);
fseek(fichier,0,SEEK_SET);
fclose(fichier);
return size;
}
void copy(char *source, char *dest)
{
int srcsize = get_file_size(source);
char *data = (char *)malloc(srcsize);
int fsource = open(source,O_RDONLY | O_BINARY);
int fdest = open(dest,O_WRONLY | O_CREAT | O_BINARY);
read(fsource,data,srcsize);
write(fdest,data,srcsize);
close(fsource);
close(fdest);
}

You can use function of 'dos.h' for low-level IO operation.
Following code illustrate use of them. Hope it will helpful
#include<stdio.h>
#include<dos.h>
#include<FCNTL.H>
#include<SYS\STAT.H>
void main()
{
char s_file[100],d_file[100],buf[512];
short char copy='y';
int in_handle,out_handle,flg,len;
printf("\nEnter File Name : ");
fflush(stdin);
gets(s_file);
printf("\nEnter Destination File Name : ");
fflush(stdin);
gets(d_file);
// open file for reading
in_handle=open(s_file,O_RDONLY | O_BINARY);
if(in_handle<0)
{
printf("\nSource File not Found... ");
}
else
{
// open file for writing
out_handle=open(d_file,O_CREAT|O_WRONLY|O_BINARY,S_IWRITE);
while((len=read(in_handle,buf,512))>0)
{
flg=write(out_handle,buf,len);
if(flg==-1)
break;
}
if(flg==-1)
{
printf("Unable to Create");
}
else
{
printf("File Created");
}
}
if(!(in_handle<0))
close(in_handle);
if(!(out_handle<0));
close(out_handle);
}

#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp1,*fp2;
char c;
fp1=fopen("source file","rb");
if(fp1==NULL)
exit(1);
fp2=fopen("destination file","wb");
if(fp2==NULL)
exit(1);
while((c=fgetc(fp1))!=EOF)
fputc(c,fp2);
fclose(fp1);
fclose(fp2);
return 0;
}

http://www.cs.bu.edu/teaching/c/file-io/intro/
#include <ctype.h>
#include <stdio.h>
#define BUFSIZE 1024
int main(int argc, char **argv)
{
charmybuf[BUFSIZE] = { 0 }, *p = NULL;
FILE *ifd = NULL, *ofd = NULL;
ifp = fopen( argv[1], “r” );
ofp = fopen( argv[2], “w” );
assert(ifp!=NULL);
assert(ofp!=NULL);
while ( ( n = fread( mybuf, sizeof(char), BUFSIZE ,ifd) ) > 0 )
{
fwrite(mybuf, sizeof(char),BUFSIZE,ofd);
}
fclose(ifd);
fclose(ofd);
return 0;
}

Related

Why is this function not copying the input file?

I'm just getting started into file I/O and am trying to build a function that will simply copy a file to destination.
This program compiles however an empty file is created and nothing is copied. Any advice?
#include <stdio.h>
int copy_file(char FileSource[], char FileDestination[]) {
char content;
FILE *inputf = fopen(FileSource, "r");
FILE *outputf = fopen(FileDestination, "w");
if (inputf == NULL)
;
printf("Error: File could not be read \n");
return;
while ((content = getc(inputf)) != EOF) putc(content, inputf);
fclose(outputf);
fclose(inputf);
printf("Your file was successfully copied");
return 0;
}
int main() {
char inputname[100];
char outputname[100];
printf("Please enter input file name: \n");
scanf("%s", &inputname);
printf("Please write output file name: \n");
scanf("%s", &outputname);
copy_file(inputname, outputname);
return 0;
}
There are few bugs in the code you mentioned. These two below statement
scanf("%s", &inputname);
scanf("%s", &outputname);
Are wrong as inputname and outputname are char array and array name itself address so you no need to give &inputname to scanf(). For e.g
scanf("%s",inputname);
scanf("%s",outputname);
Also ; at the end of if statement is not serving correct purpose as you expected.
This
if(inputf == NULL);
Should be
if(inputf == NULL){
/*error handling */
}
As pointed by other, getc() returns int not char. From the manual page of getc()
int getc(FILE *stream);
And this
putc(content, inputf);
Change to
putc(content, outputf); /* write the data into outputf */
Your line :
putc(content, inputf);
needs to change to
putc(content, outputf);
This code has a lot of problems:
if(inputf == NULL);
printf("Error: File could not be read \n");
return;
It is the equivalent of
if(inputf == NULL)
{
;
}
printf("Error: File could not be read \n");
return;
You have a stray ; that terminates you if statement, and whitespace doesn't matter much at all with C.
So your if statement does nothing, and your code will always emit the "Error: File could not be read" message and return without doing anything else.
What you probably want:
if(inputf == NULL)
{
printf("Error: File could not be read \n");
return;
}
This is a perfect example of why a lot of C programmers always use braces after if statements. ALWAYS.
There are multiple problems in your code:
content must be declared as int: getc() returns an int with the value of the byte read from the file or the special negative value EOF at end of file. Storing that to a char variable loses information, making the test for EOF either ambiguous (if char is signed) or always false (if char is unsigned by default).
you should pass outputf to putc.
you should return from the copy_file function if fopen fails to open either file.
you should pass the maximum number of characters to read for the filenames
you should check the return value of scanf() to avoid undefined behavior on invalid input.
Here is a corrected version:
#include <stdio.h>
int copy_file(const char *FileSource, const char *FileDestination) {
int content;
FILE *inputf, *outputf;
if ((inputf = fopen(FileSource, "r")) == NULL) {
printf("Error: cannot open input file %s\n", FileSource);
return -1;
}
if ((outputf = fopen(FileDestination, "w")) == NULL) {
printf("Error: cannot open output file %s\n", FileDestination);
fclose(inputf);
return -1;
}
while ((content = getc(inputf)) != EOF)
putc(content, inputf);
fclose(outputf);
fclose(inputf);
printf("Your file was successfully copied");
return 0;
}
int main() {
char inputname[100];
char outputname[100];
printf("Please enter input file name: \n");
if (scanf("%99s", inputname) != 1)
return 1;
printf("Please write output file name: \n");
if (scanf("%99s", &outputname) != 1)
return 1;
copy_file(inputname, outputname);
return 0;
}
Use sendfile() is more simple and efficient for copying file. You can view more detail about sendfile() by man sendfile.
#include <stdio.h>
#include <string.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("Usage: %s <srcfile> <dst_file>\n", argv[0]);
return 1;
}
char *src_file = argv[1];
char *dst_file = argv[2];
int src;
int dst;
ssize_t size;
struct stat stat_buf;
if ((src = open(src_file, O_RDONLY)) < 0)
{
printf("Can not open %s\n", src_file);
return -1;
}
if (fstat(src, &stat_buf) < 0)
{
printf("Can stat %s\n", src_file);
close(src);
return -2;
}
if ((dst = open(dst_file, O_CREAT|O_WRONLY, stat_buf.st_mode)) < 0)
{
printf("Can not open %s\n", dst_file);
return -1;
}
if ((size = sendfile(dst, src, NULL, stat_buf.st_size)) < 0)
{
printf("Fail to copy file, size: %ld\n", size);
}
else
{
printf("Success, size: %ld\n", size);
}
close(src);
close(dst);
return 0;
}

How to unpack a msgpack file?

I am writing msgpack-encoded data to a file. On writing, I am just using the fbuffer of the C API. As in (I striped all error handling for the example):
FILE *fp = fopen(filename, "ab");
msgpack_packer pk;
msgpack_packer_init(pk, fp, msgpack_fbuffer_write);
msgpack_pack_int(pk, 42);
// more data ...
How do I read this file back in? All the example I found assume that the data is in memory, however, my files are up to 5GB, it is not exactly a good idea to hold this in memory completely. Also I do not want to read in chunks myself. After all, I do not know how long the msgpack objects are, so chances are I end up with half an integer in my buffer.
Can msgpack's unpack somehow read from disk directly? Or is there some standard pattern to do this?
You might consider using "msgpack_unpacker" for that instead, which seems to be the official way that MessagePack implements a 'streaming' deserializer. Have a look at msgpack-c/example/c/lib_buffer_unpack.c
Regards, NiteHawk
Okay, I managed to do it.
Here is how to write:
#include <stdlib.h>
#include <msgpack.h>
#include <msgpack/fbuffer.h>
int main(int argc, char **argv) {
if(2 != argc) {
fprintf(stderr, "Call all writeFile <file>");
return;
}
FILE *fp = fopen(argv[1], "ab");
msgpack_packer pk;
msgpack_packer_init(&pk, fp, msgpack_fbuffer_write);
for(int i=0;i<2048;i++) {
msgpack_pack_int(&pk, i);
}
fclose(fp);
}
And this is what the read looks like:
#include <stdlib.h>
#include <msgpack.h>
static const int BUFFERSIZE = 2048;
int main(int argc, char **argv) {
if(2 != argc) {
fprintf(stderr, "Call with readFile <file>");
return 1;
}
char *inbuffer = (char *) malloc(BUFFERSIZE);
if(NULL == inbuffer) {
fprintf(stderr, "Out of memory!");
return 1;
}
FILE *fp = fopen(argv[1], "rb");
size_t off = 0;
size_t read = 0;
msgpack_unpacked unpacked;
msgpack_unpacked_init(&unpacked);
do {
read = fread(inbuffer, sizeof(char), BUFFERSIZE - off, fp);
off = 0;
while(msgpack_unpack_next(&unpacked, inbuffer, read, &off)) {
msgpack_object_print(stdout, unpacked.data);
puts("");
}
memcpy(inbuffer, &(inbuffer[off]), read-off);
off = read - off;
} while(read != 0);
free(inbuffer);
fclose(fp);
msgpack_unpacked_destroy(&unpacked);
return 0;
}
I did not try, but I think it will work with larger objects (arrays, maps etc.) as well.

Unable to read a file and pass into arguments

1) I'm trying to open a file, read the mix data (ints, chars and strings) and store them into args.
1.1) so in the sample.txt is a total of 13 (excluding args[0])
2) Need to read a file from terminal "./myprog.c < sample.txt"
Heres my code and have no idea where i went wrong:
sample.txt:
123 213 110 90 1
hello my friend
boo bleh
a b c
myprog.c:
#include <stdio.h>
int main()
{
int i = 1;
FILE *fstin=fopen(argv[0], "r"); //open the file
if (fstin == NULL) {
puts("Couldn't fopen...");
return -1;
}
//Getting all the inputs from file
while ((fscanf(fstin, "%d", argv[i])) != EOF){
i++;
}
fclose(fstin);
for (i=0; i<10; i++) {
printf("%d\n",argv[i]);
}
return 0;
}
Any help is greatly appreciated!
PS: Would like if anyone could post their complete solution? Will upload unto this post and let everyone have a review of this problem
PPS: Please excuse the poor level of coding as I am a beginner and completely new to C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int ac, char *av[]){
int i, argc=0;
char **argv=NULL, data[16];
FILE *fstin = stdin;
if(ac == 2){
if(NULL==(fstin = fopen(av[1], "r"))){
puts("Couldn't fopen...");
return -1;
}
}
while (1==fscanf(fstin, "%15s", data)){
argv = realloc(argv, (argc+1)*sizeof(char*));
argv[argc] = malloc(strlen(data)+1);
strcpy(argv[argc++], data);
}
if(ac == 2)
fclose(fstin);
for (i=0; i<argc; ++i) {
printf("%s\n", argv[i]);
}
//deallocate
return 0;
}
You are making mistake at 2nd point where you divert your file to other file which is wrong. Actually you need to first compile and need to make executable.
gcc -o my_prog ./myprog.c -Wall
You need to execute this program as below to read file from c program:
./my_prog ./sample.txt
As you are new to C programming first go to man pages related to file operations.
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
//If command line argument is not inserted then stop operation
if (2 != argc) {
printf("Invalid number of arguments : %d\n", argc);
return -1;
}
int size = 0, ret = 0;
char *data = NULL;
FILE *fp = NULL;
//Open file in read mode given from command line argument
if (NULL != (fp = fopen(argv[1], "r")))
{
//Find size of file
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
//if file is empty no need to read it.
if (size > 0)
{
//Data pointer which contains file information
data = (char *) calloc(sizeof(char), size);
if (NULL != data)
{
//Read whole file in one statement
fread(data, sizeof(char), size, fp);
printf("File %s is readed successfully\n", argv[1]);
printf("Data:\n");
printf("%s\n", data);
free(data); data = NULL;
}
else
{
perror("memory allocation failed\n");
ret = -1;
}
}
else
{
printf("File %s is empty\n", argv[1]);
}
fclose(fp); fp = NULL;
}
else
{
perror("File open failed\n");
ret = -1;
}
return ret;
}
Now Test it on your setup and if any query please post comments.

Encryption code doesn't write back to file

I compile the code then run it but it returns "Error in writing encrypted data to file. So I assume the issue is somewhere in fwrite.
But I cannot pinpoint it.
I need some help here if you could explain the problem that would be very helpful thanks a lot
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int encrypt_data(FILE *);
int main(void)
{
FILE *fp;
int return_code;
printf("Please enter the file to be encrypted: ");
char filename[200];
scanf("%s", filename);
fp=fopen(filename,"r");
return_code = encrypt_data(fp);
return 0;
}
int encrypt_data(FILE *disk_fp)
{
int i;
unsigned long int file_size;
int key_length;
char *file_buff = NULL;
char key[] = "12`3-vk0fn";
key_length = strlen(key);
fseek(disk_fp, 0, SEEK_END);
file_size = ftell(disk_fp);
rewind(disk_fp);
file_buff = malloc(file_size);
if( fread(file_buff, file_size, 1, disk_fp) != 1)
{
printf("Error in reading file\n");
return -1;
}
for( i=0; i<file_size; i++)
{
file_buff[i] = file_buff[i] ^ key[i%key_length];
}
rewind(disk_fp);
if( fwrite(file_buff, file_size, 1, disk_fp) != 1)
{
printf("Error in writing encrypted data to file\n");
return -1;
}
free(file_buff);
fclose(disk_fp);
return 0;
}
You are opening file with "r" mode which means readonly. Then you try write something to it. By the way you don't check that you open file without errors and don't close it when fwrite/fread failed.

Need to encrypt a file I have it all written but error in reading file

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int encrypt_data(FILE *);
int main(void)
{
FILE *fp;
int return_code;
printf("Please enter the file to be encrypted: ");
char filename[200];
fgets(filename, 200, stdin);
fp=fopen("filename","w+");
return_code = encrypt_data(fp);
return 0;
}
int encrypt_data(FILE *disk_fp)
{
int i;
unsigned long int file_size;
int key_length;
char *file_buff = NULL;
char key[] = "12`3-vk0fn";
key_length = strlen(key);
fseek(disk_fp, 0, SEEK_END);
file_size = ftell(disk_fp);
rewind(disk_fp);
file_buff = malloc(file_size);
if( fread(file_buff, file_size, 1, disk_fp) != 1)
{
printf("Error in reading file\n");
return -1;
}
for( i=0; i<file_size; i++)
{
file_buff[i] = file_buff[i] ^ key[i%key_length];
}
rewind(disk_fp);
if( fwrite(file_buff, file_size, 1, disk_fp) != 1)
{
printf("Error in writing encrypted data to file\n");
return -1;
}
free(file_buff);
fclose(disk_fp);
return 0;
}
The file I am trying to encrypt is "encrypt.txt" it is just a sentence of nonsense but when I compile this code and then ./a.out it asks me for the file name i enter encrypt.txt I thought it might just want the name but either way it returns "Error in reading file".
I think my fgets() fopen() is the culprit but I am very very lost in how to fix it.
If you could find the error in the code and then explain why it was messing things up it would help me in the future. Thanks a lot.
Remove double quotes of the filename variable. And put it as
fp=fopen(filename,"w+");
maybe I am wrong but with close inspection I thought you better use
scanf("%s", filename);
Instead of the fgets() function. You are reading text from user and not from file at that moment after all.

Resources