1、 編寫檔案操作的頭檔案
/**************************************************************************
copyright(c) :2014-08-5 toto
filename :file.h
author :塗作權
version :v1.1
date :2014-08-05
description :檔案操作的頭檔案
others :參考了原來的多線程操作和單線程操作當當網資料
functionlist :
history:修改曆史記錄清單,每條修改記錄應該包括修改日期
1.date:
author:
modification:
**************************************************************************/
/*将安全檢查警報設定為失效,必須放在第一行*/
#pragma warning(disable:4996)
#ifndef
_file_h_
#define
#include
<stdio.h> //注意檔案操作相關函數的頭檔案是stdio.h
#include<stdlib.h>
//為了使用system函數
<string.h> //字元串操作所用到的頭檔案
#include<windows.h>
//使用休眠函數時用到的頭檔案
<crtdbg.h> //做記憶體洩露檢測所需的頭檔案
/*************************************************************
function :getfilerow
description :獲得檔案的中記錄的行數
calls :被本函數調用的函數清單
calledby :調用本函數的函數清單
tableaccessed :被通路的表(此項僅對于牽扯到資料庫操作的程式)
tableupdated :被修改的表(此項對于牽扯到資料庫操作的程式)
path :表示的是檔案的路徑
output :輸出的資訊為空
return :傳回檔案中的行數
others :其它說明
*************************************************************/
int
getfilerow(const
char *path);
function :main_view
description :檢視菜單資訊
return :傳回值為void
void
main_view();
function :getfilesize
description :獲得檔案的大小(位元組數)
path :表示的檔案路徑
return :檔案的位元組大小
getfilesize(const
function :loadfile
description :加載檔案
tableupdated :被修改的表(此項對于牽扯到資料庫操作的程式)
path :表示的是檔案路徑
rowsize :表示的是檔案中資訊由多少行
return :傳回值為int,如果加載成功傳回1,如果加載失敗傳回0
loadfile(const char *fpath,
const int
rowsize);
function :findstringbykeyword
description :通過關鍵字的方式進行查找
keyword :通過關鍵字查找資訊
return :如果查找失敗傳回0,查找成功傳回1
findstringbykeyword(const
char *keyword, const
int rowsize);
function :freememory
description :釋放記憶體空間
pp :存儲檔案資訊的二重指針
rowsize :表示的行大小
freememory(char **pp,
function :writestr2file
description :将字元串拼接的方式寫入檔案
pfilepath :要寫入的檔案路徑
pstr :要寫入的字元串
mode :表示寫入的模式,0:表示直接追擊 1:表示換行追加
return :如果寫入成功傳回1,如果寫入失敗傳回0
others :其它說明
writestr2file(const
char *pfilepath,
const char *pstr,
int mode);
function :copy()
description :将一個檔案拷貝到另外一個檔案
pdestfile :要寫入的檔案
poriginalfile :原始檔案
output :輸出的資訊為空
<a target="_blank">int</a>
copy(const
char *pdestfile,
const char *poriginalfile);
function :encryptfilebychar()
description :将檔案按照ch進行加密,并将檔案輸出到指定檔案中
ch :用于加密的字元
encryptfilebychar(const
const
char *poriginalfile, const
char ch);
function :decodefilebychar()
description :通過ch進行解密檔案,并将檔案輸出到指定的檔案中
ch :用于解密的字元
decodefilebychar(const
function :stringencrypt()
description :将字元串變成字元串加密後的字元串
pwdstr :将字元串變成加密後的字元串
str :要加密的字元串
return :傳回加密後的字元串位址
char *
str2encryptstring(char *pwdstr,
char *str);
function :str2decodedstring()
description :将字元串變成解密之後的檔案
str :要解密的字元串
return :傳回解密後的字元串位址
str2decodedstring(char *pwdstr,
function :encryptanddecodefilebystring()
description :通過字元串str來加解密檔案
pwdstring :用于加解密的字元
encryptanddecodefilebystring(const
char* pwdstring);
#endif
2.檔案操作的實作函數
_crt_secure_no_warnings
"file.h"
char **pp =
null; //存儲指針數組的位址
int flag = 0;
char *path)
{
file *pf;
pf =
fopen(path,
"r");
if (pf ==
null)
{
//代表擷取失敗
fclose(pf);
return -1;
}
else
int
i = 0;
//是否到檔案末尾,如果到了傳回1,沒有到的時候傳回0
while (!feof(pf))
{
str[275];
//讀取一行
pf);
i++;
}
return
i;
}
calledby :調用本函數的函數清單
main_view()
system("cls");
printf("\n******************當當使用者資訊查詢系統*******************\n");
printf("\t1.載入資料至記憶體(-l)\n");
printf("\t2.查找字元串(-s)\n");
printf("\t3.釋放記憶體(-f)\n");
printf("\n*************************0.退出**************************\n");
//定義一個檔案指針
file *pf =
null;
//代表擷取檔案失敗
//fseek():moves thefile pointer to a specified location.
fseek(pf, 0,
seek_end);
//ftell( file*stream ):檔案開頭到目前位置有多少個位元組
num = ftell(pf);
//關閉檔案
num;
rowsize)
printf("加載檔案後,請稍後....");
pp = (char **)malloc(sizeof(char *)*
fopen(fpath,
printf("對不起,加載檔案失敗!");
return 0;
for (i = 0;
i < rowsize;i++)
//讀取字元串的緩沖區
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';
//存儲字元串的首位址到指針數組
pp[i] =
px;
return 1;
int rowsize)
if (pp ==
printf("對不起,您還沒有加載檔案,請您先加載檔案\n");
//周遊所有的指針數組的位址,字元串查找
char *ptemp =
strstr(pp[i],keyword);
if (ptemp !=
{
printf("\n%s",pp[i]);
}
int
for (i = 0;
free(pp[i]);
free(pp);
flag = 0;
int mode)
//a+表示以追加的方式寫檔案,如果
fopen(pfilepath,
"a+");
//表示檔案打開失敗
if (mode)
fputs(pstr,
//通過加入'\r'的方式換行,如果隔行輸出使用'\r\n'
fprintf(pf,
"\r");
else
pstr);
//寫入成功傳回1
copy(const char *pdestfile,
const char *poriginalfile)
file *pfr, *pfw;
//以代開二進制檔案的方式打開
pfr =
fopen(poriginalfile,
//要寫入的檔案
pfw =
fopen(pdestfile,
"w");
if (pfr ==
null || pfw ==
fclose(pfr);
fclose(pfw);
length = getfilesize(poriginalfile);
//配置設定記憶體,讀取檔案
char *p = (char *)malloc(length *
sizeof(char));
//讀取檔案到記憶體
fread(p,sizeof(char),length,pfr);
//寫入檔案
fwrite(p,sizeof(char),length,pfw);
char *poriginalfile,
const char ch)
//以讀的方式寫入檔案
//以寫的方式寫入檔案中
//表示加載失敗
//讀取原始檔案中的大小(通過這種方式獲得位元組數大小)
char *p = (char *)malloc(sizeof(char)
* length);
//讀取檔案到記憶體中
i < length;i++)
//加密方法是,于指定字元串進行異或操作
p[i] ^=
ch;
fopen(pdestfile,"w");
//如果是成功,則
//讀取原始檔案的大小
char *p = (char *)
malloc(sizeof(char) *
length);
fread(p,
sizeof(char),
length, pfr);
i < length;
i++)
//解密方法是,于指定字元串進行異或操作
fwrite(p,
length, pfw);
char *str)
//擷取加密長度
pwdstrlength = strlen(pwdstr);
//擷取字元串的長度
strlength = strlen(str);
if (strlength %
pwdstrlength)
times = strlength /
pwdstrlength;
i, j;
//循環次數
i < times;
for (j = 0;
j < pwdstrlength;j++)
str[pwdstrlength *
i + j] ^=
pwdstr[j];
//擷取循環次數
i < times;i++)
//循環密碼
//剩下的長度
lastlength = strlength %
i < lastlength;i++)
str[pwdstrlength * (strlength /
pwdstrlength) + i] ^=
pwdstr[i];
return
str;
description :将字元串變成解密之後的檔案
return :傳回解密後的字元串位址
j < pwdstrlength;
j++)
i < lastlength;
const char* pwdstring)
//讀取
//寫入
char *newstr = (char*)malloc(sizeof(char)
* (length + 1));
//擷取一個字元
ch = fgetc(pfr);
//不斷存入字元
newstr[i] =
//字元串處理為'\0'
newstr[length] =
//加密字元串
str2encryptstring(pwdstring,newstr);
//挨個寫入字元
fputc(newstr[i],
pfw);
}
fclose(pfr);
//關閉檔案
fclose(pfw);
return -1;