-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse.cpp
More file actions
71 lines (55 loc) · 1.44 KB
/
reverse.cpp
File metadata and controls
71 lines (55 loc) · 1.44 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
std::fstream inFile;//这样可能会快点
std::ofstream outFile;
int main(int argc, char* args[]) {
//错误处理
if (argc < 2) {
std::cerr << "参数太少!too few arguments \n";
return -1;
}
else if (argc > 2) {
std::cerr << "参数太多! too much arguments! \n";
return -1;
}
//打开文件
inFile.open(args[1], std::ios::in | std::ios::binary);
std::cout << "文件名:" << args[1] << "\n";
//新建输出文件
std::string outName = args[1];
outName += ".reverse";
outFile.open(outName, std::ios::out | std::ios::binary | std::ios::trunc);
//错误处理
if (!inFile.is_open()) {
std::cerr << "无法打开文件:" << args[1];
return -1;
}
if (!outFile.is_open()) {
std::cerr << "无法打开文件:" << outName;
return -1;
}
//获取文件大小
inFile.seekg(0, std::ios::end);
int size = (int)inFile.tellg();
std::cout << "文件大小:" << size << "\n";
//建立缓冲区
char* buffer = (char*)malloc(size);
//读取数据到缓冲区
inFile.seekg(0, std::ios::beg);
inFile.read(buffer, size);
//反转缓冲区
std::vector<char> revBuffer(buffer, buffer + size);
std::reverse(revBuffer.begin(), revBuffer.end());
//输出保存
outFile.write(revBuffer.data(), size);
//结束
inFile.close();
outFile.close();
return 0;
}
//fuck this shit, I love C more than C++
//no ,I love C++ again
//2025.07.21