hi i can copy text based files to other files but when it comes to images i cant copy them it gives me a distorted image.

printf("please give your archive a name:\n");
scanf("%s", &archive_name);
	
create_pointer = fopen( archive_name, "wb");	
printf("please type in the path for the file you want to archive\n");
scanf("%s", &original_file);

open_pointer = fopen( original_file, "rb");			
original_pointer=fopen(original_file,"rb");	

						
copy_pointer = fopen( archive_name,"wb")

Dani AI

Generated

Binary files like JPEGs must be copied as raw bytes — any text-mode I/O or string-only routines will corrupt them. As noted, the thread is missing the actual copy loop; has opened files but needs a binary-safe read/write loop, safe filename input, and error checks. Common mistakes are using text functions on binary data, accidentally truncating the source by reusing the same filename for output, or reading filenames unsafely (buffer overflows or wrong pointer use).

A minimal, robust approach is to read with fread and write with fwrite in a loop, checking return values and errors. The following example demonstrates the pattern to use:

#include <stdio.h>

int copy_file(const char *src, const char *dst) {
    FILE *in = fopen(src, "rb");
    if (!in) return -1;
    FILE *out = fopen(dst, "wb");
    if (!out) { fclose(in); return -2; }
    unsigned char buf[8192];
    size_t n;
    while ((n = fread(buf, 1, sizeof buf, in)) > 0) {
        if (fwrite(buf, 1, n, out) != n) { fclose(in); fclose(out); return -3; }
    }
    fclose(in);
    fclose(out);
    return 0;
}

Troubleshooting checklist: verify fopen succeeded, ensure source and destination are different, avoid fgets/scanf pitfalls when reading filenames (use bounded reads), check ferror if corruption persists, and on Windows be sure to use binary modes. To confirm success, compare sizes or checksums of source and copy.

Recommended Answers

All 2 Replies

Where's the code that copies the data?

thats all i have for the copying, the create_pointer lets the user create the file then the copy_pointer is the file that the user selected i hope that makes sense but it does work just not with jpeg it can copy text files only

copy_pointer = fopen( archive_name,"wb")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.