QT MSVC中文编译出错error: C2001: 常量中有换行符 — 解决方案

一直提示 warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失. 这如何忍的了

warning: C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失

error: C2001: 常量中有换行符

error: C2146: 语法错误: 缺少“)”(在标识符“label”的前面)

error: C2146: 语法错误: 缺少“;”(在标识符“label”的前面)

直接百度搜解决方案 如下:
在msvc编译器下的include/QtCore里修改qglobal.h文件
添加

#pragma execution_character_set("utf-8")

编译mysql for Qt 5.13.1驱动

windows下载qt-everywhere-src-5.13.1.zip源码包

编译mysql for Qt驱动
1.编辑源码:D:\qt-everywhere-src-5.13.1\qtbase\src\plugins\sqldrivers\mysql\mysql.pro

构建项目成功后在D:/plugins/sqldrivers下有
qsqlmysql.dll
qsqlmysql.lib
qsqlmysqld.dll
qsqlmysqld.lib

1、把qt生成的*.dll放入到qt的安装目录下
E:\Qt\Qt5.13.1\5.13.1\msvc2017\plugins\sqldrivers
2、把mysql的dll库D:\mysql-5.7.27-win32\lib下的*.dll复制到qt安装目录下的E:\Qt\Qt5.13.1\5.13.1\msvc2017\bin目录里
否则会报 “Driver not loaded Driver not loaded”

c++ 解决TCP粘包问题

#include < iostream>
#include < Windows.h>
#include < vector>
#include < string>
#include < algorithm>
#include < time.h>
#include < exception>

#pragma comment(lib,"ws2_32.lib")
using namespace std;

WSADATA wsaData;
SOCKET hSocket;
SOCKADDR_IN servAddr;

size_t readn(SOCKET socket, void* buf, size_t count)
{
	int left = count; //剩下的字节
	char* ptr = (char*)buf;
	while (left > 0)
	{

		int readBytes = recv(socket, ptr, left, 0);
		if (readBytes < 0)//read函数小于0有两种情况:1中断 2出错
		{
			if (errno == EINTR)//读被中断
			{
				continue;
			}
			return -1;
		}
		if (readBytes == 0)//读到了EOF
		{
			// 服务器关闭
			return count - left;
		}
		left -= readBytes;
		ptr += readBytes;
	}
	return count;
}

/// 从缓冲区中读取指定长度的数据,但不清除缓冲区内容
size_t read_peek(int sockfd, char *buf, size_t len)
{
	while (1)
	{
		int ret = recv(sockfd, buf, len, MSG_PEEK);
		if (ret == 0)
		{
			std::cout << "服务器连接断开!!!" << std::endl;
			return 0;
		}
		if (ret == -1 && errno == EINTR)
		{
			//出现中断
			continue;
		}
		return ret;
	}
}

/// 按行读取数据
/// sockfd : 套接字
/// buf :应用层缓冲区,保存读取到的数据
/// maxline : 所规定的一行的长度
size_t readLine(int sockfd, void *buf, size_t maxline)
{
	int ret;
	int nRead = 0;
	int left = maxline; //剩下的字节数
	char * pbuf = (char *)buf;
	int count = 0;
	while (1)
	{
		ret = read_peek(sockfd, pbuf, left);   //读取长度为left的socket缓冲区内容
		if (ret <= 0)
		{
			return ret;
		}
		nRead = ret;
		for (int i = 0; i < nRead; i++)//看看读取出来的数据中是否有换行符\n
		{
			if (pbuf[i] == '\n')//如果有换行符
			{
				ret = readn(sockfd, pbuf, i + 1);//读取一行
				if (ret != i + 1)	//一定会读到i+1个字符,否则是读取出错
				{
					exit(EXIT_FAILURE);
				}
				return ret + count;
			}
		}
		
		//窥探的数据中并没有换行符
		//把这段没有\n的读出来
		ret = readn(sockfd, pbuf, nRead);
		if (ret != nRead)
		{
			exit(EXIT_FAILURE);
		}
		pbuf += nRead;
		left -= nRead;
		count += nRead;
	}
	return -1;
}

int main()
{
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
	{
		cout << "WSAStartup() error" << endl;
		return -1;
	}

	hSocket = socket(PF_INET, SOCK_STREAM, 0);
	if (hSocket == INVALID_SOCKET)
	{
		cout << "hSocket() error" << endl;
		return -1;
	}

	memset(&servAddr, 0, sizeof(servAddr));
	servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	servAddr.sin_family = AF_INET;
	servAddr.sin_port = htons(8888);

	if (connect(hSocket, (SOCKADDR*)& servAddr, sizeof(SOCKADDR)))
	{
		cout << "连接失败!" << endl;
		closesocket(hSocket);
		WSACleanup();

		return -1;
	}

	while (true)
	{
		int ret = readLine(hSocket, recvbuf, 1024);
		if (ret == -1)
		{
			cerr << "readline" << endl;
		}
		else if (ret == 0)
		{
			printf("service closed\n");
			break;
		}
		string ttt = recvbuf;
		cout << ttt.substr(0, ret);
		memset(recvbuf, 0, sizeof(recvbuf));
	}
	return 0;
}

c++ hiredis

#include <cstdio>
#include <string.h>
#include <hiredis/hiredis.h>

using namespace std;

int main()
{
    redisContext *conn = redisConnect("127.0.0.1", 6379);
    if (conn != NULL && conn->err)
    {
        printf("connection error: %s\n", conn->errstr);
        return 0;
    }

    redisReply *reply;
    for (int i=0;i<10000;i++)
    {
        reply = (redisReply*)redisCommand(conn, "publish mychannel test:{\"username\":\"BlackMamba\",\"password\":123456}"); // publish message 中不能有空格
        freeReplyObject(reply);
    }
    
    redisFree(conn);
    return 0;
}

//g++ -o redis Redis.cpp -lhiredis -levent

c++11 json 处理

c++11 json的库 https://github.com/dropbox/json11

tcp接收json字符串并处理

void *ThreadTcp(void *ptr)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[4096];
int n;

if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(9060);

if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
printf("bind socket error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}

if( listen(listenfd, 10) == -1){
printf("listen socket error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}

printf("======waiting for client's request======\n");
while(1){
if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){
printf("accept socket error: %s(errno: %d)", strerror(errno), errno);
continue;
}
cout << connfd << endl; n = recv(connfd, buff, MAXLINE, 0); buff[n] = '\0'; //printf("recv msg from client: %s\n", buff); string err_comment; auto recv_data = Json::parse(buff, err_comment); Json::array account_list = recv_data.array_items(); for (auto &account_data : account_list) { cout << "account: " << account_data["account"].string_value() << endl; cout << "password: " << account_data["password"].string_value() << endl; } //close(connfd); } close(listenfd); return 0; }

c++ 磁盘读写

#include <iostream>
#include <string>
#include <fstream>

int main()
{
	string instId = "00001";
	double price = 3500;

	ofstream fout("test.txt", ios::app);
	fout << "合约代码:" << instId.c_str() << " 价格:" << price << endl;
	fout << "合约代码:" << instId.c_str() << " 价格:" << price + 50 << endl;
	fout.close();
	return 0;
}

int main()
{
	string instId = "00001";
	double price = 5000;

	ofstream fout("test.csv", ios::app);
	fout << "合约代码:" << instId.c_str() << ",价格:" << price << endl;
	fout << "合约代码:" << instId.c_str() << ",价格:" << price + 50 << endl;

	fout.close();
	return 0;
}