0%

C-01

几个Win32API使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <Windows.h>
#include <process.h>
#include <stdio.h>

// 消除黑窗口
//#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
/* https://blog.csdn.net/zy_505775013/article/details/107432177 */

void main() {
//system("calc");
//HWND win = 0x00080644;
HWND win = FindWindowA(0, "title");
//HWND win = FindWindowA("#32770 (对话框)", NULL); 这里第一个参数这样设置会找不到
HWND btn = FindWindowExA(win, 0, "Button","Button1");
if (win == NULL) {
MessageBoxA(0, "没找到", "提示", 0);
return ;
}
else {
MessageBoxA(0, "找到", "提示 关闭", 0);
}
//SendMessageA(btn, WM_CLOSE, 0, 0);// 关闭btn
/*
SendMessageA(btn, BM_CLICK, 0 ,0);
SendMessageA(btn, BM_CLICK, 0, 0); // 点击btn
*/

EnableWindow(btn, 0);//禁用
Sleep(3000);
EnableWindow(btn, 1);//启用



//SetWindowPos(win, NULL, 100, 100, 200, 300, 0);
//else {
//SetWindowTextA(win, "hhhh");
//SetWindowPos(win,NULL,100,100,200,300,0);
/*while (1) {
ShowWindow(win, SW_HIDE);
Sleep(500);
ShowWindow(win, SW_SHOW);
Sleep(500);
}*/
//}
system("pasue");

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

void main() {
// 同步: 需要先执行完该操作后才能printf
//system("\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\""); //需要用\进行转移"内的内容
//printf("12345");

//异步
//ShellExecuteA(0, "open", "\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\"", 0, 0, 0);
ShellExecuteA(0, "open", "notepad", 0, 0, 6);
//第一个系统弹出; 第二个表示执行; 第三个表示执行的命令行; 第45默认 ;第六个是0代表窗口隐藏 3最大化 6最小化
printf("12345");

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void main() {

time_t ts;
srand((unsigned int)time(&ts));

int data = rand() % 4; //0 1 2 3 代表四个方位
printf("%d", data);
return;


HWND win = FindWindowA("Chrome_WidgetWin_1", "新标签页 - Google Chrome");
if (win) {
while (1)
{
POINT POS1;
GetCursorPos(&POS1);
SetWindowPos(win,NULL, POS1.x ,POS1.y,400,400,0);
Sleep(50);
}
}
}

HOOK system

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// hook  system()

#include <Windows.h>
#include <stdlib.h>
#include "detours.h" //使用该库去修改函数的地址 找到原地址再去修改成新的

#pragma comment(lib,"detours.lib")

//system(); //下面为system函数的定义
//_DCRTIMP int __cdecl system(
// _In_opt_z_ char const* _Command
//);

int (*lpold)(
char const* _Command
) = system;

int newsystem(_In_opt_z_ char const* _Command) {
MessageBoxA(0, "hook", _Command, 0);
// lpold(_Command); //挂钩 先执行上面的内容然后在执行原内容
lpold(_Command);
}

_declspec(dllexport) void hook() {
DetourRestoreAfterWith();//恢复之前的模式状态 避免反复拦截
DetourTransactionBegin();//开始劫持
DetourUpdateThread(GetCurrentThread());
DetourAttach((void**)&lpold, newsystem);
DetourTransactionCommit();
}

线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>

// 使用指针进行冒泡排序 不会破坏原数据
void main() {
int a[8] = { 33,4,3,2,6,7,7,10 };
const int* p[8] = { a,a + 1,a + 2,a + 3,a + 4,a + 5,a + 6,a + 7 }; // 加const后 p 只能读不能修改
for (size_t i = 0; i < 8-1; i++)
{

for (size_t j = 0; j < 8 - 1 - i; j++) {

if (*p[j]>*p[j+1]) {
int* lptemp = p[j];
p[j] = p[j + 1];
p[j + 1] = lptemp;
}
}
}

for (size_t i = 0; i < 8; i++)
{
printf("%d\t", *p[i]);
}
system("pause");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define  _CRT_SECURE_NO_WARNINGS

#include <Windows.h>
#include <stdio.h>
#include <process.h>


DWORD WINAPI NZ(LPVOID lpparam) {
MessageBoxA(0, "NNNNNNAAAAATEXT", "TITLE", 0);
}

void fn(void* lp) {
int* p = lp;
char str[1024] = {0};
sprintf(str, "第%d个窗口",*p); //打印输出到缓冲区中
//格式化函数 第一个参数缓冲区 第二个参数 第三个参数
// 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
// 需要在最上方声明 #define _CRT_SECURE_NO_WARNINGS
MessageBoxA(0, str, str, 0);
}


void main() {
//使用createThread创建线程
//HANDLE hThread; // 线程句柄
//DWORD dwThreadId;// 保持线程ID
//for (size_t i = 0; i < 3; i++)
//{
// hThread = CreateThread(NULL, NULL, NZ, NULL, 0, &dwThreadId);
// WaitForSingleObject(hThread, INFINITE); // 添加该行为异步 否则是同布
// CloseHandle(hThread);
//}

//_beginthread 相比与CreateTHread 更好些
for (size_t i = 0; i < 3; i++)
{
int a[3] = { 1,2,3 };
//_beginthread(fn, 0, &i); // 第三个参数
_beginthread(fn, 0, &a[i]); //三个不同的内存地址值
}


system("pause");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <process.h>
#include <stdio.h>
#include <time.h>

/*使用多线程查找生产随机数中选中的数字 这里是100个数据使用10个线程
如果是100个数据 8个线程 没法除尽 14*7=98 最后一个线程在处理剩下的

*/

//#define M 100 // 数据数量
//#define N 8 //线程数量

struct stINFO
{
int* lpstart; // 起始地址
int length;
int findNum;
int threadId;
};

int FLAG = 0; //判断是否找到 没找到为0 找到为1

void find(void* p) {
if (FLAG) {
return;
}
struct stINFO* lp = p;
printf("线程%d开始查找-----------------\n",lp->threadId);
for ( int* start = lp->lpstart; start < (lp->lpstart+lp->length); start++)
{
if (*start == lp->findNum) {
printf("线程%d找到,数据为%d,内存地址为:%p---\n", lp->threadId, *start, start);
FLAG = 1;
return;
}
}
printf("线程%d查找完毕,未找到----------\n",lp->threadId);
}

#define M 100 //初始化100个数据量

void main() {

int a[M] = { 0 };
time_t ts;
unsigned int randomNum = time(&ts);
srand(randomNum);
for (size_t i = 0; i < M; i++)
{
a[i] = rand() % 100;
printf("%4d", a[i]);
if ((i + 1) % 10 == 0) {
printf("\n");
}
}

int findnumber;
printf("输入要查找的数字:");
scanf_s("%d", &findnumber);
int N;
printf("输入使用的线程数量:");
scanf_s("%d", &N);

struct stINFO info[10];

if (M % N == 0) { //数据标准可以除尽 数据量可以被线程数量平均分配
for (size_t i = 0; i < N; i++)
{
info[i].lpstart = a + M/N * i;
info[i].length = M/N;
info[i].findNum = findnumber;
info[i].threadId = i;

_beginthread(find, 0, &info[i]);
}
}
else {// 100/7=14+2 100 8
for (size_t i = 0; i < N-1; i++)
{
info[i].lpstart = a + M / (N-1) * i;
info[i].length = M / (N-1);
info[i].findNum = findnumber;
info[i].threadId = i;
_beginthread(find, 0, &info[i]);
}
//处理最后一个线程
int i = N - 1;
info[i].lpstart = a + M / (N - 1) * i;
info[i].length = M / (N - 1);
info[i].findNum = findnumber;
info[i].threadId = i;
_beginthread(find, 0, &info[i]);

}



system("pause");
}

const权限控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
/*
* const 做权限控制
const 在*左边 const int* p (查询权限) 可以查询所有
const 在*右边 int * const p 可以修改值 不可以修改指向 只能修改自己 不能把地址执行别的地址(类似用户权限)
const 在*两边 const int * const p 只能读自己的数据 没法修改
*/

void main() {
int a[10] = { 0 };
int(*p)[10] = &a; //数组指针

printf("%p\n%p",p,p+1);

}

字符串相关函数编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int GetStringLenth(char *str) {
int length = 0;
while (*str)
{
length++;
*str++;
}
return length;
}

//双指针轮询 使用两个指针 错位

void DeleteStringD(char *str){
char* p1 = str;
char* p2 = str;
while (*p1 = *p2) {
if (*p1 != 'd') {
// 如果不是选中的字母 两个指针就同时往前动
// p1-p1 -> dedf => eedf => edf => ddf => fdf => df => df
// a b c d e d f
// p2-p2 -> edf => df => f => f => f => 空
// abcdedf => abceedf => abceddf => abcefdf => abcef

p1++;
p2++; //如果是d 那么p1不动 然后p2往前面加 到d时 p2把值赋给p1
}
else {
p2++;
}
}
}

// 字符串异或加密 // 异或之后 再异或就会变成原来的数
void XOREn(char* str, int key) {
for (size_t i = 0; i < strlen(str); i++)
{
str[i] ^= key;
}
}



void main() {
char str1[] = "abcdedf";

//printf("%d\n", GetStringLenth(str1));
//printf("%d", strlen(str1));

//DeleteStringD(str1);
//printf("%s\n", str1);

XOREn(str1,64);
//printf("%s\n", str1);
//XOREn(str1,64);
printf("%s", str1);

}

异或分段加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 异或分段加密
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void 异或分段加密(char* str, char* pass) {
int 加密字符串长度 = strlen(str);
int 密码长度 = strlen(pass);

if (加密字符串长度 % 密码长度 == 0) { // 120 / 6 122 /6
for (size_t i = 0; i < 加密字符串长度 / 密码长度; i++)
{
for (size_t j = 0; j < 密码长度; j++)
{
str[i * 密码长度 + j] ^= pass[j];
}
}
}
else { // 122 / 6 = 20...2
for (size_t i = 0; i < 加密字符串长度 / 密码长度; i++)
{
for (size_t j = 0; j < 密码长度; j++)
{
str[i * 密码长度 + j] ^= pass[j]; // 1-120
}
}
for (size_t k = 0; k < 加密字符串长度 % 密码长度; k++)
{
str[加密字符串长度 / 密码长度 * 密码长度 + k] ^= pass[k]; // 121 122
}
}

}

void main() {
char str[] = "awagege";
char pass[] = "12345";
异或分段加密(str, pass);
printf("\n%s", str);
异或分段加密(str, pass);
printf("\n%s", str);

}

链表

增加、删除不需要移动直接改变指针的指向 缺点:查询/修改无法像数组定位,必须使用循环方式进行定位

(为了便于理解 多数函数都使用累中文)

静态链表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>

struct NODE {
int num;
char s[10];
struct NODE* lpNext;
};

void main() {
struct NODE st1, st2, st3, st4;
st1.num = 1;
st2.num = 2;
st3.num = 3;
st4.num = 4;

struct NODE* p = &st1; // 头节点

st1.lpNext = &st2;
st2.lpNext = &st3;
st3.lpNext = &st4;
st4.lpNext = NULL;

//删除st3
st2.lpNext = st3.lpNext;

//添加st5到st4后
struct NODE st5;
st5.num = 100;
st5.lpNext = NULL;
st4.lpNext = &st5;

//中间插入在st1 st2之间
struct NODE st6;
st6.num = 6;
st6.lpNext = &st2;
st1.lpNext = &st6;

//修改st6的num
for (p ; p!=NULL ; p= p->lpNext)
{
if (p->num == 100) {
p->num = 20;
}
printf("%d\n", p->num);
}

//遍历num
//while (p) {
// printf("%d\n", p->num);
// p = p->lpNext;
//}
//或者
/*for (p ; p!=NULL ; p = p->lpNext)
{
printf("%d\n", p->num);
}*/
}
动态链表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// linked.h
#pragma once

struct NODE {
int num;
struct NODE* lpNext ;
};

typedef struct NODE node;

void 显示链表内容(node* 头节点);
void 尾部添加(node* 头节点, int data); //头节点 起始数据地址
void 头部添加(node* 头节点, int data);
void 删除(node* 头节点, int data);
void 插入(node* 头节点, int newdata, int data);
void 修改(node* 头节点, int newdata, int data);
node* 查找(node* 头节点, int data);
void 打印环形列表(node* 头节点);
int 判断是否为环形链表(node* 头节点);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdio.h>
#include <stdlib.h>
#include "linked.h"

void main() {
node* 头节点 = NULL;
尾部添加(&头节点, 1);
尾部添加(&头节点, 2);
尾部添加(&头节点, 4);
尾部添加(&头节点, 6);
尾部添加(&头节点, 8);
头部添加(&头节点, 10);
//删除(&头节点, 4);
//插入(&头节点, 5, 8);
//修改(&头节点, 20, 6);

//创建环形链表
node* p = 头节点;
for (p; p->lpNext!=NULL; p=p->lpNext)
{

}
p->lpNext = 头节点;
判断是否为环形链表(头节点) ? printf("环形\n"): printf("不是环形\n");

打印环形列表(头节点);
//显示链表内容(头节点);

//查找(头节点, 1) == NULL ? printf("未找到查找数据") : printf("找到数据:%d,地址是:%p", 查找(头节点, 1)->num, 查找(头节点, 1));
}

void 尾部添加(node** 头节点, int data) { //这里需要一个二级指针
// 初始化一个新的节点
node* 新节点 = malloc(sizeof(node));
新节点->lpNext = NULL;
新节点->num = data;

if (*头节点 == NULL) {
*头节点 = 新节点;
}
else
{
node* p = *头节点; //备份一个头节点 方便后续操作
while (p->lpNext !=NULL)
{
p = p->lpNext;
}
p->lpNext = 新节点;
}
}

void 头部添加(node ** 头节点 ,int data) {
// 初始化一个新的节点
node* 新节点 = malloc(sizeof(node));
新节点->lpNext = NULL;
新节点->num = data;
if (*头节点 == NULL) {
*头节点 = 新节点;
}
else {
新节点->lpNext = *头节点; //头节点的地址
*头节点 = 新节点;

}
}

void 显示链表内容(node* 头节点) {
if (头节点) {
printf("数据为:%4d,当前节点地址为:%p,下一个节点地址为%p\n", 头节点->num, 头节点, 头节点->lpNext);
显示链表内容(头节点->lpNext);
}
}

//双指针 p1在前 p2在后
void 删除(node** 头节点, int data) {
node *p1 = NULL, *p2 = NULL;

//备份一个头节点 再进行后续操作
p1 = *头节点;

while (p1) {
if (p1->num != data) //找不到要删除的数据
{
p2 = p1;
p1 = p1->lpNext; // p1在p2前一个
}
else
{
break; //跳出
}
}
if (p1 != *头节点) {
p2->lpNext = p1->lpNext;
free(p1);
}
else //p1在最前面(删除头节点)
{
*头节点 = p1->lpNext;
free(p1);
}

}

void 插入(node **头节点, int newdata, int data) {
node* p1 = NULL, * p2 = NULL;

//初始化插入的新节点
node* 新节点 = malloc(sizeof(node));
新节点->lpNext = NULL;
新节点->num = newdata;

//备份
p1 = *头节点;

while (p1) {
if (p1->num != data) //找不到要拆入到其前面的数据
{
p2 = p1;
p1 = p1->lpNext;
}
else {
break;
}
}
if (p1 == *头节点)
{
头节点 = 新节点;
新节点->lpNext = p1;

}
else //如果在中间 p2 新节点 p1
{
p2->lpNext = 新节点;
新节点->lpNext = p1;
}

}

void 修改(node** 头节点, int newdata, int data) { //**二级指针 保存头节点的地址,不是一个备份副本 这样在后面main中 使用修改(头节点x,x)时可以修改头节点的值
node* p = malloc(sizeof(node));
p->lpNext = NULL;
p->num = NULL;

p = *头节点;
while (p!= NULL)
{
if (p->num == data) {
p->num = newdata;
p = p->lpNext;
}
else {
p = p->lpNext;
}
}
}

node* 查找(node* 头节点,int data) { //修改和查找差不多

/*while (头节点) {
if (头节点->num == data) {
return 头节点;
}
头节点 = 头节点->lpNext;
}*/

//或者使用for
for (头节点; 头节点!=NULL; 头节点= 头节点->lpNext)
{
if (头节点->num == data) {
return 头节点;
}
}
return NULL;
}

void 打印环形列表(node* 头节点) {
node* p = 头节点;
for (p;p->lpNext != 头节点; p = p->lpNext)
{
printf("数据为:%d, 当前节点地址: %p ,下一个节点地址:% p\n", p->num, p, p->lpNext);
}
printf("数据为:%d, 当前节点地址: %p ,下一个节点地址:% p\n", p->num, p, p->lpNext);
}

int 判断是否为环形链表(node* 头节点) //使用两个指针 p1 p2 p1速度为2 p1速度为1 在某一时刻会相遇说明是环形链表
{
int Flag = 0; //默认0 不是
node* p1 = NULL, * p2 = NULL;

for (node *p1 =头节点,*p2=头节点; p1 !=NULL && p2!=NULL; p1=p1->lpNext,p2=p2->lpNext)
{
//p2 快
p2 = p2->lpNext;
if (p1 == p2) {
Flag = 1;
break;
}
}
}

分割合并二进制文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <process.h>


char path[] = "C:\\Users\\test\\Desktop\\cdemo2.exe";
char** 路径数组 = NULL;


// 获取文件字节数
int _getSize(char* path) {
FILE* p = fopen(path, "rb"); //rb 打开二进制文件
fseek(p, 0, SEEK_END);

int size = ftell(p);
fclose(p);
//printf("\n原文件大小为:%d\n",size);
return size;

}

void 分割(char* path, int num) {
FILE* p = fopen(path, "rb");
int size = _getSize(path);
if (size % num == 0) { //字节大小整除
for (size_t i = 0; i < num; i++)
{
FILE* 分割后的文件 = fopen(路径数组[i],"wb");
for (size_t j = 0; j < size/num; j++) {
fputc(fgetc(p), 分割后的文件);
}
fclose(分割后的文件);
}
}
else { //不整除 最后一个单独处理
for (size_t i = 0; i < num-1; i++)
{
FILE* 分割后的文件 = fopen(路径数组[i], "wb");
for (size_t j = 0; j < size / (num-1); j++) {
fputc(fgetc(p), 分割后的文件);

}
fclose(分割后的文件);
}

FILE* 分割后的文件 = fopen(路径数组[num-1], "wb"); //最后一个路径
for (size_t i = 0; i < size%(num-1); i++)
{
fputc(fgetc(p), 分割后的文件);
}
fclose(分割后的文件);
}

fclose(p);
}


void 合并(char * newpath, int num) {
FILE* p = fopen(newpath, "wb");
for (size_t i = 0; i < num; i++)
{
int len = _getSize(路径数组[i]);
FILE* pp = fopen(路径数组[i], "rb");
for (size_t j = 0; j < len; j++)
{
fputc(fgetc(pp), p);
}
fclose(pp);
}
fclose(p);
}



void main() {
int num;
scanf_s("%d", &num);

路径数组 = malloc(sizeof(char*) * num);
for (size_t i = 0; i < num; i++)
{
路径数组[i] = malloc(sizeof(char) * 50);
sprintf(路径数组[i], "C:\\Users\\test\\Desktop\\Project%d.exe", i+1);
printf("%s", 路径数组[i]);

}
分割(path, num);
合并("C:\\Users\\test\\Desktop\\back.exe", num);

for (size_t i = 0; i < num; i++)
{
free(路径数组[i]);
}
free(路径数组);


}

查看目录下文件

1
2
3
4
5
6
7
8
9
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void main() {
char cmd[100] = {0};
char* path = "C:\\Users\\test\\Desktop";
sprintf(cmd, "dir /b /a:-d \"%s\"",path); //dir /b 仅仅显示文件名 /a:-d 排除目录
system(cmd);
}

文本操作

读取txt并输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void main() {
char c[1000];
FILE* p = fopen("C:\\Users\\test\\Desktop\\test.txt","r");
while (fgets(c, sizeof(c), p)!=NULL)
{
printf("%s", c);
}
fclose(p);

}
分割txt文本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int line = 0;
char path[] = "C:\\Users\\test\\Desktop\\test.txt";
char** 路径数组 = NULL;

// 获取文件字节数
int _getSize(char* path) {
FILE* p = fopen(path, "r");
fseek(p, 0, SEEK_END);

int size = ftell(p);
fclose(p);
printf("\n原文件大小为:%d\n",size);
return size;

}

int _getLine() {

char c[1000];
FILE* p = fopen("C:\\Users\\test\\Desktop\\test.txt", "r");
while (fgets(c, sizeof(c), p) != NULL)
{
//printf("%s", c);
line++;
}
printf("总行数为 %d 行\n",line);

fclose(p);
}


void 分割(char* path, int num) {
char str[100] = { 0 };
FILE* p = fopen(path, "r");
if (line % num == 0) { //字节大小整除
for (size_t i = 0; i < num; i++)
{
FILE* 分割后的文件 = fopen(路径数组[i], "w");
for (size_t j = 0; j < line / num; j++) {
fputs(fgets(str,sizeof(str), p), 分割后的文件);
}
fclose(分割后的文件);
}
}
else { //不整除 最后一个单独处理
for (size_t i = 0; i < num - 1; i++)
{
FILE* 分割后的文件 = fopen(路径数组[i], "w");
for (size_t j = 0; j < line / (num - 1); j++) {
fputs(fgets(str,sizeof(str), p), 分割后的文件);

}
fclose(分割后的文件);
}

FILE* 分割后的文件 = fopen(路径数组[num - 1], "w"); //最后一个路径
for (size_t i = 0; i < line % (num - 1); i++)
{
fputs(fgets(str,sizeof(str), p), 分割后的文件);
}
fclose(分割后的文件);
}

fclose(p);
}


void main() {
int num;
_getLine();
scanf_s("%d", &num);
//53 4 17*3+2
路径数组 = malloc(sizeof(char*) * num);
for (size_t i = 0; i < num; i++)
{
路径数组[i] = malloc(sizeof(char) * 50);
sprintf(路径数组[i], "C:\\Users\\test\\Desktop\\Text%d.txt", i + 1);
printf("%s", 路径数组[i]);
}
分割(path, num);

for (size_t i = 0; i < num; i++)
{
free(路径数组[i]);
}
free(路径数组);

}
查找
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main() {

FILE* p = fopen("C:\\Users\\test\\Desktop\\Text1.txt", "r");
if(p==NULL){
return;
}
while (p != feof(p)) {
char str[255] = { 0 };
fgets(str, 255, p);
char* lpstr = strstr(str, "Windows");
if (lpstr != NULL) {
printf("%s", str);
}
}

fclose(p);
}
修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main() {

FILE* p = fopen("C:\\Users\\test\\Desktop\\test2.txt", "r");
FILE* p2 = fopen("C:\\Users\\test\\Desktop\\Temp.txt", "w");
if (p == NULL) {
return;
}
while (!feof(p)) {
char str[255] = { 0 };
fgets(str, 255, p);
char* lpstr = strstr(str, "Windows");
if (lpstr != NULL) { //找到相关字符进行替换
printf("%s", str);
fputs("Linux", p2); //替换为Linux
}
else {
fputs(str, p2);
}
}


fclose(p);
fclose(p2);
remove("C:\\Users\\test\\Desktop\\test2.txt");
rename("C:\\Users\\test\\Desktop\\Temp.txt", "C:\\Users\\test\\Desktop\\test2.txt");

}
插入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main() {

FILE* p = fopen("C:\\Users\\test\\Desktop\\test2.txt", "r");
FILE* p2 = fopen("C:\\Users\\test\\Desktop\\Temp.txt", "w");
if (p == NULL) {
return;
}
while (!feof(p)) {
char str[255] = { 0 };
fgets(str, 255, p);
char* lpstr = strstr(str, "Windows");
if (lpstr != NULL) { //找到相关字符在其前后插入即可
//printf("%s", str);
fputs("Linux", p2);
fputs(str, p2);
fputs("Linux", p2);
}
else {
fputs(str, p2);
}
}


fclose(p);
fclose(p2);
remove("C:\\Users\\test\\Desktop\\test2.txt");
rename("C:\\Users\\test\\Desktop\\Temp.txt", "C:\\Users\\test\\Desktop\\test2.txt");

}

欢迎关注我的其它发布渠道

------------- 💖 🌞 本 文 结 束 😚 感 谢 您 的 阅 读 🌞 💖 -------------