C、C++笔记

2017年6月 阅读书籍《C和指针》


 #if 0

 #endif

比注释掉代码好。(《C和指针》2017.06.07)


 

全局变量和全局静态变量的区别
1)全局变量是不显式用static修饰的全局变量,全局变量默认是有外部链接性的,作用域是整个工程,在一个文件内定义的全局变量,在另一个文件中,通过extern 全局变量名的声明,就可以使用全局变量。
2)全局静态变量是显式用static修饰的全局变量,作用域是声明此变量所在的文件,其他的文件即使用extern声明也不能使用。(2017.06.07)
 

数组名的值是指针常量。
数组有指针与下标两种使用方式,下标绝不会比指针更有效率,但指针有时会比下标更有效率。
指针比下标更有效率的场合:在循环中一次一步地移动时。(《C和指针》2017.06.12)

 
 二维数组做函数参数:
void func2( int (*mat)[10] );
void func2( int mat[][10] );
 
二级指针的作用。
函数外面指针*p,要通过函数修改其指向的内容,函数形参应写为**p,否则作为值传递,只是单纯拷贝一份指针。你可以改变指针所指向内存的值,但你不能改变原指针所对指向的内存位置。
 
 

 gcc C语言编译:

       (1).预处理,生成预编译文件(.文件):

        Gcc –E hello.c –o hello.i
       (2).编译,生成汇编代码(.s文件):

        Gcc –S hello.i –o hello.s
       (3).汇编,生成目标文件(.o文件):
        Gcc –c hello.s –o hello.o
       (4).链接,生成可执行文件:
        Gcc hello.o –o hello


 

 目标文件反汇编objdump -S obj   或者 objdump -d obj

 各个段信息        objdump -s obj

 查看目标文件文件格式分段信息objdump -h obj 

查看各个段大小size obj

 


 

函数指针:

  int f(int);

      int (*pf)(int) = &f;

      int ans = f(25);

      ans = (*pf)(25);     //将函数指针转化为函数名,和上一条左右一致

      ans = pf(25);         //这个转换并不是必须的,因为编译器在执行函数调用操作符之前又会把它转换回去2333


C++ 获取指定目录下所有文件目录,这个函数是网上找的,但没有提到64位和32位写法的差别,我这里是64位和32位两个版本。

需要头文件:

#include <io.h>
#include <iostream>
#include <vector>
using namespace std;

32位:

/*
** 作者:sonne
** 日期:2017-06-12
** 说明:读取制定目录下所有文件路径和文件名
*/
void FileOperate::getFileNamesAndPaths(string path, vector<string>& files, vector<string>& file_name)
{
    //文件句柄  
    long   hFile = 0;
    //文件信息,声明一个存储文件信息的结构体  
    struct _finddata_t fileinfo;
    //字符串,存放路径
    string p;
    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)//若查找成功,则进入
    {
        do
        {
            //如果是目录,迭代之
            if ((fileinfo.attrib &  _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                     getFileNamesAndPaths(p.assign(path).append("\\").append(fileinfo.name), files, file_name);
            }
            //如果不是,加入列表  
            else
            {
                 files.push_back(p.assign(path).append("\\").append(fileinfo.name));
                 file_name.push_back(fileinfo.name);
             }
        } while (_findnext(hFile, &fileinfo) == 0);
        //_findclose函数结束查找
        _findclose(hFile);
    }
}

 

64位:

/*
 ** 作者:sonne
 ** 日期:2017-06-12
 ** 说明:读取制定目录下所有文件路径和文件名
 */
