日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言文件操作函数的编写

發(fā)布時間:2024/9/27 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言文件操作函数的编写 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、? 編寫文件操作的頭文件

/**************************************************************************

Copyright(C)??? :2014-08-5 toto

Filename?????? :file.h

Author????????? :涂作權

Version???????? :V1.1

Date??????????? :2014-08-05

Description???? :文件操作的頭文件

Others????????? :參考了原來的多線程操作和單線程操作當當網數(shù)據(jù)

FunctionList?? :

?

History:修改歷史記錄列表,每條修改記錄應該包括修改日期

1.Date:

Author:

Modification:

**************************************************************************/

/*將安全檢查警報設置為失效,必須放在第一行*/

#pragma warning(disable:4996)

#ifndef _FILE_H_

#define _FILE_H_

?

#include <stdio.h>??? //注意文件操作相關函數(shù)的頭文件是stdio.h

#include<stdlib.h>??? //為了使用system函數(shù)

#include <string.h>?? //字符串操作所用到的頭文件

#include<Windows.h>?? //使用休眠函數(shù)時用到的頭文件

#include <crtdbg.h>?? //做內存泄露檢測所需的頭文件

?

/*************************************************************

Function???????? :getFileRow

Description????? :獲得文件的中記錄的行數(shù)

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

path???????????? :表示的是文件的路徑

Output?????????? :輸出的信息為空

Return?????????? :返回文件中的行數(shù)

Others?????????? :其它說明

*************************************************************/

int getFileRow(const char *path);

?

/*************************************************************

Function???????? :main_view

Description????? :查看菜單信息

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

Output?????????? :輸出的信息為空

Return?????????? :返回值為void

Others?????????? :其它說明

*************************************************************/

void main_view();

?

/*************************************************************

Function???????? :getFileSize

Description????? :獲得文件的大小(字節(jié)數(shù))

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

path???????????? :表示的文件路徑

Output?????????? :輸出的信息為空

Return?????????? :文件的字節(jié)大小

Others?????????? :其它說明

*************************************************************/

int getFileSize(const char *path);

?

/*************************************************************

Function???????? :loadFile

Description????? :加載文件

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated ???:被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

path???????????? :表示的是文件路徑

rowSize????????? :表示的是文件中信息由多少行

Output?????????? :輸出的信息為空

Return?????????? :返回值為int,如果加載成功返回1,如果加載失敗返回0

Others?????????? :其它說明

*************************************************************/

int loadFile(const char *fPath, const int rowSize);

?

/*************************************************************

Function???????? :findStringByKeyword

Description????? :通過關鍵字的方式進行查找

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

keyword????????? :通過關鍵字查找信息

rowSize????????? :表示的是文件中信息由多少行

Output?????????? :輸出的信息為空

Return?????????? :如果查找失敗返回0,查找成功返回1

Others?????????? :其它說明

*************************************************************/

int findStringByKeyword(const char *keyword, const int rowSize);

?

/*************************************************************

Function???????? :freeMemory

Description????? :釋放內存空間

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pp?????????????? :存儲文件信息的二重指針

rowSize????????? :表示的行大小

Output?????????? :輸出的信息為空

Return?????????? :返回值為void

Others?????????? :其它說明

*************************************************************/

void freeMemory(char **pp, const int rowSize);

?

/*************************************************************

Function???????? :writeStr2File

Description????? :將字符串拼接的方式寫入文件

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pFilePath??????? :要寫入的文件路徑

pStr???????????? :要寫入的字符串

mode???????????? :表示寫入的模式,0:表示直接追擊?? 1:表示換行追加

Output?????????? :輸出的信息為空

Return?????????? :如果寫入成功返回1,如果寫入失敗返回0

Others??? ???????:其它說明

*************************************************************/

int writeStr2File(const char *pFilePath, const char *pStr, int mode);

?

