知识补充
目录
编辑
makefile的实现
类型转化:
输出输入型参数:
read和write
makefile的实现
一个makefile 默认从上到下只生成一个可执行文件,可以通过在最前面定义一个目标文件all ,而all 的依赖列表是两个可执行的方式来生成两个可执行 但是没有写all的依赖列表 ,all不会产生可执行程序
SERVER=server
CLIENT=client
cc=g++
SERVER_SRC=Server.cc
CLIENT_SRC=Client.cc
.PHONY:all
all:$(SERVER) $(CLIENT)
$(SERVER):$(SERVER_SRC)
$(cc) -o $@ $^ -std=c++11
$(CLIENT):$(CLIENT_SRC)
$(cc) -o $@ $^ -std=c++11
.PHONY:clean
clean:
rm -f $(SERVER) $(CLIENT)
类型转化:
字符串转整形 std::stoi()
#include <string> std::string str = "12345"; int num = std::stoi(str); // 字符串转int
整形转字符串 std::to_string()
#include <string> int num = 42; double pi = 3.14159; std::string s1 = std::to_string(num); // "42" std::string s2 = std::to_string(pi); // "3.141590"
std::string转 const char* 用string中的c_str() 一般传参时经常用
输出输入型参数:
std::string *: 输出型参数
const std::string &: 输入型参数
std::string &: 输入输出型参数
read和write
- write(fd ,str, strlen(str)) 不会将\0读入 ,strlen中不含\0
- read如果读的是字符串想打印 ,需要预留一个位置再最后加\0 ,再进行打印 , 读的数据如果不想打印 ,不用预留位置
- wirte和read都是数据流,它们的行为是严格的字节级原始数据写入和读出,完全按照用户指定的内容和长度进行操作
const char *str = "hello";
write(fd, str, strlen(str)); // 写入 5 个字节 ('h', 'e', 'l', 'l', 'o'),不含 '\0'