C++
Wei Jieyang Lv4

简介

语法教程

输入输出

变量类型

基本内置类型

bool

char

int

float

double

void

wchar_t

string

char,char*,string转换

enum

枚举类型

指针

操作

文件操作

1
2
3
4
5
ofstream outfile;
/* 打开文件 */
outfile.open("file.txt", ios::out | ios::trunc ); //模式结合
/* 关闭文件*/
outfile.close();
1
void open(const char *filename, ios::openmode mode);
  • filename:要打开的文件的名称和位置(绝对路径)

    相对路径的位置取得是可执行文件的位置,而不是代码文件所在位置

    • xcode/Users/username/Library/Developer/Xcode/DerivedData/区块链-cwiqijiormracofndmvocmukooxi/Build/Products/Debug
  • mode:文件的打开模式(早期c++用ios代替ios_base)

    • ios::app 追加模式。所有写入都追加到文件末尾。
    • ios::ate 文件打开后定位到文件末尾
    • ios::in 打开文件用于读取(ifstream默认)
    • ios::out 打开文件用于写入(ofstream默认)
    • ios::trunc 如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0

    ios是C++的一个类,ios_base是有关输入输出流的类的公共基类,ios继承了basic_ios类,而basic_ios又继承了ios_base,所以说ios_base也是ios类的间接基类。

    • 其中ios_base类具有静态常量in、out、binary等(在VS中ios_base类还有一个叫_Iosb的基类,这些静态成员在_Iosb中,但是C++标准没有这种说法),
    • ios_base类也是basic_ostream、basic_istream等多个类的直接或间接基类。从这个意义上,写ostream::binary什么的可能也没什么问题,但是习惯上都用ios::in、ios::binary,或者ios_base::in、ios_base::binary之类的。
    • 所以,综上,ios类继承了ios_base类的静态成员变量in、out、binary等,所以既可以用ios,也可以用ios_base。望采纳。

ostream

该数据类型表示输出文件流,用于创建文件并向文件写入信息。

istream

该数据类型表示输入文件流,用于从文件读取信息。

fstream

该数据类型通常表示文件流,且同时具有 ofstream 和 ifstream 两种功能

文件夹

函数说明

opendir(打开目录)

  • 相关函数:open,readdir,closedir,rewinddir,seekdir,telldir,scandir
  • 表头文件:#include<dirent.h>
  • 定义函数:DIR * opendir(const char * name);
  • 函数说明:
    • opendir()用来打开参数name指定的目录,并返回DIR*形态的目录流,和open()类似,接下来对目录的读取和搜索都要使用此返回值。
  • 返回值:成功则返回 *DIR** 型态的目录流,打开失败则返回NULL。
  • 错误代码
    • EACCESS 权限不足
    • EMFILE 已达到进程可同时打开的文件数上限
    • ENFILE 已达到系统可同时打开的文件数上限
    • ENOTDIR 参数name非真正的目录
    • ENOENT 参数name 指定的目录不存在,或是参数name 为一空字符串
    • ENOMEM 核心内存不足。

readdir(读取目录)

  • 相关函数:open,opendir,closedir,rewinddir,seekdir,telldir,scandir

  • 表头文件:#include<dirent.h>

  • 定义函数:struct dirent * readdir(DIR * dir);

  • 函数说明:

    • readdir()返回参数dir目录流的下个目录进入点。

    • struct dirent{
        ino_t d_ino;   //此目录进入点的inode
        ff_t d_off;    //目录文件开头至此目录进入点的位移,当前目录下为0
        signed short int d_reclen;  //d_name的长度,不包含NULL字符
        unsigned char d_type;       //d_name 所指的文件类型
        //DT_REG 文件    DT_DIR 文件夹
        har d_name[256];  //文件名
      };
      
      1
      2
      ```

  • 返回值:成功则返回下个目录进入点。有错误发生或读取到目录文件尾则返回NULL。

  • 错误代码

    • EBADF 参数dir为无效的目录流

closedir(关闭目录)

  • 相关函数:opendir
  • 表头文件:#include<dirent.h>
  • 定义函数:int closedir(DIR *dir);
  • 函数说明:closedir()关闭参数dir所指的目录流。
  • 返回值:关闭成功则返回0,失败返回-1,错误原因存于errno 中
  • 错误代码
    • EBADF 参数dir为无效的目录流

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getFiles(char *path, vector<string>& files){
struct dirent *dirp;
DIR* dir = opendir(path);
while ((dirp = readdir(dir)) != nullptr) {
if (dirp->d_type == DT_REG) {
// 文件
printf("%s\n", dirp->d_name);
} else if (dirp->d_type == DT_DIR) {
// 文件夹
// getFiles(dirp->d_name, files); //迭代
}
}
closedir(dir);
}

命名空间

教程

面向对象

资源库

STL

标准库

<time.h>

struct timespec

1
2
3
4
5
6
7
8
typedef long time_t;
#ifndef _TIMESPEC
#define _TIMESPEC
struct timespec {
time_t tv_sec; // seconds
long tv_nsec; // nanoseconds, 纳秒
};
#endif

一般由函数int clock_gettime(clock id_t, struct timespec*)获取特定时钟的时间

  • 常用clock有如下4种:

    • CLOCK_REALTIME 统当前时间,从1970年1.1日算起
    • CLOCK_MONOTONIC 系统的启动时间,不能被设置
    • CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
    • CLOCK_THREAD_CPUTIME_ID 本线程运行时间
  • 函数:

    1
    2
    3
    struct tm* localtime(const time_t* clock);  //线程不安全
    struct tm* localtime_r(const time_t* timer, struct tm* result );//线程安全
    size_t strftime(char* ptr, size_t maxsize, const char* format, const struct tm* timeptr );

struct timeval

1
2
3
4
5
6
7
8
9
struct timeval {
time_t tv_sec; // seconds
long tv_usec; // microseconds
};

struct timezone{
int tz_minuteswest; //miniutes west of Greenwich
int tz_dsttime; //type of DST correction
};

一般由函数int gettimeofday(struct timeval* tv, struct timezone* tz)获取系统的时间

  • Post title:C++
  • Post author:Wei Jieyang
  • Create time:2021-01-23 18:13:55
  • Post link:https://jieyang-wei.github.io/2021/01/23/C++/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.