程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> [C/C++學院]0801

[C/C++學院]0801

編輯:關於C++

重定向以及文件掃描

 

#define   _CRT_SECURE_NO_WARNINGS//關閉安全檢查

#include

#include

void main1()

{

       char str[100] = { 0 };

       scanf(%s, str);

       printf(str=%s
, str);

       system(str);

}

void main2()

{

       char str[100] = { 0 };

       fscanf(stdin,%s, str);

       fprintf(stdout,str=%s
, str);

       system(str);

}

void  main3()

{

       char *path = C:\Users\wuyq\Desktop\newcmd.txt;     

       int num=0;

       char docmd[30] = { 0 };

       scanf(%d%s, &num, docmd);//接受鍵盤輸入

       FILE *pf;

       pf = fopen(path, w);//寫入

       if (pf == NULL)

       {

              printf(文件打開失敗);

       }

       else

       {

              fprintf(pf, for /l %%i in (1,1,%d) do %s, num, docmd);

              fclose(pf);

       }

       system(pause);

}

 

void main4()

{

       char cmd[100] = { 0 };

       int num = 0;

       char docmd[30] = { 0 };

       char *path = C:\Users\wuyq\Desktop\newcmd.txt;

       FILE *pf = fopen(path, r);//讀取

       if (pf == NULL)

       {

              printf(文件打開失敗);

              return;

       }

       else

       {

              fscanf(pf, for /l %%i in (1,1,%d) do %s, &num, docmd);

              printf(num=%d,docmd=%s, num, docmd);

       }

       system(pause);

}

 

void main()

{

       int num ;

       scanf(num=%d, &num);//必須精確對應

       printf(num=%d, num);

       system(pause);

}

 

 

二進制加密解密

#define  _CRT_SECURE_NO_WARNINGS//關閉安全檢查

#include

#include

 

int getfilesize(char *path)

{

       FILE *pf = fopen(path, r);

       if (pf == NULL)

       {

              return -1;

       }

       else

       {

              fseek(pf, 0, SEEK_END);

              int length = ftell(pf);

              return length;//獲取文件大小

 

       }

}

 

 

void copy(char *oldpath, char *newpath)

{

       FILE *pfr, *pfw;

       pfr = fopen(oldpath, rb);

       pfw = fopen(newpath, wb);//寫入二進制模式

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

       {

              fclose(pfr);//關閉文件

              fclose(pfw);

              return;

       }

       else

       {

              int length = getfilesize(oldpath);

              char *p = (char *)malloc(length*sizeof(char));//分配內存,讀取文件

              fread(p, sizeof(char), length, pfr);//讀取二進制到內存

              fwrite(p, sizeof(char), length, pfw);//寫入二進制到文件         

             

              fclose(pfr);//關閉文件

              fclose(pfw);

       }

}

 

 

void jia(char *oldpath, char *newpath)

{

       FILE *pfr, *pfw;

       pfr = fopen(oldpath, rb);

       pfw = fopen(newpath, wb);//寫入二進制模式

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

       {

              fclose(pfr);//關閉文件

              fclose(pfw);

              return;

       }

       else

       {

              int length = getfilesize(oldpath);

              char *p = (char *)malloc(length*sizeof(char));//分配內存,讀取文件

              fread(p, sizeof(char), length, pfr);//讀取二進制到內存

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

              {

                     p[i] ^= 'A';//抑或

              }            

              fwrite(p, sizeof(char), length, pfw);//寫入二進制到文件

 

              fclose(pfr);//關閉文件

              fclose(pfw);

       }

}

 

void jie(char *oldpath, char *newpath)

{

       FILE *pfr, *pfw;

       pfr = fopen(oldpath, rb);

       pfw = fopen(newpath, wb);//寫入二進制模式

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

       {

              fclose(pfr);//關閉文件

              fclose(pfw);

              return;

       }

       else

       {

              int length = getfilesize(oldpath);

              char *p = (char *)malloc(length*sizeof(char));//分配內存,讀取文件

              fread(p, sizeof(char), length, pfr);//讀取二進制到內存

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

              {

                     p[i] ^= 'A';//解密

              }

 

              fwrite(p, sizeof(char), length, pfw);//寫入二進制到文件

 

              fclose(pfr);//關閉文件

              fclose(pfw);

       }

}

 

void main()

{

     char  *oldpath = C:\Users\wuyq\Desktop\calc.exe;

       char  *newpath = C:\Users\wuyq\Desktop\calc.exe;

       char  *newjiepath = C:\Users\wuyq\Desktop\calc.exe;

       jia(oldpath, newpath);

       jie(newpath, newjiepath);

      

       system(pause);

}

簡單加密


#define  _CRT_SECURE_NO_WARNINGS//關閉安全檢查

#include

#include

 

// 'a' ->'b'

//hello -> ifmmp

//textjjia.txt

//textjie.txt

//按照字節的方式加密

 

