首先我們先來學習一下動態(tài)庫的調用,先找到動態(tài)庫的.dll和.lib文件并將其導入到同源文件相同級別的文件夾下面,然后在添加進其頭文件,并右擊項目處,然后點擊鏈接,鏈接我們的lib文件(一定要是全名稱包括擴展名),然后我們就可以調用動態(tài)庫的函數(shù)了。
Dll是我們具體的函數(shù), lib使我們的函數(shù)描述文件。
-
#include <stdio.h>
-
#include <stdlib.h>
-
/*
-
該代碼是對文件讀寫操作的使用
-
*/
-
#pragma warning(disable:4996)
-
#define MAX_DATA_LEN 4096
-
//進行加密的函數(shù)
-
int FileSymEnc(char * from, char * to);
-
-
void main() {
-
-
char * from = "C:/Users/Administrator/Desktop/結構體的拷貝問題.docx";
-
char * to = "C:/Users/Administrator/Desktop/副本結構體的拷貝問題.docx";
-
//下面調用函數(shù)進行文件的讀寫
-
int re = FileSymEnc(from, to);
-
if (re == 0) {
-
printf("文件讀寫錯誤!");
-
}
-
system("pause");
-
}
-
-
int FileSymEnc(char * from, char * to) {
-
//返回值,以確定執(zhí)行狀態(tài),-0代表失敗,1代表成功
-
int re = 1;
-
//寫了幾個
-
int writtenLen = 0;
-
//準備寫幾個
-
int plainlen = 0;
-
//在堆區(qū)分配4k內存空間
-
unsigned char * buff = malloc(MAX_DATA_LEN);
-
buff = memset(buff, 0, MAX_DATA_LEN);
-
FILE * f = fopen(from, "rb");
-
FILE * t = fopen(to, "wb");
-
if (f == NULL) {
-
printf("打開讀文件錯誤!");
-
goto END;
-
}
-
if (t == NULL) {
-
printf("打開寫文件錯誤!");
-
goto END;
-
}
-
-
while (!feof(f))
-
{
-
-
writtenLen = fread(buff, 1, MAX_DATA_LEN, f);
-
//判讀是否讀到了文件末尾,如果讀到了則跳出循環(huán)
-
if (feof(f))
-
{
-
plainlen = writtenLen;
-
break;
-
}
-
-
if (writtenLen != MAX_DATA_LEN) {
-
//沒有讀取成功
-
printf("文件讀入失??!");
-
re = 0;
-
goto END;
-
}
-
//讀取成功進行文件的寫入
-
writtenLen = fwrite(buff, 1, MAX_DATA_LEN, t);
-
if (writtenLen != MAX_DATA_LEN) {
-
printf("文件寫入失敗!");
-
re = 0;
-
goto END;
-
}
-
}
-
-
//外面處理不到4K的寫入問題,我們本不用進行格外的小數(shù)據(jù)讀寫,主不過這和我們的加密方式相關連得
-
writtenLen = fwrite(buff, 1, plainlen, t);
-
if (writtenLen != plainlen) {
-
printf("文件寫入失敗!");
-
re = 0;
-
goto END;
-
}
-
-
END:
-
//堆分配的內存進行釋放
-
if (buff != NULL) {
-
free(buff);
-
buff = NULL;
-
}
-
//進行文件的關閉操作
-
-
if (f != NULL) {
-
fclose(f);
-
f = NULL;
-
}
-
if (t != NULL) {
-
fclose(t);
-
f = NULL;
-
}
-
return re;
-
}
|