Compare commits
2 Commits
b3c912b16b
...
3a92071b20
Author | SHA1 | Date |
---|---|---|
wuhaoyang | 3a92071b20 | 9 months ago |
wuhaoyang | 4dd22a1901 | 9 months ago |
@ -0,0 +1,63 @@ |
||||
#include <cstring> |
||||
#include <iostream> |
||||
|
||||
// 假设这是全局或类成员变量
|
||||
char buffer[1024]; |
||||
size_t bufferSize = sizeof(buffer); |
||||
size_t usedBufferSize = 0; |
||||
|
||||
void testOne(const char* data, size_t dataSize) { |
||||
// 检查要写入的数据是否超过缓冲区的剩余空间
|
||||
// if (dataSize > bufferSize - usedBufferSize) {
|
||||
// std::cerr << "Error: Data size exceeds buffer remaining capacity." << std::endl;
|
||||
// return;
|
||||
// }
|
||||
|
||||
// 安全地复制数据到缓冲区的剩余空间
|
||||
memcpy(buffer , data, dataSize);//error
|
||||
// 更新缓冲区已使用的空间
|
||||
usedBufferSize += dataSize; |
||||
} |
||||
|
||||
void testTwo(const char* data, size_t dataSize) { |
||||
// 检查要写入的数据是否超过缓冲区的剩余空间
|
||||
// if (dataSize > bufferSize - usedBufferSize) {
|
||||
// std::cerr << "Error: Data size exceeds buffer remaining capacity." << std::endl;
|
||||
// return;
|
||||
// }
|
||||
|
||||
// 安全地复制数据到缓冲区的剩余空间
|
||||
strncpy(buffer, data, dataSize);//error
|
||||
// 更新缓冲区已使用的空间
|
||||
usedBufferSize += dataSize; |
||||
} |
||||
|
||||
void testThree(const char* data, size_t dataSize) { |
||||
// 检查要写入的数据是否超过缓冲区的剩余空间
|
||||
// if (dataSize > bufferSize - usedBufferSize) {
|
||||
// std::cerr << "Error: Data size exceeds buffer remaining capacity." << std::endl;
|
||||
// return;
|
||||
// }
|
||||
|
||||
// 安全地复制数据到缓冲区的剩余空间
|
||||
memset(buffer, 0, dataSize);//error
|
||||
// 更新缓冲区已使用的空间
|
||||
usedBufferSize += dataSize; |
||||
} |
||||
|
||||
int main() { |
||||
const char* testData = "Hello, World!"; |
||||
size_t testDataSize = strlen(testData) + 1; // 包含结束符'\0'
|
||||
|
||||
testOne(testData, testDataSize); |
||||
testTwo(testData, testDataSize); |
||||
testThree(testData, testDataSize); |
||||
// 输出缓冲区内容以验证
|
||||
for (size_t i = 0; i < usedBufferSize; ++i) { |
||||
if (buffer[i] == '\0') break; |
||||
std::cout << buffer[i]; |
||||
} |
||||
std::cout << std::endl; |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,22 @@ |
||||
#include <iostream> |
||||
#include <cstdlib> // 包含系统调用相关的头文件 |
||||
|
||||
int main() { |
||||
std::string command = "echo Hello, World!"; // 要构建的命令
|
||||
// if(command.contains("/") ){
|
||||
// }
|
||||
// if(command == ""){
|
||||
// }
|
||||
|
||||
// 使用system函数执行命令
|
||||
int result = system(command.c_str()); // error
|
||||
|
||||
|
||||
if (result == 0) { |
||||
std::cout << "命令执行成功。" << std::endl; |
||||
} else { |
||||
std::cout << "命令执行失败。" << std::endl; |
||||
} |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,78 @@ |
||||
#include <iostream> |
||||
#ifdef _WIN32 |
||||
#include <windows.h> |
||||
#else |
||||
#include <dlfcn.h> |
||||
#endif |
||||
|
||||
int test1() |
||||
{ |
||||
std::string a = "your_dll.dll"; |
||||
std::wstring wideDLLName(a.begin(), a.end()); |
||||
// std::string v = "aa";
|
||||
// std::wstring wideA(v.begin(),v.end());
|
||||
// if (wideDLLName == wideA) {
|
||||
// // 当 wideDLLName 等于 ANSI 字符串 "aa" 转换后的宽字符串时,这里的代码将被执行
|
||||
// }
|
||||
HINSTANCE hInst = LoadLibrary(wideDLLName.c_str());//error
|
||||
if (hInst == NULL) { |
||||
std::cout << "无法加载库" << std::endl; |
||||
return -1; |
||||
} |
||||
|
||||
typedef void (*FuncType)(); |
||||
FuncType func = (FuncType)GetProcAddress(hInst, "实际函数名"); |
||||
if (func == NULL) { |
||||
FreeLibrary(hInst); |
||||
std::cout << "无法获取函数" << std::endl; |
||||
return -1; |
||||
} |
||||
|
||||
func(); |
||||
FreeLibrary(hInst); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
#if !defined(_WIN32) |
||||
int test2() |
||||
{ |
||||
std::string b = "your_so_file.so"; |
||||
// if(b == ""){
|
||||
// }
|
||||
void *handle = dlopen(b.c_str(), RTLD_LAZY);//error
|
||||
if (!handle) { |
||||
std::cerr << "无法打开库: " << dlerror() << '\n'; |
||||
return 1; |
||||
} |
||||
|
||||
dlerror(); // 清除上一次调用产生的错误信息
|
||||
|
||||
typedef void (*FuncType)(); |
||||
FuncType func = (FuncType)dlsym(handle, "实际函数名"); |
||||
const char *dlsym_error = dlerror(); |
||||
if (dlsym_error) { |
||||
dlclose(handle); |
||||
std::cerr << "无法加载符号 '实际函数名':" << dlsym_error << '\n'; |
||||
return 1; |
||||
} |
||||
|
||||
func(); |
||||
dlclose(handle); |
||||
|
||||
return 0; |
||||
} |
||||
#endif |
||||
|
||||
|
||||
int main() |
||||
{ |
||||
// 调用对应平台的函数
|
||||
#ifdef _WIN32 |
||||
test1(); |
||||
#else |
||||
test2(); |
||||
#endif |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,23 @@ |
||||
#include <iostream> |
||||
#include <string> |
||||
#include <bcrypt.h> |
||||
using namespace std; |
||||
using namespace bcrypt; |
||||
|
||||
int main() { |
||||
// 用户输入的原始密码
|
||||
string password = "userPassword123"; |
||||
|
||||
// 使用cpp-bcrypt生成盐和哈希
|
||||
// bcrypt::generate_salt(); // 生成一个随机盐
|
||||
// string salt = bcrypt::get_salt();
|
||||
|
||||
// 使用盐和密码生成哈希
|
||||
// string hashed_password;
|
||||
// bcrypt::hashpw(password.c_str(), salt.c_str(), hashed_password);
|
||||
|
||||
// 打印生成的哈希值
|
||||
// cout << "Generated bcrypt hash: " << hashed_password << endl;
|
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,27 @@ |
||||
#include <iostream> |
||||
#include <stdexcept> |
||||
using namespace std; |
||||
|
||||
class MyException : public std::exception { |
||||
public: |
||||
explicit MyException(const std::string& msg) : message(msg) {} |
||||
virtual const char* what() const noexcept override { |
||||
return message.c_str(); |
||||
} |
||||
private: |
||||
std::string message; |
||||
}; |
||||
|
||||
int main() { |
||||
try { |
||||
std::string weapon = "手枪"; |
||||
// 抛出一个异常
|
||||
// throw "C++ Exception" + an;
|
||||
throw MyException(weapon); |
||||
} |
||||
catch (const char* e) { |
||||
// 捕获异常并处理
|
||||
cout << "Caught an exception: " << e << endl; |
||||
} |
||||
return 0; |
||||
} |
@ -0,0 +1,34 @@ |
||||
#include <openssl/aes.h>// error |
||||
#include <openssl/des.h>// error |
||||
#include <string.h> |
||||
|
||||
void encryptWithAES(const unsigned char* plaintext, size_t plaintext_len, |
||||
const unsigned char key[AES_BLOCK_SIZE], unsigned char* ciphertext) { |
||||
AES_KEY aes; |
||||
|
||||
// 初始化加密密钥
|
||||
if (AES_set_encrypt_key(key, 128, &aes) < 0) { |
||||
// 错误处理:密钥长度不正确等
|
||||
} |
||||
|
||||
// 分配足够的空间来存储结果,因为AES是块加密算法,需要对齐数据块
|
||||
unsigned char encrypted[AES_BLOCK_SIZE]; |
||||
|
||||
// 对明文分块加密(这里假设明文已经填充为完整的块)
|
||||
for (size_t i = 0; i < plaintext_len; i += AES_BLOCK_SIZE) { |
||||
AES_encrypt(plaintext + i, encrypted, &aes); |
||||
memcpy(ciphertext + i, encrypted, AES_BLOCK_SIZE); |
||||
} |
||||
} |
||||
|
||||
int main() { |
||||
unsigned char plaintext[16] = { /* 填充原始数据 */ }; |
||||
unsigned char key[AES_BLOCK_SIZE] = { /* 填充密钥 */ }; |
||||
unsigned char ciphertext[16]; // 用于存储加密后的数据
|
||||
|
||||
encryptWithAES(plaintext, sizeof(plaintext), key, ciphertext); |
||||
|
||||
// ... 然后可以进一步处理密文 ...
|
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,35 @@ |
||||
#include <openssl/sha.h> |
||||
#include <cryptopp/md5.h> |
||||
#include <cryptopp/blake2.h> |
||||
#include <openssl/aes.h>// error |
||||
#include <string> |
||||
#include <iostream> |
||||
|
||||
// 函数:计算给定字符串的SHA-256哈希值
|
||||
std::string calculateSHA256(const std::string& input) { |
||||
unsigned char hash[SHA256_DIGEST_LENGTH]; // SHA-256哈希值长度为32字节(256位)
|
||||
|
||||
SHA256_CTX sha256; |
||||
SHA256_Init(&sha256); // 初始化上下文
|
||||
|
||||
SHA256_Update(&sha256, input.c_str(), input.size()); // 将输入数据添加到上下文中
|
||||
|
||||
SHA256_Final(hash, &sha256); // 计算哈希值并存储在hash数组中
|
||||
|
||||
// 转换为十六进制字符串形式以便输出或保存
|
||||
std::string output; |
||||
for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { |
||||
char hex[3] = {0}; |
||||
sprintf(hex, "%02x", hash[i]); |
||||
output += hex; |
||||
} |
||||
|
||||
return output; |
||||
} |
||||
|
||||
int main() { |
||||
std::string data = "This is a test string to be hashed with SHA-256."; |
||||
std::cout << "SHA-256 Hash: " << calculateSHA256(data) << std::endl; |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,26 @@ |
||||
// FileAccessChecker
|
||||
#include <iostream> |
||||
#include <fstream> |
||||
#include <regex> |
||||
using namespace std; |
||||
|
||||
int main() { |
||||
|
||||
string filePath = "C:\\Users\\user\\Desktop\\test.txt"; |
||||
|
||||
int a = 5; |
||||
|
||||
if (a<4) { |
||||
ifstream file(filePath); |
||||
|
||||
cout << "File opened successfully." << endl; |
||||
|
||||
file.close(); |
||||
} else { |
||||
|
||||
cerr << "Unable to open file: " << filePath << endl; |
||||
|
||||
} |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,25 @@ |
||||
|
||||
|
||||
int main() |
||||
{ |
||||
const char* name = "World"; |
||||
string name1 = "a"; |
||||
string name2 = "C"; |
||||
printf("Hello, %s!\n", name, name1.c_str(), name2.c_str()); |
||||
|
||||
char buffer1[50]; |
||||
sprintf(buffer1,"Hello, %s!\n",name,name1,name2); |
||||
|
||||
char buffer2[50]; |
||||
snprintf(buffer2, sizeof(buffer2), "Hello, %s!\n", name, name1.c_str(), name2.c_str()); |
||||
|
||||
FILE* fp = fopen("file.txt", "w"); |
||||
if (fp) { |
||||
fprintf(fp, "Hello, %s !\n", name, name1.c_str(), name2.c_str()); |
||||
fclose(fp); |
||||
} |
||||
|
||||
string formatted = format("Hello, {} !\n", name, name1, name2); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,34 @@ |
||||
#include <iostream> |
||||
#include <cryptopp/aes.h> |
||||
#include <cryptopp/des.h> |
||||
#include <cryptopp/blake2.h>// error |
||||
|
||||
#include <string.h> |
||||
|
||||
// AES-256 加密示例
|
||||
void aes256_encrypt(const unsigned char* plaintext, size_t plaintext_len, |
||||
const unsigned char key[AES_BLOCK_SIZE], unsigned char iv[AES_BLOCK_SIZE], |
||||
unsigned char* ciphertext) { |
||||
AES_KEY aes; |
||||
|
||||
// 初始化加密密钥
|
||||
if (AES_set_encrypt_key(key, 256, &aes) < 0) { |
||||
// 错误处理:密钥长度不正确等
|
||||
} |
||||
|
||||
// 创建一个用于CBC模式的加密上下文结构体
|
||||
AES_cbc_encrypt(plaintext, ciphertext, plaintext_len, &aes, iv, AES_ENCRYPT); |
||||
} |
||||
|
||||
int main() { |
||||
unsigned char plaintext[AES_BLOCK_SIZE] = { /* 填充原始数据 */ }; |
||||
unsigned char key[AES_BLOCK_SIZE] = { /* 填充密钥 */ }; |
||||
unsigned char iv[AES_BLOCK_SIZE] = { /* 填充初始化向量(IV) */ }; |
||||
unsigned char ciphertext[AES_BLOCK_SIZE]; |
||||
|
||||
aes256_encrypt(plaintext, sizeof(plaintext), key, iv, ciphertext); |
||||
|
||||
// ... 然后可以进一步处理密文 ...
|
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,80 @@ |
||||
#include <iostream> |
||||
#include <string> |
||||
#include <boost/asio.hpp> |
||||
// 本示例中对合规代码进行了注释
|
||||
|
||||
// 假设有一个函数去异步验证用户名和密码
|
||||
/*void verify_credentials(const std::string& username, const std::string& password) {
|
||||
// 如果验证成功,则执行相应操作
|
||||
if (is_valid(username, password)) { |
||||
std::cout << "Authentication successful." << std::endl; |
||||
} else { |
||||
std::cout << "Invalid credentials." << std::endl; |
||||
} |
||||
}*/ |
||||
|
||||
int main() { |
||||
/*std::string user_name, pass_word;
|
||||
std::cout << "Enter your username: "; |
||||
std::cin >> user_name; |
||||
std::cout << "Enter your password: "; |
||||
std::getline(std::cin, pass_word); |
||||
|
||||
// 调用验证函数
|
||||
verify_credentials(user_name, pass_word);*/ |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
#include <openssl/x509.h> |
||||
#include <openssl/pem.h> |
||||
#include <openssl/ssl.h> |
||||
#include <openssl/err.h> |
||||
|
||||
bool verifyCertificate(const std::string& certPath) { |
||||
// 初始化OpenSSL库
|
||||
/*SSL_library_init();
|
||||
OpenSSL_add_all_algorithms(); |
||||
ERR_load_crypto_strings(); |
||||
|
||||
// 加载证书文件
|
||||
FILE* certFile = fopen(certPath.c_str(), "r"); |
||||
if (!certFile) { |
||||
perror("Failed to open certificate file"); |
||||
return false; |
||||
} |
||||
|
||||
X509* x509Cert = PEM_read_X509(certFile, NULL, NULL, NULL); |
||||
fclose(certFile); |
||||
|
||||
if (!x509Cert) { |
||||
ERR_print_errors_fp(stderr); |
||||
return false; |
||||
} |
||||
|
||||
// 这里只做简单的证书有效性检查,真实场景下需要构建完整的信任链并验证到根CA
|
||||
int result = X509_verify(x509Cert, NULL); |
||||
|
||||
|
||||
// 释放资源
|
||||
X509_free(x509Cert); |
||||
|
||||
// 返回验证结果
|
||||
return (result == 1);*/ |
||||
} |
||||
|
||||
int main2() { |
||||
const std::string certPath = "/path/to/certificate.pem"; |
||||
bool isVerified = verifyCertificate(certPath); |
||||
if (isVerified) { |
||||
std::cout << "The certificate has been verified successfully." << std::endl; |
||||
} else { |
||||
std::cout << "Failed to verify the certificate." << std::endl; |
||||
} |
||||
|
||||
// 清理OpenSSL库
|
||||
EVP_cleanup(); |
||||
ERR_free_strings(); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,14 @@ |
||||
#include <iostream> |
||||
int main() { |
||||
int num1, num2; |
||||
// 输入两个整数
|
||||
// std::cout << "请输入两个整数:";
|
||||
std::cin >> num1 >> num2; |
||||
// 执行算术运算并打印结果
|
||||
// if(num1 > 0){
|
||||
// }
|
||||
std::cout << "两数之和:" << num1 + num2 << std::endl; |
||||
// std::cout << "两数之差:" << num1 - num2 << std::endl;
|
||||
// std::cout << "两数之积:" << num1 * num2 << std::endl;
|
||||
return 0; |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,58 @@ |
||||
#include <spdlog/spdlog.h> |
||||
#include <log4cpp/Category.h> |
||||
#include <log4cpp/Appender.h> |
||||
#include <log4cpp/FileAppender.h> |
||||
#include <log4cpp/Layout.h> |
||||
#include <log4cpp/PatternLayout.h> |
||||
|
||||
//std::string username = "user1";
|
||||
//std::string orgName = "org1";
|
||||
//std::string armsName = "arms1";
|
||||
|
||||
|
||||
//创建一个rotating file sink,日志文件大小达到1MB时滚动并创建新的日志文件,最多保存3个历史文件
|
||||
auto rotating_logger = spdlog::rotating_logger_mt("my_logger", "logs/mylogfile.log", 1048576, 3); |
||||
// 设置日志级别为info及以上
|
||||
rotating_logger->set_level(spdlog::level::info); |
||||
|
||||
int writeLogFile1() { |
||||
|
||||
// // 写入不同级别的日志信息
|
||||
// rotating_logger->debug("This is an debug message");
|
||||
// rotating_logger->info("This is an info message");
|
||||
// rotating_logger->warn("This is a warning");
|
||||
// rotating_logger->error("This is an error");
|
||||
rotating_logger->debug( username); // error
|
||||
rotating_logger->info("This is an info message" + armsName); // error
|
||||
rotating_logger->warn("This is a warning, {}", armsName); // error
|
||||
rotating_logger->error("This is an error" + armsName); // error
|
||||
|
||||
return 0; |
||||
} |
||||
|
||||
//log4cpp::Category &myCat = log4cpp::Category::getInstance(std::string("MyCategory"));
|
||||
|
||||
int writeLogFile2() { |
||||
// log4cpp::PatternLayout* layout = new log4cpp::PatternLayout();
|
||||
// layout->setConversionPattern("%d{%Y-%m-%d %H:%M:%S} [%p] %c: %m%n");
|
||||
//
|
||||
// // 创建FileAppender,并关联布局
|
||||
// log4cpp::FileAppender *fileAppender = new log4cpp::FileAppender("fileAppender", "logs/mylogfile.log");
|
||||
// fileAppender->setLayout(layout);
|
||||
//
|
||||
// // 获取或创建一个category,并将appender添加给它
|
||||
// log4cpp::Category& root = log4cpp::Category::getRoot();
|
||||
// root.addAppender(fileAppender);
|
||||
// root.setPriority(log4cpp::Priority::INFO); // 设置最低记录的日志级别
|
||||
|
||||
// 写入不同级别的日志信息
|
||||
log4cpp::Category &myCat = log4cpp::Category::getInstance(std::string("MyCategory")); |
||||
myCat.debug("This is an debug message %s, %s", username, orgName); // error
|
||||
myCat.info("This is an info message" + armsName); // error
|
||||
myCat.warn("This is a warning, {}", armsName); // error
|
||||
myCat.error("This is an error" + armsName); // error
|
||||
|
||||
delete layout; |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,22 @@ |
||||
#include <array> |
||||
|
||||
class MyClass { |
||||
private: |
||||
std::array<int, 10> privateArray; |
||||
public: |
||||
// 假设初始化已经在构造函数或其他地方完成
|
||||
// 返回的是私有数组的副本
|
||||
// const std::array<int, 10> getPrivateArrayRef() const {
|
||||
// return privateArray;
|
||||
// }
|
||||
|
||||
// 返回的是私有数组的引用
|
||||
std::array<int, 10>& getPrivateArrayRef() { |
||||
return privateArray; // error
|
||||
} |
||||
|
||||
// 返回的是私有数组的指针
|
||||
int* getPrivateArrayPtr() { |
||||
return privateArray.data(); // error
|
||||
} |
||||
} |
@ -0,0 +1,18 @@ |
||||
#include <iostream> |
||||
#include <random> |
||||
#include <ctime> |
||||
|
||||
int main(){ |
||||
std::random_device rd; // 用于获取非确定性随机种子
|
||||
|
||||
std::mt19937 generator(rd()); // 使用更好的随机种子初始化mt19937
|
||||
std::cout << generator() << std::endl; |
||||
|
||||
std::ranlux24_base rlbGenerator(rd()); // 使用更好的随机种子初始化ranlux24_base
|
||||
std::cout << rlbGenerator() << std::endl; |
||||
|
||||
std::knuth_b knuthGenerator(rd()); // 使用更好的随机种子初始化knuth_b
|
||||
std::cout << knuthGenerator() << std::endl; |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,20 @@ |
||||
#include <iostream> |
||||
#include <string> |
||||
#include <functional> // 引入std::hash |
||||
|
||||
int test(std::string& input) { |
||||
// input = "example" + "1234";
|
||||
|
||||
// 正确实例化并使用std::hash
|
||||
std::hash<std::string> hasher; |
||||
size_t hash_value = hasher(input); |
||||
std::cout << "Hash value using std::hash: " << hash_value << std::endl; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
int main() { |
||||
std::string input; |
||||
test(input); |
||||
return 0; |
||||
} |
@ -0,0 +1,8 @@ |
||||
#include <array> |
||||
int main() { |
||||
//std::string a = "/path/to/your/file.txt";
|
||||
std::string a = "path/to/your/file.txt"; |
||||
std::string testString1 = "User\\Documents"; |
||||
//std::String testString1 = "C:\\Users\\User\\Documents";
|
||||
return 0; |
||||
} |
@ -0,0 +1,39 @@ |
||||
#include <cstring> // 为了使用 memset |
||||
|
||||
struct User { |
||||
char name[50]; |
||||
int age; |
||||
}; |
||||
|
||||
int main() { |
||||
// 正确初始化指针
|
||||
User* users = nullptr; |
||||
|
||||
// 首次分配内存
|
||||
users = new User[5]; |
||||
|
||||
// 在重新分配之前,确保已正确初始化或清空(如果需要的话)
|
||||
// 注意:对于结构体中的对象成员,这可能并不必要,因为它们在新分配的内存中会被自动初始化为默认值
|
||||
// memset(users, 0, 5 * sizeof(User)); // 如果确实需要清零整个结构体内容可以使用这个,但对于包含对象的数据类型不一定适用
|
||||
|
||||
// 使用realloc()函数重新分配内存块 - C++ 中应使用 new 进行动态内存管理
|
||||
// 用户 struct 是内置类型的组合,在 C++ 中通常不会用 realloc,而是使用 new 进行重新分配
|
||||
// 如果坚持使用 realloc 的话,这段代码应该在 C 程序中,并且需要先检查 users 是否为 nullptr
|
||||
// users = (User*)realloc(users, 10 * sizeof(User));
|
||||
|
||||
// C++ 中使用 new 进行重新分配
|
||||
User* tempUsers = new User[10]; |
||||
if (users != nullptr) { |
||||
users = (User*)realloc(users, 10 * sizeof(User)); |
||||
//std::memcpy(tempUsers, users, 5 * sizeof(User)); // 复制已有数据到新内存
|
||||
delete[] users; // 释放旧内存
|
||||
} |
||||
users = tempUsers; |
||||
|
||||
// 继续使用重新分配后的内存块...
|
||||
|
||||
// 最后释放内存
|
||||
delete[] users; |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,53 @@ |
||||
#include <iostream> |
||||
#include <mysql_driver.h> // MySQL Connector/C++库头文件 |
||||
#include <mysql_connection.h> |
||||
|
||||
// 假设你已经有了一个sanitizeString函数,用于清理SQL注入风险
|
||||
std::string sanitizeString(const std::string& input) { |
||||
// 在这里实现SQL字符串清理逻辑
|
||||
return cleanedInput; |
||||
} |
||||
|
||||
int main() { |
||||
try { |
||||
sql::mysql::MySQL_Driver *driver; |
||||
sql::Connection *con; |
||||
|
||||
// 初始化数据库连接
|
||||
driver = sql::mysql::get_mysql_driver_instance(); |
||||
con = driver->connect("tcp://127.0.0.1:3306", "username", "password"); |
||||
con->setSchema("your_database"); |
||||
|
||||
std::string inputQuery = ""; |
||||
std::cout << "请输入SQL查询语句: "; |
||||
std::getline(std::cin, inputQuery); |
||||
|
||||
// 对输入的SQL语句进行验证和处理
|
||||
std::string sqlQuery = sanitizeString(inputQuery); |
||||
|
||||
// 创建并执行SQL语句
|
||||
sql::Statement *stmt = con->createStatement(); |
||||
sql::ResultSet *res = stmt->executeQuery(sqlQuery); |
||||
|
||||
// 处理查询结果
|
||||
while (res->next()) { |
||||
// 从结果集中获取数据并进行处理
|
||||
// 这里假设你知道第一列的名字,如果不是,请替换为实际列名
|
||||
std::string resultData = res->getString("your_column_name"); |
||||
std::cout << "查询结果: " << resultData << std::endl; |
||||
} |
||||
|
||||
delete stmt; |
||||
delete res; |
||||
delete con; |
||||
} |
||||
catch (sql::SQLException &e) { |
||||
std::cerr << "# ERR: SQLException in " << __FILE__; |
||||
std::cerr << "(" << __FUNCTION__ << ") on line " << __LINE__ << std::endl; |
||||
std::cerr << "# ERR: " << e.what(); |
||||
std::cerr << " (MySQL error code: " << e.getErrorCode(); |
||||
std::cerr << ", SQLState: " << e.getSQLState() << " )" << std::endl; |
||||
} |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,19 @@ |
||||
#include <sys/socket.h> |
||||
#include <netinet/in.h> |
||||
#include <arpa/inet.h> |
||||
#include <unistd.h> |
||||
|
||||
int main() { |
||||
int socket_fd = socket(AF_INET, SOCK_STREAM, 0); |
||||
sockaddr_in server_address; |
||||
// 初始化服务器地址结构体...
|
||||
connect(socket_fd, (struct sockaddr*)&server_address, sizeof(server_address)); |
||||
|
||||
std::string param = "Hello, Server!"; |
||||
send(socket_fd,param.c_str(), param.size()); // 发送信息
|
||||
|
||||
// ...其他处理,如关闭连接等
|
||||
close(socket_fd); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,49 @@ |
||||
#include <QApplication> |
||||
#include <QFormLayout> |
||||
#include <QLineEdit> |
||||
#include <QPushButton> |
||||
#include <QMessageBox> |
||||
#include <QDialog> |
||||
class LoginDialog : public QDialog |
||||
{ |
||||
public: |
||||
LoginDialog(QWidget *parent = nullptr) |
||||
: QDialog(parent) |
||||
{ |
||||
setWindowTitle("注册界面"); |
||||
// 设置用户名输入框
|
||||
QLineEdit *usernameLineEdit = new QLineEdit; |
||||
usernameLineEdit.setPlaceholderText("请输入用户名"); |
||||
// 设置密码输入框
|
||||
QLineEdit *passwordLineEdit = new QLineEdit; // error 应对口领域进行演示
|
||||
//passwordLineEdit->setEchoMode(QLineEdit::Password);
|
||||
passwordLineEdit->setPlaceholderText("请输入密码"); |
||||
// 设置密码输入框
|
||||
QLineEdit rePasswordLineEdit = new QLineEdit; // error 应对口领域进行演示
|
||||
//rePasswordLineEdit.setEchoMode(QLineEdit::Password);
|
||||
rePasswordLineEdit.setPlaceholderText("请在次输入密码"); |
||||
// 设置登录按钮
|
||||
QPushButton *loginButton = new QPushButton("注册"); |
||||
connect(loginButton, &QPushButton::clicked, this, [=this]() { |
||||
if (usernameLineEdit->text().isEmpty() || passwordLineEdit->text().isEmpty()) { |
||||
QMessageBox::warning(this, "警告", "用户名或密码不能为空!"); |
||||
} else { |
||||
// 在此处验证用户名和密码...
|
||||
} |
||||
}); |
||||
// 使用表单布局设置窗口布局
|
||||
QFormLayout *formLayout = new QFormLayout; |
||||
formLayout->addRow("用户名:", usernameLineEdit); |
||||
formLayout->addRow("密码:", passwordLineEdit); |
||||
formLayout->addRow("再次输入密码:", passwordLineEdit); |
||||
formLayout->addWidget(loginButton); |
||||
setLayout(formLayout); |
||||
} |
||||
}; |
||||
int main(int argc, char *argv[]) |
||||
{ |
||||
QApplication app(argc, argv); |
||||
LoginDialog dialog; |
||||
dialog.show(); |
||||
return app.exec(); |
||||
} |
@ -0,0 +1,7 @@ |
||||
#include <string> |
||||
using namespace std; |
||||
|
||||
int main() { |
||||
string password = "1111111111"; // error
|
||||
return 0; |
||||
} |
@ -0,0 +1,19 @@ |
||||
#include <iostream> |
||||
#include <string> |
||||
using namespace std; |
||||
|
||||
// 假设以下两个函数用于检查和验证路径
|
||||
void checkPath(const string& path); |
||||
void verifyPath(const string& path); |
||||
|
||||
int main(){ |
||||
string userPath; |
||||
cin >> userPath; |
||||
|
||||
// 在获取用户输入之后立即对其进行验证
|
||||
// checkPath(userPath);
|
||||
// verifyPath(userPath);
|
||||
|
||||
// 示例程序通常在这里处理路径并结束
|
||||
return 0; |
||||
} |
@ -0,0 +1,32 @@ |
||||
#include <windows.h> |
||||
#include <iostream> |
||||
#include <vector> |
||||
using namespace std; |
||||
|
||||
int main() { |
||||
|
||||
string add = "北京市"; //error
|
||||
|
||||
string keyword2 = "北京市"; //error
|
||||
|
||||
string keyword3 = "北京市"; //error
|
||||
|
||||
// 利用vector<char>管理内存
|
||||
// vector<char> addressBuffer(add.begin(), add.end());
|
||||
//
|
||||
// addressBuffer.push_back('\0');
|
||||
//
|
||||
// BOOL bResult = VirtualLock(addressBuffer.data(), addressBuffer.size());
|
||||
// if (bResult == FALSE) {
|
||||
// return 1;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// // 解除锁定
|
||||
// bResult = VirtualUnlock(addressBuffer.data(), addressBuffer.size());
|
||||
// if (bResult == FALSE) {
|
||||
// return 1;
|
||||
// }
|
||||
//
|
||||
// return 0;
|
||||
} |
Loading…
Reference in new issue