char jiami(char ch)

{

       return ch ^ 123;

}

 

char jiemi(char ch)

{

       return ch  ^123;

}

 

void jia(char *path, char *pathjia)

{

       FILE *pfr, *pfw;

       pfr = fopen(path, r);//讀取

       pfw = fopen(pathjia, w);//寫入

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

       {

              return;

       }

       else

       {

              while (!feof(pfr))//到了文件末尾1,沒有到就是0

              {

                     char  ch = fgetc(pfr);//讀取字符

                     putchar(ch);

                     fputc(jiami(ch), pfw);//寫入一個加密的字符

              }

              fclose(pfr);

              fclose(pfw);//關閉文件

       }

}

 

void jie(char *path, char *pathjie)

{

       FILE *pfr, *pfw;

       pfr = fopen(path, r);//讀取

       pfw = fopen(pathjie, w);//寫入

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

       {

              return;

       }

       else

    {

              while (!feof(pfr))//到了文件末尾1,沒有到就是0

              {

                     char  ch = fgetc(pfr);//讀取字符

                     putchar(ch);

                     fputc(jiemi(ch), pfw);//寫入一個加密的字符

              }

              fclose(pfr);

              fclose(pfw);//關閉文

       }

}

 

void main()

{

       char *path = C:\Users\yincheng01\Desktop\text.txt;

       char *pathjia = C:\Users\yincheng01\Desktop\textjia.txt;

       char *pathjie = C:\Users\yincheng01\Desktop\textjie.txt;

       jia(path, pathjia);

       jie(pathjia, pathjie);

 

       system(pause);

}

 

void main1()

{

       FILE *pfr;

       char *path = C:\Users\yincheng01\Desktop\text.txt;

       pfr = fopen(path, r);

       if (pfr == NULL)

       {

              printf(文件打開失敗);

       }

       else

       {

              printf(
原來的資料
);

              while (!feof(pfr))//feof沒有到文件末尾返回0,到了返回1

              {

                     char ch=fgetc(pfr);//從文件讀取一個字符

                     putchar(ch);//輸出字符

 

              }

              rewind(pfr);//回到文件開頭

              printf(
加密後的資料
);

              while (!feof(pfr))//feof沒有到文件末尾返回0,到了返回1

              {

                     char ch = fgetc(pfr);//從文件讀取一個字符

                     putchar(ch+1);//輸出字符

 

              }

              fclose(pfr);//關閉文件

 

       }

       system(pause);

}

按照密碼加密

#define  _CRT_SECURE_NO_WARNINGS//關閉安全檢查

#include

#include

#include

 

//加密,按照密碼 

// 文件加密

// 字符串加密

char * stringjiami(char *password, char *string);

//字符串解密

char * stringjiemi(char *password, char *jiastring);

void filejiami(char *path, char *pathjia, char *password);

void filejiemi(char *pathjia, char *pathjie, char *password);

#define  _CRT_SECURE_NO_WARNINGS//關閉安全檢查

#include

#include

#include

 

//加密,按照密碼 

// 文件加密

// 字符串加密

char * stringjiami(char *password, char *string);

//字符串解密

char * stringjiemi(char *password, char *jiastring);

void filejiami(char *path, char *pathjia, char *password);

void filejiemi(char *pathjia, char *pathjie, char *password);

#include密碼加密.h

 

int  getfilesize(char * path)

{

         FILE *pf = fopen(path, r);

         if (pf == NULL)

         {

                   return -1;

         }

         else

         {

                   fseek(pf, 0, SEEK_END);//文件末尾

                   int length = ftell(pf);

                   return length;//返回長度

         }

}

 

char * stringjiami(char *password, char *string)

{

         int passlength = strlen(password);//獲取加密長度

         int stringlength = strlen(string);//獲取字符串長度

         if (stringlength %passlength ==0)

         {

                   int ci = stringlength /passlength;//循環次數

                   for (int i = 0; i < ci; i++)//循環次

                   {

                            for (int j =0 ; j < passlength; j++)//循環密碼

                            {

                                     string[passlength*i+j] ^= password[j];//異或加密

                            }

                   }

 

         }

         else

         {

                   int ci = stringlength / passlength;//循環次數

                   for (int i = 0; i < ci; i++)//循環次

                   {

                            for (int j = 0; j < passlength; j++)//循環密碼

                            {

                                     string[passlength*i + j] ^= password[j];//異或加密

                            }

                   }

                   int lastlength = stringlength%passlength;//剩下的長度

                   for (int i = 0; i < lastlength; i++)

                   {

 

                            string[passlength*(stringlength/passlength)+i] ^= password[i];

                   }

         }

         return  string;

}

//字符串解密

char * stringjiemi(char *password, char *jiastring)