/*************************************************************

Function???????? :copy()

Description????? :將一個文件拷貝到另外一個文件

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pDestFile??????? :要寫入的文件

pOriginalFile??? :原始文件

mode???????????? :表示寫入的模式,0:表示直接追擊?? 1:表示換行追加

Output??????????:輸出的信息為空

Return?????????? :如果寫入成功返回1,如果寫入失敗返回0

Others?????????? :其它說明

*************************************************************/

int copy(const char *pDestFile, const char *pOriginalFile);

?

/*************************************************************

Function???????? :encryptFileByChar()

Description????? :將文件按照ch進行加密,并將文件輸出到指定文件中

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pDestFile??????? :要寫入的文件

pOriginalFile??? :原始文件

ch?????????????? :用于加密的字符

Output?????????? :輸出的信息為空

Return?????????? :如果寫入成功返回1,如果寫入失敗返回0

Others?????????? :其它說明

*************************************************************/

int encryptFileByChar(const char *pDestFile,

??? const char *pOriginalFile, const char ch);

?

/*************************************************************

Function???????? :deCodeFileByChar()

Description????? :通過ch進行解密文件,并將文件輸出到指定的文件中

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pDestFile??????? :要寫入的文件

pOriginalFile??? :原始文件

ch?????????????? :用于解密的字符

Output?????????? :輸出的信息為空

Return?????????? :如果寫入成功返回1,如果寫入失敗返回0

Others?????????? :其它說明

*************************************************************/

int deCodeFileByChar(const char *pDestFile,

??? const char *pOriginalFile, const char ch);

?

/*************************************************************

Function???????? :stringEncrypt()

Description????? :將字符串變成字符串加密后的字符串

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pwdStr?????????? :將字符串變成加密后的字符串

str????????????? :要加密的字符串

Output?????????? :輸出的信息為空

Return?????????? :返回加密后的字符串地址

Others?????????? :其它說明

*************************************************************/

char * str2EncryptString(char *pwdStr, char *str);

?

/*************************************************************

Function???????? :str2DecodedString()

Description???? ?:將字符串變成解密之后的文件

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pwdStr?????????? :將字符串變成加密后的字符串

str????????????? :要解密的字符串

Output?????????? :輸出的信息為空

Return?????? ????:返回解密后的字符串地址

Others?????????? :其它說明

*************************************************************/

char * str2DecodedString(char *pwdStr, char *str);

?

/*************************************************************

Function???????? :encryptAndDecodeFileByString()

Description????? :通過字符串str來加解密文件

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pDestFile??????? :要寫入的文件

pOriginalFile??? :原始文件

pwdString??????? :用于加解密的字符

Output?????????? :輸出的信息為空

Return?????????? :如果寫入成功返回1,如果寫入失敗返回0

Others?????????? :其它說明

*************************************************************/

int encryptAndDecodeFileByString(const char *pDestFile,

??? const char *pOriginalFile, const char* pwdString);

?

?

#endif

?

2.文件操作的實現(xiàn)函數(shù)

?

#define _CRT_SECURE_NO_WARNINGS

#include "file.h"

?

char **pp = NULL;?? //存儲指針數(shù)組的地址

int flag = 0;

?

/*************************************************************

Function???????? :getFileRow

Description????? :獲得文件的中記錄的行數(shù)

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

path???????????? :表示的是文件的路徑

Output?????????? :輸出的信息為空

Return?????????? :返回文件中的行數(shù)

Others?????????? :其它說明

*************************************************************/

int getFileRow(const char *path)

{

??? FILE *pf;

??? pf = fopen(path, "r");

??? if (pf == NULL)

??? {

??????? //代表獲取失敗

??????? fclose(pf);

??????? return -1;

??? }

??? else

??? {

??????? int i = 0;

??????? //是否到文件末尾,如果到了返回1,沒有到的時候返回0

??????? while (!feof(pf))

??????? {

??????????? char str[275];

??????????? //讀取一行

??????????? fgets(str, 275, pf);

??????????? i++;

??????? }

??????? fclose(pf);

??????? return i;

??? }

}

?

