文章目录希望能每天都不加班~~
- 前言
- 一、CFile读取Unicode编码文件
- 二、多线程循环输入多文件
- 三、多线程复制文件
- 总结
前言
坐标武汉,c++岗实习了三天,完成了三个粗浅的MFC相关的题目
一、CFile读取Unicode编码文件
宽字符转换成正常的char字符再输入到一个二维数组中,重点代码如下
void Change(char array[ROW][COLUMN], int filelen, wchar_t *wp) {
DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, wp, -1, NULL, 0, NULL, FALSE);
char *psText;
psText = new char[dwNum];
if (!psText)
{
delete[]psText;
}
WideCharToMultiByte(CP_OEMCP, NULL, wp, -1, psText, filelen, NULL, FALSE);
int lineIndex = 0;
int nextIndex = 0;
for (int i = 0; i < dwNum; i++) {
if (psText[i] == 'r') {
continue;
}
if (psText[i] == 'n') {
for (int j = 0; nextIndex < i - 1; nextIndex++, j++) {
array[lineIndex][j] = psText[nextIndex];
}
nextIndex += 2;
lineIndex++;
}
}
}
void Read(char array[ROW][COLUMN]) {
CFile file1;
file1.Open(_T("C:\Users\zh\Desktop\source.txt"), CFile::modeReadWrite);
int filelen = file1.GetLength();
wchar_t *wp = new wchar_t[filelen + 1];
memset(wp, 0, (filelen + 1) * sizeof(wchar_t));
file1.Seek(2, 0);
file1.Read(wp, filelen);
wp[filelen] = _T(' ');
file1.Close();
Change(array, filelen, wp);
}
二、多线程循环输入多文件
使用Windows的多线程api,控制顺序用的是Event,代码如下:
#include #include三、多线程复制文件#include #define THREADCOUNT 5 HANDLE ghEvents[THREADCOUNT][THREADCOUNT]; HANDLE ghThreads[THREADCOUNT]; HANDLE EndEvent[THREADCOUNT]; HANDLE EndThread; DWORD WINAPI ThreadFunc(LPVOID lpParam); DWORD WINAPI EndFunc(LPVOID lpParam); FILE* file1[THREADCOUNT]; static int counts = 0; static char end = NULL; void OpenFile() { for (int i = 0; i < THREADCOUNT; i++) { std::string a = "C:\Users\zh\Desktop\"; char b = 'A' + i; std::string c = a + b + ".txt"; char* m = (char*)(c).c_str(); file1[i] = fopen(m, "w+"); } } void CreateEventsandThreads() { for (int i = 0; i < THREADCOUNT; i++) { for (int j = 0; j < THREADCOUNT; j++) { ghEvents[i][j] = CreateEvent(NULL, FALSE, FALSE, NULL); if (ghEvents[i][j] == NULL) { std::cout << GetLastError(); return; } } } DWORD dwThreadID; for (int i = 0; i < THREADCOUNT; i++) { ghThreads[i] = CreateThread(NULL, 0, ThreadFunc, LPVOID(i), CREATE_SUSPENDED, &dwThreadID); if (ghThreads[i] == NULL) { std::cout << GetLastError(); return; } EndEvent[i] = CreateEvent(NULL, FALSE, FALSE, NULL); if (EndEvent[i] == NULL) { std::cout << GetLastError(); return; } } EndThread = CreateThread(NULL, 0, EndFunc, NULL, CREATE_SUSPENDED, &dwThreadID); if (EndThread == NULL) { std::cout << GetLastError(); return; } while (counts < THREADCOUNT) { SetEvent(ghEvents[0][counts]); ResumeThread(ghThreads[counts++]); } } DWORD WINAPI ThreadFunc(LPVOID lpParam) { int j = int(lpParam); char a = 'A' + j; DWORD dwWaitResult; while (TRUE) { DWORD endWaitResult = WaitForSingleObject(EndEvent[j], 5); if (endWaitResult == WAIT_OBJECT_0) { break; } dwWaitResult = WaitForMultipleObjects(THREADCOUNT, ghEvents[j], FALSE, 500); int number = dwWaitResult - WAIT_OBJECT_0; if (number >= THREADCOUNT || number < 0) { continue; } if (j == THREADCOUNT - 1) { std::cout << a << std::endl; fputc(a, file1[number]); fputc('n', file1[number]); ResetEvent(ghEvents[j][number]); SetEvent(ghEvents[0][number]); } else { std::cout << a; fputc(a, file1[number]); ResetEvent(ghEvents[j][number]); SetEvent(ghEvents[j + 1][number]); } } return 0; } DWORD WINAPI EndFunc(LPVOID lpParam) { std::cout << "请输入任意按键结束全部线程" << std::endl; std::cin >> end; if (end != NULL) { for(int i = 0; i < THREADCOUNT; i++) { SetEvent(EndEvent[i]); } } return 0; } int main() { OpenFile(); CreateEventsandThreads(); system("pause"); return 0; }
操作系统课写过类似的,不过当时是linux用的pthread,而且是copy的^^,
比例是1文件:2线程(1读1写):2buffer池
代码如下
#include #include总结#define THREADCOUNT 4 #define MAXMEMERY 4 #define MAXNUM 10 HANDLE readEvents[THREADCOUNT][2]; HANDLE writeEvents[THREADCOUNT][2]; static int endEvents[THREADCOUNT] = { 0 }; HANDLE readThreads[THREADCOUNT]; HANDLE writeThreads[THREADCOUNT]; DWORD WINAPI ReadThreadFunc(LPVOID lpParam); DWORD WINAPI WriteThreadFunc(LPVOID lpParam); FILE* file1[THREADCOUNT]; FILE* file2[THREADCOUNT]; static char buffer[THREADCOUNT][MAXMEMERY]; void OpenReadFile() { for (int i = 0; i < THREADCOUNT; i++) { std::string a = "C:\Users\zh\Desktop\"; char b = 'A' + i; std::string c = a + b + ".txt"; char* m = (char*)(c).c_str(); file1[i] = fopen(m, "r"); } } void OpenWriteFile() { for (int i = 0; i < THREADCOUNT; i++) { std::string a = "C:\Users\zh\Desktop\abc\"; char b = 'A' + i; std::string c = a + b + ".txt"; char* m = (char*)(c).c_str(); file2[i] = fopen(m, "a+"); } } void CreateEventsandThreads() { for (int i = 0; i < THREADCOUNT; i++) { for (int j = 0; j < 2; j++) { readEvents[i][j] = CreateEvent(NULL, FALSE, FALSE, NULL); if (readEvents[i][j] == NULL) { std::cout << GetLastError(); return; } writeEvents[i][j] = CreateEvent(NULL, FALSE, FALSE, NULL); if (writeEvents[i][j] == NULL) { std::cout << GetLastError(); return; } } } DWORD dwThreadID; for (int i = 0; i < THREADCOUNT; i++) { readThreads[i] = CreateThread(NULL, 0, ReadThreadFunc, LPVOID(i), CREATE_SUSPENDED, &dwThreadID); if (readThreads[i] == NULL) { std::cout << GetLastError(); return; } writeThreads[i] = CreateThread(NULL, 0, WriteThreadFunc, LPVOID(i), CREATE_SUSPENDED, &dwThreadID); if (writeThreads[i] == NULL) { std::cout << GetLastError(); return; } } int count = 0; while (count < THREADCOUNT) { SetEvent(readEvents[count][0]); ResumeThread(readThreads[count]); ResumeThread(writeThreads[count++]); } } DWORD WINAPI ReadThreadFunc(LPVOID lpParam) { int j = int(lpParam); DWORD dwWaitResult; while (TRUE) { dwWaitResult = WaitForMultipleObjects(2, readEvents[j], FALSE, INFINITE); int number = dwWaitResult - WAIT_OBJECT_0; if (number >= 2 || number < 0) { continue; } for (int i = 0; i < MAXMEMERY; i++) { fscanf(file1[j], "%c", &buffer[j][i]); if (feof(file1[j])) { for (int m = i; m < MAXMEMERY; m++) { buffer[j][m] = ' '; } endEvents[j] = 1; break; } } ResetEvent(readEvents[j][number]); SetEvent(writeEvents[j][number]); if (endEvents[j] == 1) { break; } } return 0; } DWORD WINAPI WriteThreadFunc(LPVOID lpParam) { int j = int(lpParam); DWORD dwWaitResult; while (TRUE) { dwWaitResult = WaitForMultipleObjects(2, writeEvents[j], FALSE, INFINITE); int number = dwWaitResult - WAIT_OBJECT_0; if (number >= 2 || number < 0) { continue; } fputs(buffer[j], file2[j]); if (endEvents[j] == 1) { break; } ResetEvent(writeEvents[j][number]); SetEvent(readEvents[j][abs(number - 1)]); } return 0; } int main() { OpenReadFile(); OpenWriteFile(); CreateEventsandThreads(); system("pause"); return 0; }
代码肯定还有不完善的地方,实习目前也是先体验一周,感觉不错准备暑期继续来这边实习,感觉这种带薪学习还有人指导,并且不用加班的感觉也太爽了!



