DEV Community

海前 王
海前 王

Posted on

wtring

include

include // 包含 MFC 的头文件

int main() {
CString a, b;
a = "X23456789";

b = a.Left(4);
std::wcout << "\n" << b.GetString();

std::wcout << "\n" << a.Left(4).GetString();

b = a.Mid(1);
std::wcout << "\n" << b.GetString();

b = a.Mid(2, 4);
std::wcout << "\n" << b.GetString();

b = a.Right(4);
std::wcout << "\n" << b.GetString();

return 0;
Enter fullscreen mode Exit fullscreen mode

}

// iofile.cpp

include

include

include

include

using namespace std;
static int abc = 0;
int reader()
{
string filename = "D:\叶轮-修改后.NC";
ifstream fin(filename.c_str());
int index = 0;
string strline;
while (getline(fin, strline) )
{
bool exists = strline.find("X3yyyu");
// cout << strline << '\t' << !exists << endl;
if (exists)
{
// cout << "hello world\n";
abc++;//183 s
break;
}else
{
cout << "not exist";
}
index++;
}
fin.close();
cout << "Done!\n";
return 0;
}
int main()
{
clock_t start, end;
start = clock();
reader();
end = clock();
cout << "The time cost is: " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
cout << "this is" << abc;
}

// 使用fread函数与fwrite函数对文件进行操作
// 使用feof文件终止符判断文件是否已经读取完毕。
// 同时可以使用文件的偏移指针确定文件大小来决定变量该为多大

define _CRT_SECURE_NO_WARNINGS

include

include

include

int main() {
FILE* fp;
char buf[1024];

// 打开文件
fp = fopen("D:\\叶轮-修改后.NC", "r");
if (fp == NULL)
{
    printf("open file failed!\n");
    return -1;
}

// 一行一行读取文件内容
while (fgets(buf, 1024, fp) != NULL)
{
    printf("%s", buf);
}

// 关闭文件
fclose(fp);

return 0;
Enter fullscreen mode Exit fullscreen mode

}

include

include

include

include

include

include

int CountLines(const CString& filePath)
{
CFile file;
if (file.Open(filePath, CFile::modeRead | CFile::typeBinary))
{
DWORD fileSize = file.GetLength();
if (fileSize == 0)
{
file.Close();
return 0;
}

    char* buffer = new char[fileSize];
    file.Read(buffer, fileSize);
    file.Close();

    int lineCount = 0;
    char* p = buffer;
    while (p < buffer + fileSize)
    {
        if (*p == '\n')
            lineCount++;
        p++;
    }

    delete[] buffer;
    return lineCount + 1;
}

return 0;
Enter fullscreen mode Exit fullscreen mode

}

int main()
{
CString filePath = _T("your_file_path.txt");
int lineCount = CountLines(filePath);
std::cout << "Total lines in file: " << lineCount << std::endl;

return 0;
Enter fullscreen mode Exit fullscreen mode

}

/*#include

include

include

include

include

include

// 互斥锁,用于保护共享资源
std::mutex mtx;

// 读取文件的线程函数
DWORD WINAPI ReadFileThread(LPVOID lpParam)
{
std::string filePath = reinterpret_cast(lpParam);

// 打开文件
std::ifstream file(filePath, std::ios::binary);
if (!file)
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to open file: " << filePath << std::endl;
    return 0;
}

// 读取文件内容
std::vector<char> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());

// 关闭文件
file.close();

// 在控制台输出读取到的文件内容大小
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Read file: " << filePath << ", Size: " << buffer.size() << " bytes" << std::endl;

return 0;
Enter fullscreen mode Exit fullscreen mode

}

int main()
{
std::vectorstd::string filePaths = {
"D:\叶轮-修改后.NC",
// 添加更多文件路径...
};

// 创建线程句柄数组
std::vector<HANDLE> threadHandles(filePaths.size());

// 创建并启动线程
for (size_t i = 0; i < filePaths.size(); ++i)
{
    threadHandles[i] = CreateThread(nullptr, 0, ReadFileThread, reinterpret_cast<LPVOID>(const_cast<char*>(filePaths[i].c_str())), 0, nullptr);
    if (threadHandles[i] == nullptr)
    {
        std::cout << "Failed to create thread for file: " << filePaths[i] << std::endl;
        continue;
    }
}

// 等待线程完成
WaitForMultipleObjects(static_cast<DWORD>(threadHandles.size()), threadHandles.data(), TRUE, INFINITE);

// 关闭线程句柄
for (size_t i = 0; i < threadHandles.size(); ++i)
{
    CloseHandle(threadHandles[i]);
}

return 0;
Enter fullscreen mode Exit fullscreen mode

include

include

include

include

include

// 互斥锁,用于保护共享资源
std::mutex mtx;

// 读取文件的线程函数
DWORD WINAPI ReadFileThread(LPVOID lpParam)
{
std::string filePath = reinterpret_cast(lpParam);

// 打开文件
HANDLE hFile = CreateFile(filePath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to open file: " << filePath << std::endl;
    return 0;
}

// 获取文件大小
DWORD fileSize = GetFileSize(hFile, nullptr);

// 创建缓冲区
std::vector<char> buffer(fileSize);

// 读取文件内容
DWORD bytesRead;
if (!ReadFile(hFile, buffer.data(), fileSize, &bytesRead, nullptr))
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to read file: " << filePath << std::endl;
    CloseHandle(hFile);
    return 0;
}

// 关闭文件
CloseHandle(hFile);

// 在控制台输出读取到的文件内容大小
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Read file: " << filePath << ", Size: " << bytesRead << " bytes" << std::endl;

return 0;
Enter fullscreen mode Exit fullscreen mode

}

int main()
{
std::vectorstd::string filePaths = {
"D:\叶轮-修改后.NC",
// 添加更多文件路径...
};

// 创建线程句柄数组
std::vector<HANDLE> threadHandles(filePaths.size());

// 创建并启动线程
for (size_t i = 0; i < filePaths.size(); ++i)
{
    threadHandles[i] = CreateThread(nullptr, 0, ReadFileThread, reinterpret_cast<LPVOID>(const_cast<char*>(filePaths[i].c_str())), 0, nullptr);
    if (threadHandles[i] == nullptr)
    {
        std::cout << "Failed to create thread for file: " << filePaths[i] << std::endl;
        continue;
    }
}

// 等待线程完成
WaitForMultipleObjects(static_cast<DWORD>(threadHandles.size()), threadHandles.data(), TRUE, INFINITE);

// 关闭线程句柄
for (size_t i = 0; i < threadHandles.size(); ++i)
{
    CloseHandle(threadHandles[i]);
}

return 0;
Enter fullscreen mode Exit fullscreen mode

}/
/

include

include

include

include

include

// 互斥锁,用于保护共享资源
std::mutex mtx;

// 读取文件的线程函数
DWORD WINAPI ReadFileThread(LPVOID lpParam)
{
std::string filePath = reinterpret_cast(lpParam);

// 将窄字符转换为宽字符
int wideCharLength = MultiByteToWideChar(CP_UTF8, 0, filePath.c_str(), -1, nullptr, 0);
std::vector<wchar_t> wideCharBuffer(wideCharLength);
MultiByteToWideChar(CP_UTF8, 0, filePath.c_str(), -1, wideCharBuffer.data(), wideCharLength);

// 打开文件
HANDLE hFile = CreateFileW(wideCharBuffer.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to open file: " << filePath << std::endl;
    return 0;
}

// 获取文件大小
DWORD fileSize = GetFileSize(hFile, nullptr);

// 创建缓冲区
std::vector<char> buffer(fileSize);

// 读取文件内容
DWORD bytesRead;
if (!ReadFile(hFile, buffer.data(), fileSize, &bytesRead, nullptr))
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to read file: " << filePath << std::endl;
    CloseHandle(hFile);
    return 0;
}

// 关闭文件
CloseHandle(hFile);

// 在控制台输出读取到的文件内容大小
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Read file: " << filePath << ", Size: " << bytesRead << " bytes" << std::endl;

return 0;
Enter fullscreen mode Exit fullscreen mode

}

int main()
{
std::vectorstd::string filePaths = {
"D:\叶轮-修改后.NC",
// 添加更多文件路径...
};

// 创建线程句柄数组
std::vector<HANDLE> threadHandles(filePaths.size());

// 创建并启动线程
for (size_t i = 0; i < filePaths.size(); ++i)
{
    threadHandles[i] = CreateThread(nullptr, 0, ReadFileThread, reinterpret_cast<LPVOID>(const_cast<char*>(filePaths[i].c_str())), 0, nullptr);
    if (threadHandles[i] == nullptr)
    {
        std::cout << "Failed to create thread for file: " << filePaths[i] << std::endl;
        continue;
    }
}

// 等待线程完成
WaitForMultipleObjects(static_cast<DWORD>(threadHandles.size()), threadHandles.data(), TRUE, INFINITE);

// 关闭线程句柄
for (size_t i = 0; i < threadHandles.size(); ++i)
{
    CloseHandle(threadHandles[i]);
}

return 0;
Enter fullscreen mode Exit fullscreen mode

}/
/

include

include

include

include

include

// 互斥锁,用于保护共享资源
std::mutex mtx;

// 读取文件的线程函数
DWORD WINAPI ReadFileThread(LPVOID lpParam)
{
std::string filePath = reinterpret_cast(lpParam);

// 将窄字符转换为宽字符
int wideCharLength = MultiByteToWideChar(CP_UTF8, 0, filePath.c_str(), -1, nullptr, 0);
std::vector<wchar_t> wideCharBuffer(wideCharLength);
MultiByteToWideChar(CP_UTF8, 0, filePath.c_str(), -1, wideCharBuffer.data(), wideCharLength);

// 打开文件
HANDLE hFile = CreateFileW(wideCharBuffer.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to open file: " << filePath << std::endl;
    return 0;
}

// 获取文件大小
DWORD fileSize = GetFileSize(hFile, nullptr);

// 创建缓冲区
std::vector<char> buffer(fileSize);

// 读取文件内容
DWORD bytesRead;
if (!ReadFile(hFile, buffer.data(), fileSize, &bytesRead, nullptr))
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to read file: " << filePath << std::endl;
    CloseHandle(hFile);
    return 0;
}

// 关闭文件
CloseHandle(hFile);

// 统计行数
int lineCount = 0;
for (char c : buffer)
{
    if (c == '\n')
    {
        lineCount++;
    }
}

// 在控制台输出读取到的文件内容大小和行数
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Read file: " << filePath << ", Size: " << bytesRead << " bytes, Line count: " << lineCount << std::endl;

return 0;
Enter fullscreen mode Exit fullscreen mode

}

int main()
{
std::vectorstd::string filePaths = {
"D:\叶轮-修改后.NC",
// 添加更多文件路径...
};

// 创建线程句柄数组
std::vector<HANDLE> threadHandles(filePaths.size());

// 创建并启动线程
for (size_t i = 0; i < filePaths.size(); ++i)
{
    threadHandles[i] = CreateThread(nullptr, 0, ReadFileThread, reinterpret_cast<LPVOID>(const_cast<char*>(filePaths[i].c_str())), 0, nullptr);
    if (threadHandles[i] == nullptr)
    {
        std::cout << "Failed to create thread for file: " << filePaths[i] << std::endl;
        continue;
    }
}

// 等待线程完成
WaitForMultipleObjects(static_cast<DWORD>(threadHandles.size()), threadHandles.data(), TRUE, INFINITE);

// 关闭线程句柄
for (size_t i = 0; i < threadHandles.size(); ++i)
{
    CloseHandle(threadHandles[i]);
}

return 0;
Enter fullscreen mode Exit fullscreen mode

}

include

include

include

include

include

// 互斥锁,用于保护共享资源
std::mutex mtx;

// 读取文件的线程函数
DWORD WINAPI ReadFileThread(LPVOID lpParam)
{
std::string filePath = reinterpret_cast(lpParam);

// 将窄字符转换为宽字符
int wideCharLength = MultiByteToWideChar(CP_UTF8, 0, filePath.c_str(), -1, nullptr, 0);
std::vector<wchar_t> wideCharBuffer(wideCharLength);
MultiByteToWideChar(CP_UTF8, 0, filePath.c_str(), -1, wideCharBuffer.data(), wideCharLength);

// 打开文件
HANDLE hFile = CreateFileW(wideCharBuffer.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to open file: " << filePath << std::endl;
    return 0;
}

// 获取文件大小
DWORD fileSize = GetFileSize(hFile, nullptr);

// 创建缓冲区
std::vector<char> buffer(fileSize);

// 读取文件内容
DWORD bytesRead;
if (!ReadFile(hFile, buffer.data(), fileSize, &bytesRead, nullptr))
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to read file: " << filePath << std::endl;
    CloseHandle(hFile);
    return 0;
}

// 关闭文件
CloseHandle(hFile);

// 统计行数并保存每一行内容
int lineCount = 0;
std::vector<std::string> lines;
std::string currentLine;
for (char c : buffer)
{
    if (c == '\n')
    {
        lineCount++;
        lines.push_back(currentLine);
        currentLine.clear();
    }
    else
    {
        currentLine += c;
    }
}

// 在控制台输出读取到的文件内容大小、行数和每一行的内容
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Read file: " << filePath << ", Size: " << bytesRead << " bytes, Line count: " << lineCount << std::endl;
for (const std::string& line : lines)
{
    std::cout << "Line: " << line << std::endl;
}

return 0;
Enter fullscreen mode Exit fullscreen mode

}

int main()
{
std::vectorstd::string filePaths = {
"D:\叶轮-修改后.NC",
// 添加更多文件路径...
};

// 创建线程句柄数组
std::vector<HANDLE> threadHandles(filePaths.size());

// 创建并启动线程
for (size_t i = 0; i < filePaths.size(); ++i)
{
    threadHandles[i] = CreateThread(nullptr, 0, ReadFileThread, reinterpret_cast<LPVOID>(const_cast<char*>(filePaths[i].c_str())), 0, nullptr);
    if (threadHandles[i] == nullptr)
    {
        std::cout << "Failed to create thread for file: " << filePaths[i] << std::endl;
        continue;
    }
}

// 等待线程完成
WaitForMultipleObjects(static_cast<DWORD>(threadHandles.size()), threadHandles.data(), TRUE, INFINITE);

// 关闭线程句柄
for (size_t i = 0; i < threadHandles.size(); ++i)
{
    CloseHandle(threadHandles[i]);
}

return 0;
Enter fullscreen mode Exit fullscreen mode

}

include

include

include

include

include

// 互斥锁,用于保护共享资源
std::mutex mtx;

// 结构体,保存行数和对应的行内容
struct LineInfo
{
int lineCount;
std::string lineContent;

LineInfo(int count, const std::string& content) : lineCount(count), lineContent(content) {}
Enter fullscreen mode Exit fullscreen mode

};

// 读取文件的线程函数
DWORD WINAPI ReadFileThread(LPVOID lpParam)
{
std::string filePath = reinterpret_cast(lpParam);

// 将窄字符转换为宽字符
int wideCharLength = MultiByteToWideChar(CP_UTF8, 0, filePath.c_str(), -1, nullptr, 0);
std::vector<wchar_t> wideCharBuffer(wideCharLength);
MultiByteToWideChar(CP_UTF8, 0, filePath.c_str(), -1, wideCharBuffer.data(), wideCharLength);

// 打开文件
HANDLE hFile = CreateFileW(wideCharBuffer.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to open file: " << filePath << std::endl;
    return 0;
}

// 获取文件大小
DWORD fileSize = GetFileSize(hFile, nullptr);

// 创建缓冲区
std::vector<char> buffer(fileSize);

// 读取文件内容
DWORD bytesRead;
if (!ReadFile(hFile, buffer.data(), fileSize, &bytesRead, nullptr))
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to read file: " << filePath << std::endl;
    CloseHandle(hFile);
    return 0;
}

// 关闭文件

// 统计行数并保存每一行内容
std::vector<LineInfo> lines;
int lineCount = 0;
std::string currentLine;
for (char c : buffer)
{
    if (c == '\n')
    {
        lineCount++;
        lines.push_back(LineInfo(lineCount, currentLine));
        currentLine.clear();
    }
    else
    {
        currentLine += c;
    }
}

// 在控制台输出读取到的文件内容大小
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Read file: " << filePath << ", Size: " << bytesRead << " bytes" << std::endl;

// 打印行数和内容
for (const LineInfo& line : lines)
{
    std::cout << "Line: " << line.lineCount << ", Content: " << line.lineContent << std::endl;
}
CloseHandle(hFile);

return 0;
Enter fullscreen mode Exit fullscreen mode

}

int main()
{
std::vectorstd::string filePaths = {
"D:\叶轮-修改后.NC",
};

// 创建线程句柄数组
std::vector<HANDLE> threadHandles(filePaths.size());

// 创建并启动线程
for (size_t i = 0; i < filePaths.size(); ++i)
{
    threadHandles[i] = CreateThread(nullptr, 0, ReadFileThread, reinterpret_cast<LPVOID>(const_cast<char*>(filePaths[i].c_str())), 0, nullptr);
    if (threadHandles[i] == nullptr)
    {
        std::cout << "Failed to create thread for file: " << filePaths[i] << std::endl;
        continue;
    }
}

// 等待线程完成
WaitForMultipleObjects(static_cast<DWORD>(threadHandles.size()), threadHandles.data(), TRUE, INFINITE);

return 0;

*/
Enter fullscreen mode Exit fullscreen mode

include

include

include

include

include

include

std::mutex mtx;

struct LineInfo
{
int lineCount;
std::string lineContent;

LineInfo(int count, const std::string& content) : lineCount(count), lineContent(content) {}
Enter fullscreen mode Exit fullscreen mode

};

void ReadFileThread(const std::string& filePath, std::vector& lines)
{
HANDLE hFile = CreateFileA(filePath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
std::lock_guardstd::mutex lock(mtx);
std::cout << "Failed to open file: " << filePath << std::endl;
return;
}

DWORD fileSize = GetFileSize(hFile, nullptr);
std::vector<char> buffer(fileSize);

DWORD bytesRead;
if (!ReadFile(hFile, buffer.data(), fileSize, &bytesRead, nullptr))
{
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << "Failed to read file: " << filePath << std::endl;
    CloseHandle(hFile);
    return;
}

CloseHandle(hFile);

int lineCount = 0;
std::string currentLine;
for (char c : buffer)
{
    if (c == '\n')
    {
        lineCount++;
        lines.push_back(LineInfo(lineCount, currentLine));
        currentLine.clear();
    }
    else
    {
        currentLine += c;
    }
}

std::lock_guard<std::mutex> lock(mtx);
std::cout << "Read file: " << filePath << ", Size: " << bytesRead << " bytes" << std::endl;
Enter fullscreen mode Exit fullscreen mode

}

void fun()
{
std::vectorstd::string filePaths = {
"D:\叶轮-修改后.NC",

};

std::vector<std::vector<LineInfo>> fileContents(filePaths.size());

std::vector<std::thread> threads;
for (size_t i = 0; i < filePaths.size(); ++i)
{
    threads.emplace_back(ReadFileThread, filePaths[i], std::ref(fileContents[i]));
}

for (auto& thread : threads)
{
    thread.join();
}

for (const auto& content : fileContents)
{
    if (!content.empty())
    {
        if (content.size() >= 1000)
        {
            std::cout << "File content (line 1000): " << content[999].lineContent << std::endl;
        }

        if (content.size() >= 3000000)
        {
            std::cout << "File content (line 300000): " << content[2999999].lineContent << std::endl;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}
int main()
{
fun();
return 0;

}

include

include

include

include

include

include

include

include

include

include

if 0

/*
// 使用内存映射文件进行读取
void ReadFileWithMemoryMapping(const std::wstring& filename) {
HANDLE fileHandle = CreateFileW(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fileHandle == INVALID_HANDLE_VALUE) {
std::wcout << L"Failed to open the file." << std::endl;
return;
}

HANDLE mappingHandle = CreateFileMappingW(fileHandle, NULL, PAGE_READONLY, 0, 0, NULL);
if (mappingHandle == NULL) {
    std::wcout << L"Failed to create a file mapping object." << std::endl;
    CloseHandle(fileHandle);
    return;
}

LPVOID fileData = MapViewOfFile(mappingHandle, FILE_MAP_READ, 0, 0, 0);
if (fileData == NULL) {
    std::wcout << L"Failed to map the file into memory." << std::endl;
    CloseHandle(mappingHandle);
    CloseHandle(fileHandle);
    return;
}

// 获取文件大小
DWORD fileSize = GetFileSize(fileHandle, NULL);

// 读取文件内容
std::wstring fileContent(static_cast<const wchar_t*>(fileData), fileSize / sizeof(wchar_t));

// 打印文件内容
std::wcout << L"File content: " << std::endl;
std::wcout << fileContent << std::endl;

// 解除内存映射
UnmapViewOfFile(fileData);
CloseHandle(mappingHandle);
CloseHandle(fileHandle);
Enter fullscreen mode Exit fullscreen mode

}

int main() {
const std::wstring filename = L"D:\叶轮-修改后.NC";
ReadFileWithMemoryMapping(filename);
return 0;
}*/

include

include

int main() {
const char* path = "D:\modify.NC";

// 开始计时
clock_t start = clock();

HANDLE hFile = CreateFileA(path,
   /* GENERIC_READ | GENERIC_WRITE,*/
    0,
    FILE_SHARE_READ|FILE_SHARE_WRITE,
    NULL,
   OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);
if (hFile == INVALID_HANDLE_VALUE) {
    std::cout << "CreateFile fail" << std::endl;
    return -1;
}

// 创建一个文件映射内核对象
HANDLE hFileMap = CreateFileMapping(hFile,
    NULL,
    PAGE_READWRITE,
    NULL,
    1000000,
    0);
if (hFileMap == NULL) {
    std::cout << "CreateFileMapping fail" << std::endl;
    CloseHandle(hFile);
    return -1;
}

// 将文件数据映射到进程的地址空间
char* pMapData = (char*)MapViewOfFile(hFileMap,
    FILE_MAP_ALL_ACCESS,
    NULL,
    NULL,
    NULL);
if (pMapData == NULL) {
    std::cout << "MapViewOfFile fail" << std::endl;
    CloseHandle(hFileMap);
    CloseHandle(hFile);
    return -1;
}

// 读取数据
char* pBuf = pMapData;

// 计时结束
clock_t finish = clock();
printf("读取1G身份证文件的运行时间:%f毫秒\n", (float)(finish - start));

// 撤销文件视图
UnmapViewOfFile(pBuf);

// 关闭映射文件句柄
CloseHandle(hFileMap);
CloseHandle(hFile);

return 0;
Enter fullscreen mode Exit fullscreen mode

}

endif

include

include

int main() {
// 打开文件
HANDLE hFile = CreateFile(L"D:\使用说明.txt", 0,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
std::cout << "Failed to open file" << std::endl;
return 1;
}

// 获取文件大小
DWORD file_size = GetFileSize(hFile, NULL);

// 创建文件映射对象
HANDLE hMapFile = CreateFileMapping(hFile, NULL, PAGE_READWRITE, NULL, file_size, NULL);
if (hMapFile == NULL) {
    std::cout << "Failed to create file mapping" << std::endl;
    CloseHandle(hFile);
    return 1;
}

// 映射文件到内存
LPVOID mapped_file = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, file_size);
if (mapped_file == NULL) {
    std::cout << "Failed to map file into memory" << std::endl;
    CloseHandle(hMapFile);
    CloseHandle(hFile);
    return 1;
}

// 写入数据到映射的内存
const char* data = "Hello, Memory Mapping!";
strcpy_s((char*)mapped_file, file_size, data);

// 刷新文件映射到磁盘
FlushViewOfFile(mapped_file, file_size);

// 输出映射的内存内容
std::cout << "File content: " << (char*)mapped_file << std::endl;

// 取消内存映射
UnmapViewOfFile(mapped_file);

// 关闭文件映射对象和文件句柄
CloseHandle(hMapFile);
CloseHandle(hFile);

return 0;
Enter fullscreen mode Exit fullscreen mode

}

include

include

include

include

include

include

include

//44s
std::mutex mtx; // 互斥锁,用于保护共享数据
std::vectorstd::string lines; // 存储读取的行

// 文件读取线程函数
void ReadFileThread(const std::string& filename, int startLine, int numLines, std::vector& threadTimes) {
std::ifstream file(filename);
if (!file.is_open()) {
std::lock_guardstd::mutex lock(mtx);
std::cout << "Failed to open the file." << std::endl;
return;
}

std::string line;
for (int i = 0; i < startLine; ++i) {
    std::getline(file, line); // 跳过前面的行
}

// 开始计时
auto start = std::chrono::high_resolution_clock::now();

for (int i = 0; i < numLines; ++i) {
    if (std::getline(file, line)) {
        std::lock_guard<std::mutex> lock(mtx); // 获取锁,保护共享数据访问
        lines.push_back(line);
    }
    else {
        break; // 文件读取完毕
    }
}

file.close();

// 停止计时
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = end - start;
threadTimes.push_back(duration.count());
Enter fullscreen mode Exit fullscreen mode

}

// 计算总体读取时间和每个线程的读取时间
void ComputeTotalTime(const std::vector& threadTimes) {
double totalTime = 0.0;
for (double time : threadTimes) {
totalTime += time;
}

std::cout << "Total read time for all threads: " << totalTime << " seconds" << std::endl;

double avgTime = totalTime / threadTimes.size();
std::cout << "Average read time per thread: " << avgTime << " seconds" << std::endl;
Enter fullscreen mode Exit fullscreen mode

}

int main() {
const std::string filename = "D:\叶轮-修改后.NC";
const int numThreads = 4; // 线程数量
std::vectorstd::thread threads;
std::vector threadTimes;

std::ifstream file(filename);
if (!file.is_open()) {
    std::cout << "Failed to open the file." << std::endl;
    return 0;
}

int totalLines = 0;
std::string line;
while (std::getline(file, line)) {
    totalLines++;
}

file.close();

int linesPerThread = totalLines / numThreads;
int remainingLines = totalLines % numThreads;

int startLine = 0;
for (int i = 0; i < numThreads; ++i) {
    int numLines = linesPerThread;
    if (i == numThreads - 1) {
        numLines += remainingLines; // 最后一个线程处理剩余的行数
    }

    threads.emplace_back(ReadFileThread, filename, startLine, numLines, std::ref(threadTimes));
    startLine += numLines;
}

// 等待所有线程执行完毕
for (auto& thread : threads) {
    thread.join();
}

// 输出向量中的行数
std::cout << "Total lines: " << lines.size() << std::endl;

// 计算总体读取时间和每个线程的读取时间
ComputeTotalTime(threadTimes);

return 0;
Enter fullscreen mode Exit fullscreen mode

}

include

include

include

include

include

int main1() {
std::vectorstd::string lines;
std::ifstream file("your_file.txt");

if (file.is_open()) {
    const std::streamsize buffer_size = 4096;
    char* buffer = new char[buffer_size];
    file.rdbuf()->pubsetbuf(buffer, buffer_size);

    std::string line;

    // 开始计时
    auto start = std::chrono::high_resolution_clock::now();

    while (std::getline(file, line)) {
        lines.push_back(line);
    }

    // 停止计时
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> duration = end - start;

    file.close();
    delete[] buffer;

    // 输出向量中的行数和读取时间
    std::cout << "Total lines: " << lines.size() << std::endl;
    std::cout << "Read time: " << duration.count() << " seconds" << std::endl;
}
else {
    std::cout << "Failed to open the file." << std::endl;
}

return 0;
Enter fullscreen mode Exit fullscreen mode

}*/

include

/**
template
class SharedPtr {
private:
T* ptr;
int* refCount;

public:
SharedPtr(T* p = nullptr) : ptr(p), refCount(new int(1)) {}

SharedPtr(const SharedPtr<T>& other) : ptr(other.ptr), refCount(other.refCount) {
    (*refCount)++;
}

SharedPtr<T>& operator=(const SharedPtr<T>& other) {
    if (this != &other) {
        Release();
        ptr = other.ptr;
        refCount = other.refCount;
        (*refCount)++;
    }
    return *this;
}

~SharedPtr() {
    Release();
    std::cout << "hello world";
}

T& operator*() const {
    return *ptr;
}

T* operator->() const {
    return ptr;
}
Enter fullscreen mode Exit fullscreen mode

private:
void Release() {
(*refCount)--;
if (*refCount == 0) {
delete ptr;
delete refCount;
ptr = nullptr;
refCount = nullptr;
}
}
};

class Base {
public:
virtual void Print() const = 0;
};

class Derived : public Base {
public:
void Print() const override {
std::cout << "Derived class" << std::endl;
}
};

class Wrapper {
private:
SharedPtr ptr;

public:
Wrapper(SharedPtr p) : ptr(p) {}

void DoSomething() {

    ptr->Print();
}
Enter fullscreen mode Exit fullscreen mode

};

int main() {
SharedPtr derivedPtr(new Derived());
Wrapper wrapper(derivedPtr);
wrapper.DoSomething();

return 0;
Enter fullscreen mode Exit fullscreen mode

}*/

include

template
class UniquePtr {
private:
T* ptr;

public:
UniquePtr(T* p = nullptr) : ptr(p) {}

UniquePtr(const UniquePtr<T>&) = delete;
UniquePtr<T>& operator=(const UniquePtr<T>&) = delete;

UniquePtr(UniquePtr<T>&& other) noexcept : ptr(other.ptr) {
    other.ptr = nullptr;
}

UniquePtr<T>& operator=(UniquePtr<T>&& other) noexcept {
    if (this != &other) {
        delete ptr;
        ptr = other.ptr;
        other.ptr = nullptr;
    }
    return *this;
}

~UniquePtr() {
    delete ptr;
    std::cout << "hello";
}

T& operator*() const {
    return *ptr;
}

T* operator->() const {
    return ptr;
}
bool operator==(const UniquePtr<T>& other) const {
    return ptr == other.ptr;
}

bool operator!=(const UniquePtr<T>& other) const {
    return !(*this == other);
}
Enter fullscreen mode Exit fullscreen mode

};

class MyClass {
public:
void Print() const {
std::cout << "MyClass" << std::endl;
}
};

int main() {
UniquePtr ptr(new MyClass());
ptr->Print();
/**
UniquePtr ptr2(std::move(ptr));
if (ptr == nullptr) {
std::cout << "ptr is nullptr" << std::endl;
}
*/
return 0;
}

/******************************************/

if 0

include

include

include

class CMyWindow : public CWindowImpl
{
public:
DECLARE_WND_CLASS(_T("CMyWindowClass"))

BEGIN_MSG_MAP(CMyWindow)
    MESSAGE_HANDLER(WM_PAINT, OnPaint)
END_MSG_MAP()

LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(&ps);

    // 绘制窗口内容
    RECT rect;
    GetClientRect(&rect);
    DrawText(hdc, _T("Hello, World!"), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

    EndPaint(&ps);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

};
/*
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 创建窗口
CMyWindow wnd;
wnd.Create(nullptr, CRect(100, 100, 400, 300), _T("My Window"), WS_OVERLAPPEDWINDOW);

// 获取窗口矩形区域
CRect rect;
wnd.GetWindowRect(rect);

// 无效化窗口矩形区域并触发重绘
wnd.InvalidateRect(rect, TRUE);

// 消息循环
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return static_cast<int>(msg.wParam);
Enter fullscreen mode Exit fullscreen mode

}*/

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 创建窗口
HWND hWnd = CreateWindow(
_T("STATIC"), _T("My Window"), WS_OVERLAPPEDWINDOW,
100, 100, 400, 300,
nullptr, nullptr, hInstance, nullptr
);

// 获取窗口矩形区域
RECT rect;
GetWindowRect(hWnd, &rect);

// 将矩形坐标转换为客户区坐标
POINT pt = { 0, 0 };
ScreenToClient(hWnd, &pt);
OffsetRect(&rect, -pt.x, -pt.y);

// 无效化窗口矩形区域并触发重绘
Enter fullscreen mode Exit fullscreen mode

// InvalidateRect(hWnd, &rect, TRUE);

// 显示窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

// 消息循环
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return static_cast<int>(msg.wParam);
Enter fullscreen mode Exit fullscreen mode

}

include

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);

    // 绘制背景
    RECT rect;
    GetClientRect(hWnd, &rect);
    HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255)); // 白色背景
    FillRect(hdc, &rect, hBrush);
    DeleteObject(hBrush);

    // 绘制文本
    SetTextColor(hdc, RGB(0, 0, 0)); // 黑色文本
    SetBkMode(hdc, TRANSPARENT); // 文本背景透明
    DrawText(hdc, L"Hello, World!", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

    EndPaint(hWnd, &ps);
    return 0;
}
case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
Enter fullscreen mode Exit fullscreen mode

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 注册窗口类
const wchar_t* CLASS_NAME = L"MyWindowClass";

WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;

RegisterClass(&wc);

// 创建窗口
HWND hWnd = CreateWindowEx(
    0, CLASS_NAME, L"My Window", WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
    NULL, NULL, hInstance, NULL
);

if (hWnd == NULL)
    return 0;

// 显示窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return (int)msg.wParam;
Enter fullscreen mode Exit fullscreen mode

}

include

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);

    // 绘制背景
    RECT rect;
    GetClientRect(hWnd, &rect);
    HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255)); // 白色背景
    FillRect(hdc, &rect, hBrush);
    DeleteObject(hBrush);

    // 绘制文本
    SetTextColor(hdc, RGB(0, 0, 0)); // 黑色文本
    SetBkMode(hdc, TRANSPARENT); // 文本背景透明
    DrawText(hdc, L"Hello, World!", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

    EndPaint(hWnd, &ps);
    return 0;

case WM_RBUTTONDOWN:

    hdc = GetDC(hwnd);
    GetClientRect(hwnd, &rect);
    DrawText(hdc, TEXT("右键键被单机咯!"), 13, &rect, DT_VCENTER | DT_SINGLELINE);
    ReleaseDC(hWnd, hdc);
    InvalidateRect(hWnd, NULL, true);
    //UpdateWindow(hwnd);
    Sleep(1000);
    return 0;
case WM_LBUTTONDOWN:
    hdc = GetDC(hWnd);
    GetClientRect(hWnd, &rect);
    DrawText(hdc, TEXT("左键被单机咯!"), 13, &rect, DT_CENTER | DT_SINGLELINE);
    ReleaseDC(hWnd, hdc);
    return 0;
}
case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
Enter fullscreen mode Exit fullscreen mode

}

void InvalidateWindow(HWND hWnd)
{
RECT rect;
GetClientRect(hWnd, &rect);
InvalidateRect(hWnd, &rect, TRUE);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 注册窗口类
const wchar_t* CLASS_NAME = L"MyWindowClass";

WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;

RegisterClass(&wc);

// 创建窗口
HWND hWnd = CreateWindowEx(
    0, CLASS_NAME, L"My Window", WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
    NULL, NULL, hInstance, NULL
);

if (hWnd == NULL)
    return 0;

// 显示窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

// 模拟触发重绘
InvalidateWindow(hWnd);

// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return (int)msg.wParam;
Enter fullscreen mode Exit fullscreen mode

}

endif

include

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);

    // 绘制背景
    RECT rect;
    GetClientRect(hWnd, &rect);
    HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255)); // 白色背景
    FillRect(hdc, &rect, hBrush);
    DeleteObject(hBrush);

    // 绘制文本
    SetTextColor(hdc, RGB(0, 0, 0)); // 黑色文本
    SetBkMode(hdc, TRANSPARENT); // 文本背景透明
    DrawText(hdc, L"Hello, World!", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

    EndPaint(hWnd, &ps);
    return 0;
}
case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
case WM_RBUTTONDOWN:
{
    HDC hdc = GetDC(hWnd);
    RECT rect;
    GetClientRect(hWnd, &rect);
    DrawText(hdc, L"右键被点击了!", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    ReleaseDC(hWnd, hdc);
    InvalidateRect(hWnd, NULL, TRUE);
    Sleep(10000);
    return 0;
}
case WM_LBUTTONDOWN:
{
    HDC hdc = GetDC(hWnd);
    RECT rect;
    GetClientRect(hWnd, &rect);
    DrawText(hdc, L"左键被点击了!", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    ReleaseDC(hWnd, hdc);
    return 0;
}
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
Enter fullscreen mode Exit fullscreen mode

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 注册窗口类
const wchar_t* CLASS_NAME = L"MyWindowClass";

WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;

RegisterClass(&wc);

// 创建窗口
HWND hWnd = CreateWindowEx(
    0, CLASS_NAME, L"My Window", WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
    NULL, NULL, hInstance, NULL
);

if (hWnd == NULL)
    return 0;

// 显示窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return (int)msg.wParam;
Enter fullscreen mode Exit fullscreen mode

}

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay