C言語でファイルの一括コピーを実現

多くのフォルダーを含むデータセットを取得し、各フォルダーにコピーするファイルが含まれているとします。(テキストファイルとバイナリファイルを含む)

プログラムのアイデアは3つのステップに分かれています。

1. .txtファイルを作成し、各行にコピーするファイルのパスを保存します

2. .txtファイルを作成し、コピーしたファイルが保存されているパスを各行に保存します

3.ファイルコピー機能を使用してコピーする

batバッチコマンドDIR *。* / B> file name list.txtを使用して、フォルダーの下のすべてのフォルダー名を抽出します。

fprintf関数を使用して、コピーするファイルのパスを作成します。Txtファイル(ファイル名リスト2.txt)、同様にファイルを保存するパス(ファイル名リスト3.txt)を作成します。

#include <stdio.h>
#include <iostream>
using namespace std;
struct add
{
	char a[10];
	char b[10];
}; 
typedef struct add ADD;

int main()
{
	ADD *addbuff = new ADD[436];
	int total = 0; 
	FILE *fpRead = fopen("G://s3c//Data//文件名列表.txt", "r");
	FILE *fpWrite = fopen("G://s3c//Data//文件名列表2.txt", "w");

	if (fpRead != NULL)
	{
		//while (!feof(fpRead))
		for (total = 0; total < 436; total++)
		{
			fscanf(fpRead, "%s ", addbuff[total].a);
			fprintf(fpWrite, "G://s3c//Data//%s//%s.osgb\n", addbuff[total].a, addbuff[total].a);
			cout << addbuff[total].a << endl;
		}
		fclose(fpRead);
		fclose(fpWrite);
	}

	return 0;

} 

ファイルをコピーする

#include <stdio.h>
#include <iostream>
using namespace std;

struct path
{
	char a[10];
};
typedef struct path PATH;

int CopyFile(char* SrcFile, char* DesFile)
{
	FILE* fp, *fw;
	long length;
	long n, cpyfinish;
	char buf[1024];
	if ((fp = fopen(SrcFile, "rb")) == NULL)
	{
		return 1;
	}
	fw = fopen(DesFile, "wb");

	fseek(fp, 0L, SEEK_END);
	length = ftell(fp);
	rewind(fp);
	while (length>0)
	{
		n = fread(buf, 1, 1024, fp);
		cpyfinish = fwrite(buf, 1, n, fw);
		length -= cpyfinish;
	}
	fclose(fw);
	fclose(fp);
	return 0;
}


int main()
{
	PATH *srcpath = new PATH[436];
	PATH *dstpath = new PATH[436];
	int total = 0;
	FILE *fpRead_1 = fopen("G://s3c//Data//文件名列表2.txt", "r");
	FILE *fpRead_2 = fopen("G://s3c//Data//文件名列表3.txt", "r");

		
		for (total = 0; total < 436; total++)
		{
			fscanf(fpRead_1, "%s ", srcpath[total].a);
			fscanf(fpRead_2, "%s ", dstpath[total].a);
			
			cout << srcpath[total].a << endl;
			cout << dstpath[total].a << endl;
			CopyFile(srcpath[total].a, dstpath[total].a);
		}

		return 0;

}

https://blog.csdn.net/qq_32579021/article/details/81738766を参照できます。

おすすめ

転載: blog.csdn.net/Xiao_Xue_Seng/article/details/95345216