{

         int passlength = strlen(password);//獲取加密長度

         int stringlength = strlen(jiastring);//獲取字符串長度

         if (stringlength %passlength == 0)

         {

                   int ci = stringlength / passlength;//循環次數

                   for (int i = 0; i < ci; i++)//循環次

                   {

                            for (int j = 0; j < passlength; j++)//循環密碼

                            {

                                     jiastring[passlength*i + j] ^= password[j];//異或加密

                            }

                   }

         }

         else

         {

                   int ci = stringlength / passlength;//循環次數

                   for (int i = 0; i < ci; i++)//循環次

                   {

                            for (int j = 0; j < passlength; j++)//循環密碼

                            {

                                     jiastring[passlength*i + j] ^= password[j];//異或加密

                            }

                   }

                   int lastlength = stringlength%passlength;//剩下的長度

                   for (int i = 0; i < lastlength; i++)

                   {

 

                            jiastring[passlength*(stringlength / passlength) + i] ^= password[i];

                   }

         }

         return  jiastring;

}

 

 

void filejiami(char *path, char *pathjia, char *password)

{

 

         FILE *pfr,*pfw;

         pfr = fopen(path, r);

         pfw = fopen(pathjia, w);

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

         {

                   fclose(pfr);

                   fclose(pfw);

                   return;

         }

         else

         {

                   int length = getfilesize(path);//獲取長度   430

                   //int passlength = strlen(password);//獲取密碼20

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

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

                   {

                            char ch = fgetc(pfr);//獲取一個字符

                            newstr[i] = ch;//不斷存入字符

                            //fputc(newstr[i], pfw);//挨個寫入字符

 

                   }

                   newstr[length] = '';//字符串處理為''

                   stringjiami(password, newstr);//加密字符串

 

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

                   {

                            fputc(newstr[i], pfw);//挨個寫入字符

                   }      

                   fclose(pfr);

                   fclose(pfw);//關閉文件

         }

}

 

void filejiemi(char *pathjia, char *pathjie, char *password)

{

         FILE *pfr, *pfw;

         pfr = fopen(pathjia, rb);

         pfw = fopen(pathjie, wb);

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

         {

                   fclose(pfr);

                   fclose(pfw);

                   return;

         }

         else

         {

                   while (!feof(pfr))

                   {

                            char string[256] = { 0 };

                            fgets(string, 256, pfr);

                            stringjiemi(password, string);//加密啊

                            fputs(string, pfw);//寫入jie密文件

 

                   }

                   fclose(pfr);

                   fclose(pfw);//關閉文件

         }

}

#define  _CRT_SECURE_NO_WARNINGS//關閉安全檢查

#include

#include

#include密碼加密.h

 

 

void   main()

{

         char string[50] = 鋤禾日當午;

         char *password = 123;

         printf(%s
, stringjiami(password, string));

         printf(%s
, stringjiami(password, string));

 

 

         char *path = C:\Users\yincheng01\Desktop\text.txt;

         char *pathjia = C:\Users\yincheng01\Desktop\textjia.txt;

         char *pathjie = C:\Users\yincheng01\Desktop\textjie.txt;

         //printf(%d
, getfilesize(path));

         filejiami(path, pathjia, ABCDE);

         filejiami(pathjia, pathjie, ABCDE);

 

         system(pause);

}

動態庫與靜態庫

靜態庫

Lib.h

void msg();

int  add(int a, int b);
Lib.c
#include

#include

#include

 

void msg()

{

         MessageBoxA(0, 1, 2, 0);

}

 

int  add(int a, int b)

{

         return a + b;

}
Main.c
#include

#include

#includelib.h

 

#pragma comment(lib, 靜態庫與動態庫.lib)

//頭文件只是說明,lib自己已經存在接口

void main()

{

         printf(%d, add(10, 9));

         msg();

 

}

動態庫

Dll.c

#include

#include

#include

 

//導出函數,可以加載的時候調用

_declspec(dllexport) void msg()

{

         MessageBoxA(0, 1, 2, 0);

}

 

//導出函數,可以加載的時候調用

_declspec(dllexport) int  add(int a, int b)

{

         return a + b;

}

Main.c
#include

#include

#include

 

typedef void  (*pmsg)();//簡化函數指針

typedef int(*padd)(int a, int b);//簡化函數指針

 

void main()

{

         HMODULE mydll = LoadLibraryA(動態庫.dll);

         if (mydll == NULL)

         {

                   printf(動態庫加載失敗);

         }

         else

         {

                   pmsg pmsg1;//定義一個函數指針

                   pmsg1 = (pmsg)GetProcAddress(mydll, msg);//獲取函數地址

                   if (pmsg1 != NULL)

                   {

                            pmsg1();//執行

                   }

                   padd padd1;//定義函數指針

                   padd1 = (padd)GetProcAddress(mydll, add);

                   if (padd1 != NULL)

                   {

                            printf(
%d, padd1(10, 29));

                   }

 

         }

         FreeLibrary(mydll);

         system(pause);

}







 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved