SoFunction
Updated on 2025-04-14

How to copy image files in C language

In C language, direct processing of copying image files (such as JPEG, PNG, etc.) usually involves file I/O operations. These image files are binary files, so they need to read and write files in binary mode.

Image file copy code

#include <>
#include<>

int main(){
	FILE *fp1,*fp2;
	char buf[100];
	int size;
	fp1 = fopen("C:\\Users\\87417\\Desktop\\error_404.jpg","rb");
	fp2 = fopen("C:\\Users\\87417\\Desktop\\error_500.jpg","wb");
	if(fp1 == NULL){
		printf("can't open this file1.\n");
		return 0;
	}
	if(fp2 == NULL){
		printf("can't open this file2.\n");
		return 0;
	}
	while((size=fread(buf,1,100,fp1))>0){
		fwrite(buf,1,size,fp2);
	}
	fclose(fp1);
	fclose(fp2);
	return 0;
}

Use the fopen function to open the file in binary mode. rb means reading a binary file, and wb means writing to a binary file.

fread function

The fread function reads data to the file in binary mode.

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

ptr: A pointer to a memory buffer that stores data read from a file.

size: The size in bytes of each data item to be read.

nmemb: The number of data items to be read.

stream: A pointer to a FILE object, indicating the file stream from which data is to be read.

The fread function returns the number of data items successfully read, which may be less than nmemb if so many data items cannot be read before reaching the end of the file. If a read error occurs or the end of the file is reached, the ferror or feof function returns a non-zero value.

fwrite function

The fwrite function writes data to a file in binary mode.

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

ptr: A pointer to the memory buffer containing the data to be written to the file.

size: The size in bytes of each data item to be written.

nmemb: The number of data items to be written.

stream: A pointer to a FILE object, indicating the file stream to be written to the data.

The fwrite function returns the number of successfully written data items. If the return value is less than nmemb, a write error may occur. You can use the ferror function to check whether an error has occurred.

Method supplement

In C language, there is no function to directly copy pictures, but some third-party libraries, such as OpenCV, can be used to realize the function of copying pictures.

Here is an example of copying images using OpenCV:

First, you need to install the OpenCV library. On Linux, you can use a package manager to install, for example on Ubuntu, you can use the following command:

sudo apt-get install libopencv-dev

Then, you can use the following code to copy the image:

#include &lt;opencv2/&gt;
#include &lt;&gt;
 
int main() {
    // Load the original image    cv::Mat original_image = cv::imread("");
    if (original_image.empty()) {
        fprintf(stderr, "Cannot load image\n");
        return -1;
    }
 
    // Copy the picture    cv::Mat copied_image = original_image.clone();
 
    // Save the copied image    cv::imwrite("", copied_image);
 
    return 0;
}

In this example, we used OpenCVimreadFunction to load the original image and then usecloneFunction to create a copy of the image. Finally, we useimwriteFunction to save this copy.

Note that this is just a basic example, you may need to add error checking and handling code when actually using it.

This is the article about how to copy image files in C language. For more related contents of copying image files in C language, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!