/*************************************************************

Function???????? :main_view

Description????? :查看菜單信息

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy???? ???:調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

Output?????????? :輸出的信息為空

Return?????????? :返回值為void

Others?????????? :其它說明

*************************************************************/

void main_view()

{

??? system("cls");

??? printf("\n******************當當用戶信息查詢系統(tǒng)*******************\n");

??? printf("\t1.載入數(shù)據(jù)至內存(-l)\n");

??? printf("\t2.查找字符串(-s)\n");

??? printf("\t3.釋放內存(-f)\n");

??? printf("\n*************************0.退出**************************\n");

}

?

/*************************************************************

Function???????? :getFileSize

Description????? :獲得文件的大小(字節(jié)數(shù))

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

path???????????? :表示的文件路徑

Output?????????? :輸出的信息為空

Return?????????? :文件的字節(jié)大小

Others?????????? :其它說明

*************************************************************/

int getFileSize(const char *path)

{

??? //定義一個文件指針

??? FILE *pf = NULL;

??? pf = fopen(path, "r");

??? if (pf == NULL)

??? {

??????? //代表獲取文件失敗

??????? fclose(pf);

??????? return -1;

??? }

??? else

??? {

??????? //fseek():Moves thefile pointer to a specified location.

??????? fseek(pf, 0, SEEK_END);

??????? //ftell( FILE*stream ):文件開頭到當前位置有多少個字節(jié)

??????? int num = ftell(pf);

??????? //關閉文件

??????? fclose(pf);

??????? return num;

??? }

}

?

/*************************************************************

Function???????? :loadFile

Description????? :加載文件

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

path???????????? :表示的是文件路徑

rowSize????????? :表示的是文件中信息由多少行

Output?????????? :輸出的信息為空

Return?????????? :返回值為int,如果加載成功返回1,如果加載失敗返回0

Others?????????? :其它說明

*************************************************************/

int loadFile(const char *fPath, const int rowSize)

{

??? printf("加載文件后,請稍后....");

??? FILE *pf;

??? pp = (char **)malloc(sizeof(char *)* rowSize);

??? pf = fopen(fPath, "r");

??? if (pf == NULL)

??? {

??????? printf("對不起,加載文件失敗!");

??????? fclose(pf);

??????? return 0;

??? }

??? else

??? {

??????? int i;

??????? for (i = 0; i < rowSize;i++)

??????? {

??????????? //讀取字符串的緩沖區(qū)

??????????? char str[275] = { 0 };

??????????? //*fgets(char*string,int n,FILE *stream);

??????????? //從文件中逐行讀取字符串

??????????? fgets(str,sizeof(str) ,pf);

??????????? //獲取要分配的字符串長度,最后加一是因為'\0'

??????????? int strlength = strlen(str) + 1;

??????????? //分配內存

??????????? char *px = (char *)malloc(sizeof(char) * strlength);

??????????? //拷貝字符串

??????????? strcpy(px, str);

??????????? //設定最后一個字符串'\0'

??????????? px[strlength - 1] = '\0';

??????????? //存儲字符串的首地址到指針數(shù)組

??????????? pp[i] = px;

??????? }

??? }

??? return 1;

}

?

/*************************************************************

Function???????? :findStringByKeyword

Description????? :通過關鍵字的方式進行查找

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

keyword????????? :通過關鍵字查找信息

rowSize????????? :表示的是文件中信息由多少行

Output?????????? :輸出的信息為空

Return?????????? :如果查找失敗返回0,查找成功返回1

Others?????????? :其它說明

*************************************************************/

int findStringByKeyword(const char *keyword, const int rowSize)

{

??? if (pp == NULL)

??? {

??????? printf("對不起,您還沒有加載文件,請您先加載文件\n");

??????? return 0;

??? }

??? else

??? {

??????? int i;

??????? for (i = 0; i < rowSize;i++)

??????? {

??????????? //遍歷所有的指針數(shù)組的地址,字符串查找

??????????? char *pTemp = strstr(pp[i],keyword);

??????????? if (pTemp != NULL)

??????????? {

??????????????? printf("\n%s",pp[i]);

??????????? }

??????? }

??? }

??? return 1;

}

?

/*************************************************************

Function???????? :freeMemory

Description????? :釋放內存空間

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pp?????????????? :存儲文件信息的二重指針

rowSize????????? :表示的行大小

Output?????????? :輸出的信息為空

Return?????????? :返回值為void

Others?????????? :其它說明

*************************************************************/

void freeMemory(char **pp, const int rowSize)

{

??? int i;

??? for (i = 0; i < rowSize;i++)

??? {

??????? free(pp[i]);

??? }

??? free(pp);

??? flag = 0;

}

?

/*************************************************************

Function???????? :writeStr2File

Description????? :將字符串拼接的方式寫入文件

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pFilePath??????? :要寫入的文件路徑

pStr???????????? :要寫入的字符串

mode???????????? :表示寫入的模式,0:表示直接追擊?? 1:表示換行追加

Output?????????? :輸出的信息為空

Return?????????? :如果寫入成功返回1,如果寫入失敗返回0

Others?????????? :其它說明

*************************************************************/

int writeStr2File(const char *pFilePath, const char *pStr, int mode)

{

??? FILE *pf;

??? //a+表示以追加的方式寫文件,如果

??? pf = fopen(pFilePath, "a+");

??? if (pf == NULL)

??? {

??????? //表示文件打開失敗

??????? fclose(pf);

??????? return 0;

??? }

??? else

??? {

??????? if (mode)

??????? {

??????????? fputs(pStr, pf);

??????????? //通過加入'\r'的方式換行,如果隔行輸出使用'\r\n'

??????????? fprintf(pf, "\r");

??????? }

??????? else

??????? {

??????????? fprintf(pf, pStr);

??????? }

??????? fclose(pf);

??? }

??? //寫入成功返回1

??? return 1;

}

?

/*************************************************************

Function???????? :copy()

Description????? :將一個文件拷貝到另外一個文件

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pDestFile??????? :要寫入的文件

pOriginalFile??? :原始文件

mode???????????? :表示寫入的模式,0:表示直接追擊?? 1:表示換行追加

Output?????????? :輸出的信息為空

Return?????????? :如果寫入成功返回1,如果寫入失敗返回0

Others?????????? :其它說明

*************************************************************/

int copy(const char *pDestFile, const char *pOriginalFile)

{

??? FILE *pfr, *pfw;

??? //以代開二進制文件的方式打開

??? pfr = fopen(pOriginalFile, "r");

??? //要寫入的文件

??? pfw = fopen(pDestFile, "w");

??? if (pfr == NULL || pfw == NULL)

??? {

??????? fclose(pfr);

??????? fclose(pfw);

??????? return 0;

??? }

??? else

??? {

??????? int length = getFileSize(pOriginalFile);

??????? //分配內存,讀取文件

??????? char *p = (char *)malloc(length * sizeof(char));

??????? //讀取文件到內存

??????? fread(p,sizeof(char),length,pfr);

??????? //寫入文件

??????? fwrite(p,sizeof(char),length,pfw);

?

??????? //關閉文件

??????? fclose(pfr);

??????? fclose(pfw);

??? }

??? return 1;

}

?

/*************************************************************

Function???????? :encryptFileByChar()

Description????? :將文件按照ch進行加密,并將文件輸出到指定文件中

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pDestFile??????? :要寫入的文件

pOriginalFile??? :原始文件

ch?????????????? :用于加密的字符

Output?????????? :輸出的信息為空

Return?????????? :如果寫入成功返回1,如果寫入失敗返回0

Others?????????? :其它說明

*************************************************************/

int encryptFileByChar(const char *pDestFile,

??? const char *pOriginalFile, const char ch)

{

??? FILE *pfr, *pfw;

??? //以讀的方式寫入文件

??? pfr = fopen(pOriginalFile, "r");

??? //以寫的方式寫入文件中

??? pfw = fopen(pDestFile, "w");

??? if (pfr == NULL || pfw == NULL)

??? {

??????? //表示加載失敗

??????? fclose(pfr);

??????? fclose(pfw);

??????? return 0;

??? }

??? else

??? {

??????? //讀取原始文件中的大小(通過這種方式獲得字節(jié)數(shù)大小)

??????? int length = getFileSize(pOriginalFile);

??????? //分配內存,讀取文件

??????? char *p = (char *)malloc(sizeof(char) * length);

??????? //讀取文件到內存中

??????? fread(p,sizeof(char),length,pfr);

??????? int i;

??????? for (i = 0; i < length;i++)

??????? {

??????????? //加密方法是,于指定字符串進行異或操作

??????????? p[i] ^= ch;

??????? }

??????? //寫入文件

??????? fwrite(p,sizeof(char),length,pfw);

??????? fclose(pfr);

??????? fclose(pfw);

??? }

??? return 1;

}

?

/*************************************************************

Function???????? :deCodeFileByChar()

Description????? :通過ch進行解密文件,并將文件輸出到指定的文件中

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pDestFile??????? :要寫入的文件

pOriginalFile??? :原始文件

ch?????????????? :用于解密的字符

Output?????????? :輸出的信息為空

Return?????????? :如果寫入成功返回1,如果寫入失敗返回0

Others?????????? :其它說明

*************************************************************/

int deCodeFileByChar(const char *pDestFile,

??? const char *pOriginalFile, const char ch)

{

??? FILE *pfr, *pfw;

??? pfr = fopen(pOriginalFile, "r");

??? pfw = fopen(pDestFile,"w");

??? if (pfr == NULL || pfw == NULL)

??? {

??????? fclose(pfr);

??????? fclose(pfw);

??????? //如果是成功,則

??????? return 0;

??? }

??? else

??? {

??????? //讀取原始文件的大小

??????? int length = getFileSize(pOriginalFile);

??????? //分配內存,讀取文件

??????? char *p = (char *) malloc(sizeof(char) * length);

??????? //讀取文件到內存中

??????? fread(p, sizeof(char), length, pfr);

??????? int i;

??????? for (i = 0; i < length; i++)

??????? {

??????????? //解密方法是,于指定字符串進行異或操作

??????????? p[i] ^= ch;

??????? }

??????? fwrite(p, sizeof(char), length, pfw);

??????? //關閉文件

??????? fclose(pfr);

??????? fclose(pfw);

??? }

?

??? return 1;

}

?

/*************************************************************

Function???????? :stringEncrypt()

Description????? :將字符串變成字符串加密后的字符串

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pwdStr?????????? :將字符串變成加密后的字符串

str????????????? :要加密的字符串

Output?????????? :輸出的信息為空

Return?????????? :返回加密后的字符串地址

Others?????????? :其它說明

*************************************************************/

char * str2EncryptString(char *pwdStr, char *str)

{

??? //獲取加密長度

??? int pwdStrLength = strlen(pwdStr);

??? //獲取字符串的長度

??? int strLength = strlen(str);

??? if (strLength % pwdStrLength)

??? {

??????? int times = strLength / pwdStrLength;

??????? int i, j;

??????? //循環(huán)次數(shù)

??????? for (i = 0; i < times; i++)

??????? {

??????????? for (j = 0; j < pwdStrLength;j++)

??????????? {

??????????????? str[pwdStrLength * i + j] ^= pwdStr[j];

??????????? }

??????? }

??? }

??? else

??? {

??????? //獲取循環(huán)次數(shù)

??????? int times = strLength / pwdStrLength;

??????? int i, j;

??????? for (i = 0; i < times;i++)

??????? {

??????????? //循環(huán)密碼

??????????? for (j = 0; j < pwdStrLength;j++)

??????????? {

??????????????? str[pwdStrLength * i + j] ^= pwdStr[j];

??????????? }

??????? }

??????? //剩下的長度

??????? int lastLength = strLength % pwdStrLength;

??????? for (i = 0; i < lastLength;i++)

??????? {

??????????? str[pwdStrLength * (strLength / pwdStrLength) + i] ^= pwdStr[i];

??????? }

??? }

??? return str;

}

?

/*************************************************************

Function???????? :str2DecodedString()

Description????? :將字符串變成解密之后的文件

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pwdStr?????????? :將字符串變成加密后的字符串

str????????????? :要解密的字符串

Output?????????? :輸出的信息為空

Return?????????? :返回解密后的字符串地址

Others?????????? :其它說明

*************************************************************/

char * str2DecodedString(char *pwdStr, char *str)

{

??? //獲取加密長度

??? int pwdStrLength = strlen(pwdStr);

??? //獲取字符串的長度

??? int strLength = strlen(str);

??? if (strLength % pwdStrLength)

??? {

??????? int times = strLength / pwdStrLength;

??????? int i, j;

??????? //循環(huán)次數(shù)

??????? for (i = 0; i < times; i++)

??????? {

??????????? for (j = 0; j < pwdStrLength; j++)

??????????? {

??????????????? str[pwdStrLength * i + j] ^= pwdStr[j];

??????????? }

??????? }

??? }

??? else

??? {

??????? //獲取循環(huán)次數(shù)

??????? int times = strLength / pwdStrLength;

??????? int i, j;

??????? for (i = 0; i < times; i++)

??????? {

??????????? //循環(huán)密碼

??????????? for (j = 0; j < pwdStrLength; j++)

??????????? {

??????????????? str[pwdStrLength * i + j] ^= pwdStr[j];

??????????? }

??????? }

??????? //剩下的長度

??????? int lastLength = strLength % pwdStrLength;

??????? for (i = 0; i < lastLength; i++)

??????? {

??????????? str[pwdStrLength * (strLength / pwdStrLength) + i] ^= pwdStr[i];

??????? }

??? }

??? return str;

}

?

/*************************************************************

Function???????? :encryptAndDecodeFileByString()

Description????? :通過字符串str來加解密文件

Calls??????????? :被本函數(shù)調用的函數(shù)清單

CalledBy??????? :調用本函數(shù)的函數(shù)清單

TableAccessed?? :被訪問的表(此項僅對于牽扯到數(shù)據(jù)庫操作的程序)

TableUpdated??? :被修改的表(此項對于牽扯到數(shù)據(jù)庫操作的程序)

?

pDestFile??????? :要寫入的文件

pOriginalFile??? :原始文件

pwdString??????? :用于加解密的字符

Output?????????? :輸出的信息為空

Return?????????? :如果寫入成功返回1,如果寫入失敗返回0

Others?????????? :其它說明

*************************************************************/

int encryptAndDecodeFileByString(const char *pDestFile,

??? const char *pOriginalFile, const char* pwdString)

{

??? FILE *pfr, *pfw;

??? //讀取

??? pfr = fopen(pOriginalFile, "r");

??? //寫入

??? pfw = fopen(pDestFile, "w");

??? if (pfr == NULL || pfw == NULL)

??? {

??????? fclose(pfw);

??????? fclose(pfr);

??????? return 0;

??? }

??? else

??? {

??????? int length = getFileSize(pOriginalFile);

??????? char *newstr = (char*)malloc(sizeof(char) * (length + 1));

??????? int i;

??????? for (i = 0; i < length;i++)

??????? {

??????????? //獲取一個字符

??????????? char ch = fgetc(pfr);

??????????? //不斷存入字符

??????????? newstr[i] = ch;

??????? }

??????? //字符串處理為'\0'

??????? newstr[length] = '\0';

??????? //加密字符串

??????? str2EncryptString(pwdString,newstr);

?

??????? for (i = 0; i < length;i++)

??????? {

??????????? //挨個寫入字符

??????????? fputc(newstr[i], pfw);

??? ??? }

??? }

??? fclose(pfr);

??? //關閉文件

??? fclose(pfw);

???

??? return -1;

}

?

?

?

?

?

?

?

?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的C语言文件操作函数的编写的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。