void getFileNamesAndPaths(string path, vector<string>& files, vector<string>& file_name)
{
    //文件句柄  
    __int64   hFile = 0;
    //文件信息,声明一个存储文件信息的结构体  
    //struct _finddata_t fileinfo;
    struct __finddata64_t fileinfo;
    //字符串,存放路径
    string p;
    if ((hFile = _findfirst64(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)//若查找成功,则进入
    {
        do
        {
            //如果是目录,迭代之
            if ((fileinfo.attrib &  _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getFileNamesAndPaths(p.assign(path).append("\\").append(fileinfo.name), files, file_name);
            }
            //如果不是,加入列表  
            else
            {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
                file_name.push_back(fileinfo.name);
            }
        } while (_findnext64(hFile, &fileinfo) == 0);
        //_findclose函数结束查找
        _findclose(hFile);
    }
}

 


C++拷贝构造函数有两种,深拷贝和浅拷贝。

浅拷贝只是简单赋值成员变量。

若涉及堆内存,需要深拷贝。否则浅拷贝的方式会导致两个指针指向同一个堆内存可能导致同一内存被释放两次。

可以将拷贝构造函数写为private来禁止拷贝。


 删除一个目录下全部文件夹和文件:

bool removeDir(const char* szFileDir)
{
    std::string strDir = szFileDir;
    if (strDir.at(strDir.length() - 1) != '\\')
        strDir += '\\';
    WIN32_FIND_DATA wfd;
    HANDLE hFind = FindFirstFile((strDir + "*.*").c_str(), &wfd);
    if (hFind == INVALID_HANDLE_VALUE)
        return false;
    do
    {
        if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            if (_stricmp(wfd.cFileName, ".") != 0 &&
                _stricmp(wfd.cFileName, "..") != 0)
                removeDir((strDir + wfd.cFileName).c_str());
        }
        else
        {
            DeleteFile((strDir + wfd.cFileName).c_str());
        }
    } while (FindNextFile(hFind, &wfd));
    FindClose(hFind);
    RemoveDirectory(szFileDir);
    return true;
}

 

获取当前目录(当前程序所在文件夹):

    char cPath[MAX_PATH+1];
    _getcwd(cPath, sizeof(cPath));

 

windows下vs编译的C++程序要求获取管理员权限:

打开项目-项目属性-连接器-清单文件-UAC执行级别修改为requireAdministrator (/level='requireAdministrator')保存运行即可。

对你没看错,就这么简单!运行时自动要求提供管理员权限

 

获取系统目录:

    char str[MAX_PATH];
    GetSystemDirectory(str, MAX_PATH);
    std::cout << str << std::endl;

输出C:\WINDOWS\system32

 


 

 禁止64位系统文件重定向:

    PVOID OldValue = NULL;
    Wow64DisableWow64FsRedirection(&OldValue);
        //………………
        //………………
    Wow64RevertWow64FsRedirection(OldValue);

 


字符串转化为guid:

typedef struct _GUID_TEMP {
    unsigned long  Data1;
    unsigned short Data2;
    unsigned short Data3;
    int  Data4[8];
} GUID_TEMP;

BOOL LPCTSR2GUID(LPCTSTR szGUID, GUID &Guid) {
    memset(&Guid, 0, sizeof(Guid));

    GUID_TEMP GUID_TEMP;

    int nRet =
        sscanf_s(szGUID,
            "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
            &GUID_TEMP.Data1,
            &GUID_TEMP.Data2,
            &GUID_TEMP.Data3,
            &GUID_TEMP.Data4[0], &GUID_TEMP.Data4[1],
            &GUID_TEMP.Data4[2], &GUID_TEMP.Data4[3], &GUID_TEMP.Data4[4], &GUID_TEMP.Data4[5], &GUID_TEMP.Data4[6], &GUID_TEMP.Data4[7]
        );
    Guid.Data1 = GUID_TEMP.Data1;
    Guid.Data2 = GUID_TEMP.Data2;
    Guid.Data3 = GUID_TEMP.Data3;
    memcpy(&Guid.Data4[0], &GUID_TEMP.Data4[0], 1);
    memcpy(&Guid.Data4[1], &GUID_TEMP.Data4[1], 1);
    memcpy(&Guid.Data4[2], &GUID_TEMP.Data4[2], 1);
    memcpy(&Guid.Data4[3], &GUID_TEMP.Data4[3], 1);
    memcpy(&Guid.Data4[4], &GUID_TEMP.Data4[4], 1);
    memcpy(&Guid.Data4[5], &GUID_TEMP.Data4[5], 1);
    memcpy(&Guid.Data4[6], &GUID_TEMP.Data4[6], 1);
    memcpy(&Guid.Data4[7], &GUID_TEMP.Data4[7], 1);
    return (nRet == 11) ? TRUE : FALSE;
}

 guid转化为字符串:

LPCTSTR GUID2LPCTS(GUID Guid) {
    static TCHAR szBuf[65] = { 0 };
    _snprintf_s(szBuf, 64, _T("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"),
        Guid.Data1,
        Guid.Data2,
        Guid.Data3,
        Guid.Data4[0], Guid.Data4[1],
        Guid.Data4[2], Guid.Data4[3], Guid.Data4[4], Guid.Data4[5], Guid.Data4[6], Guid.Data4[7]
    );
    return szBuf;
}

 


写数据到bmp图片:

int WriteBMP(const char* file, unsigned char* Input)
{
    unsigned char head[1078] = {
        /***************************/
        //file header
        0x42,0x4d,//file type 
                  //0x36,0x6c,0x01,0x00, //file size***
        0x0,0x0,0x0,0x00, //file size***
        0x00,0x00, //reserved
        0x00,0x00,//reserved
        0x36,0x4,0x00,0x00,//head byte***
        0x28,0x00,0x00,0x00,//struct size
        0x00,0x00,0x0,0x00,//map width*** 
        0x00,0x00,0x00,0x00,//map height***

        0x01,0x00,//must be 1
        0x08,0x00,//color count***
        0x00,0x00,0x00,0x00, //compression 
        0x00,0x00,0x00,0x00,//data size***
        0x00,0x00,0x00,0x00, //dpix
        0x00,0x00,0x00,0x00, //dpiy
        0x00,0x00,0x00,0x00,//color used
        0x00,0x00,0x00,0x00,//color important
    };

    FILE *fh;

    if ((fh = fopen(file, "wb")) == NULL)
        return 0;

    int i, j;

    long num;
    num = IMAGE_X;
    head[18] = num & 0xFF;
    num = num >> 8;  head[19] = num & 0xFF;
    num = num >> 8;  head[20] = num & 0xFF;
    num = num >> 8;  head[21] = num & 0xFF;

    num = IMAGE_Y; head[22] = num & 0xFF;
    num = num >> 8;  head[23] = num & 0xFF;
    num = num >> 8;  head[24] = num & 0xFF;
    num = num >> 8;  head[25] = num & 0xFF;

    j = 0;
    for (i = 54; i < 1078; i = i + 4)
    {
        head[i] = head[i + 1] = head[i + 2] = j;
        head[i + 3] = 0;
        j++;
    }

    fwrite(head, 1, 1078, fh);

    for (i = 0; i <= IMAGE_Y - 1; i++)
    {

        fseek(fh, 1078 + (IMAGE_Y - 1 - i)*IMAGE_X, SEEK_SET);

        fwrite((Input + i * IMAGE_X), 1, IMAGE_X, fh);
    }
    fclose(fh);
    return 1;
}

 


 读文件到unsigned char数组:

int read_data_hex(unsigned char *buf, int length, const char *string)
{
    FILE *fp;
    fp = fopen(string, "rb");
    if (NULL == fp)
    {
        return -1;
    }
    fread(buf, sizeof(unsigned char), length, fp);

    fclose(fp);
    fp = NULL;
    return 0;
}

写文件到unsigned char数组:

int write_data_hex(unsigned char * array, int length, const char *string)
{
    int i = 0;
    FILE *fp;
    fp = fopen(string, "wb+");
    if (NULL == fp)
    {
        printf("file open Fail!\n");
        return -1;
    }
    while (i < length)
    {
        fwrite(&array[i], sizeof(char), 1, fp);
        i++;
    }
    fclose(fp);
    return 0;
}

 


 获取年月日时分秒:

#include<time.h>
int main()
{
    time_t timep;
    struct tm *p;
    time (&timep);
    p=gmtime(&timep);
    printf("%d\n",p->tm_sec); /*获取当前秒*/
    printf("%d\n",p->tm_min); /*获取当前分*/
    printf("%d\n",8+p->tm_hour);/*获取当前时,这里获取西方的时间,刚好相差八个小时*/
    printf("%d\n",p->tm_mday);/*获取当前月份日数,范围是1-31*/
    printf("%d\n",1+p->tm_mon);/*获取当前月份,范围是0-11,所以要加1*/
    printf("%d\n",1900+p->tm_year);/*获取当前年份,从1900开始,所以要加1900*/
    printf("%d\n",p->tm_yday); /*从今年1月1日算起至今的天数,范围为0-365*/
}

带多态性质的基类(含有virtual函数)应该声明virtual析构函数。(《Effective C++》)


C修改开机密码:

 

下面写法需要获取管理员权限

#include <stdio.h>
#include <stdlib.h>

int main() {
    char user[256] = { 0 };
    const char *user_name = "sonne";
    const char *user_password = "huawei.123";
    char print[256] = { 0 };
    sprintf_s(user, "net user %s %s", user_name, user_password);
    system(user);
    sprintf_s(print, "已经将用户名:%s 的密码改成了:%s", user_name, user_password);
    printf(print);
    system("pause");
    return 0;
}

 微软的函数,可修改密码,需要输入旧密码

NET_API_STATUS NET_API_FUNCTION NetUserChangePassword(
  IN LPCWSTR domainname,
  IN LPCWSTR username,
  IN LPCWSTR oldpassword,
  IN LPCWSTR newpassword
);

如何判断输入的开机密码是否正确?

使用NetUserChangePassword函数,只不过旧密码和新密码参数一样。然后判断返回值。

 

 

 

关于这个函数的例程:

#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")

#include <stdio.h>
#include <windows.h> 
#include <lm.h>

int wmain(int argc, wchar_t *argv[])
{
   DWORD dwError = 0;
   NET_API_STATUS nStatus;
   //
   // All parameters are required.
   //
   if (argc != 5)
   {
      fwprintf(stderr, L"Usage: %s \\\\ServerName UserName OldPassword NewPassword\n", argv[0]);
      exit(1);
   }
   //
   // Call the NetUserChangePassword function.
   //
   nStatus = NetUserChangePassword(argv[1], argv[2], argv[3], argv[4]);
   //
   // If the call succeeds, inform the user.
   //
   if (nStatus == NERR_Success)
      fwprintf(stderr, L"User password has been changed successfully\n");
   //
   // Otherwise, print the system error.
   //
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);

   return 0;
}

 


在全局作用域声明的const变量是定义该对象的文件的局部变量。此变量只存在于那个文件中,不能被其他文件访问。
-----《C++ Primer》

引用与指针的区别:

1. 指针是一个实体,而引用仅是个别名;
2. 引用使用时无需解引用(*),指针需要解引用;
3. 引用只能在定义时被初始化一次,之后不可变;指针可变;
4. 引用没有 const,指针有 const;
5. 引用不能为空,指针可以为空;
6. “sizeof 引用”得到的是所指向的变量(对象)的大小,而“sizeof 指针”得到的是指针本身(所指向的变量或对象的地址)的大小;
7. 指针和引用的自增(++)运算意义不一样;
8.从内存分配上看:程序为指针变量分配内存区域,而引用不需要分配内存区域。

 


动态链接库和COM组件的区别

     1动态链接库的表现形式只能是dll[变态该名的除外], COM组件的表现形式可以是dll也可以是exe。

     注:其实字体、驱动等也算是动态链接库的一种,这里略去...

     2 动态链接库的生成和编译器及系统相关,在Windows/Linux下系统,需要分别编译才能使用。

          COM组件是二进制编码,在Windows和Linux下可以直接使用,不需要重新编译。

     3 COM组件是按照COM规范实现的dll或者exe;动态链接库是一个可以导出函数的函数集合。

     4 动态链接库只能在本机被调用,COM组件支持分布式使用。

 


 

const对象和const成员函数的关系:
(1)有 const 修饰的成员函数(指 const 放在函数参数表的后面,而不是在函数前面或者参数表内),只能读取数据成员,不能改变数据成员;没有 const 修饰的成员函数,对数据成员则是可读可写的。
(2)除此之外,在类的成员函数后面加 const 还有什么好处呢?那就是常量(即 const)对象可以调用 const 成员函数,而不能调用非const修饰的函数。 
 

亦或校验:
BYTE bcc(BYTE* aub_data_p, BYTE auw_data_length)
{
    BYTE aub_check_sum = 0;
    WORD auw_cnt = 0;

    while (auw_cnt < auw_data_length)
    {
        aub_check_sum ^= aub_data_p[auw_cnt];
        auw_cnt++;
    }
    return aub_check_sum;
}

如果需要跳过消息头,则写auw_cnt=1


 
C++追加文件写日志,带时间

 

#include <windows.h>
#include <iostream>
#include <fstream>
#include <time.h>

void printMyTxt(std::string str)
{
    time_t rawtime;
    struct tm* ptminfo;

    time(&rawtime);
    ptminfo = localtime(&rawtime);
    CHAR* strTime = new CHAR[100];
    sprintf(strTime, "%02d-%02d-%02d %02d:%02d:%02d",
        ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday,
        ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
    std::string strLog;

    strLog = "";
    strLog.append(strTime).append("").append(str).append("\r\n");

    std::ofstream write("record.txt", std::ios::app);//打开record.txt文件,以ios::app追加的方式输入
    write << strLog << std::endl;
    write.close();//关闭文件

    return;
}

int main()
{
    printMyTxt("hhhhh");
    printMyTxt("LLLLL");
    std::cout << "Hello World!\n";
}

 


10进制转化为16进制数:

//i要转化的十进制整数,width转化后的宽度,位数不足则补0
std::string dec2hex(int i, int width)
{
    std::stringstream ioss;     //定义字符串流
    std::string s_temp;         //存放转化后字符
    ioss << std::hex << i;      //以十六制形式输出
    ioss >> s_temp;
 
    if(width > s_temp.size())
    {
        std::string s_0(width - s_temp.size(), '0');      //位数不够则补0
        s_temp = s_0 + s_temp;                            //合并
    }
 
    std::string s = s_temp.substr(s_temp.length() - width, s_temp.length());    //取右width位
    return s;
}

 


 

C++嵌套类

实际上只是“名称上的包含”而已,变量也不共享

class outc
{
public:
    int m_out_val;
    void out_fun();
    class innerc
    {
    public:
        int m_inner_val;
        void inner_fun();
    } ic;
};

void outc::out_fun()
{
    this->m_out_val = 2;
    cout << "out val set as:" << this->m_out_val<<endl;
}

void outc::innerc::inner_fun()
{
    this->m_inner_val = 3;
    cout << "inner val set as:" << this->m_inner_val<<endl;
}

int main()
{
    outc oc;
    oc.out_fun();
    outc::innerc ic;
    ic.inner_fun();
    //oc.innerc.inner_fun();
    oc.ic.inner_fun();
    return 0;
}

 

out val set as:2
inner val set as:3
inner val set as:3

 

posted @ 2017-06-07 10:42  J·Marcus  阅读(463)  评论(0编辑  收藏  举报