Skip to content

Commit ccde006

Browse files
committed
Updated to Chapter 13
1 parent caa3fa3 commit ccde006

22 files changed

+346
-90
lines changed

.vscode/Test.cpp

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
#include <iostream>
2-
#include <string>
3-
#include <sstream>
4-
#include <iomanip>
2+
#include <fstream>
3+
#include <filesystem>
4+
#include <stack>
5+
namespace fs = std::filesystem;
56
int main() {
6-
std::string str {"abcdef"};
7-
std::ostringstream strio {str, std::ios_base::out | std::ios_base::trunc};
8-
// std::ostringstream strio {"abcdef", std::ios_base::app};
9-
strio.put('g');
10-
std::cout << str;
11-
// std::cout << strio.str();
7+
try {
8+
fs::path ph {fs::current_path() / ".vscode"};
9+
std::ifstream fin {ph / "in.txt"};
10+
std::ofstream fout {ph / "out.txt"};
11+
if (!fin.is_open()) //输入文件异常检测
12+
throw std::ios_base::failure {"Cannot open in.txt"};
13+
if (!fout.is_open()) //输出文件异常检测
14+
throw std::ios_base::failure {"Cannot open out.txt"};
15+
std::stack<char> stk; //定义一个栈,方便我们存取数据
16+
int ch = {fin.get()}; //定义一个ch,逐个字符地接收get()输入数据
17+
while (!fin.eof()) { //如果fin还没有读到文件结尾
18+
stk.push(ch); //就说明ch是有效内容,把它填入栈中
19+
ch = fin.get(); //接收下一个字符输入
20+
}
21+
while (stk.size()){ //只要栈中还有数据,那就说明输出还没到头
22+
fout << stk.top(); //用fout输出栈顶元素
23+
stk.pop(); //栈顶的元素弹出
24+
}
25+
std::cout << "Done" << std::endl; //提示操作完成
26+
} catch (const std::ios_base::failure &msg) {
27+
std::cerr << "An exception has been caught:\n" << msg.what();
28+
}
1229
return 0;
1330
}

.vscode/Test.exe

265 KB
Binary file not shown.

.vscode/c_cpp_properties.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"_UNICODE"
1212
],
1313
"windowsSdkVersion": "10.0.22621.0",
14-
"compilerPath": "cl.exe",
15-
"intelliSenseMode": "windows-msvc-x64",
14+
"compilerPath": "D:\\msys64\\mingw64\\bin\\gcc.exe",
15+
"intelliSenseMode": "gcc-x64",
1616
"cppStandard": "c++17",
1717
"cStandard": "c11"
1818
}

.vscode/in.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
34 58 91

.vscode/launch.json

+36-9
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,58 @@
22
"configurations": [
33

44
{
5-
"name": "C/C++: g++.exe build and debug active file",
5+
"name": "Launch with gdb",
66
"type": "cppdbg",
77
"request": "launch",
88
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
99
"args": [],
1010
"stopAtEntry": false,
11-
"cwd": "D:\\msys64\\ucrt64\\bin",
11+
"cwd": "${workspaceFolder}",
1212
"environment": [],
1313
"externalConsole": false,
1414
"MIMode": "gdb",
15-
"miDebuggerPath": "D:\\msys64\\ucrt64\\bin\\gdb.exe",
15+
"miDebuggerPath": "D:\\msys64\\mingw64\\bin\\gdb.exe",
1616
"setupCommands": [
1717
{
1818
"description": "Enable pretty-printing for gdb",
1919
"text": "-enable-pretty-printing",
2020
"ignoreFailures": true
2121
},
22-
{
23-
"description": "Set Disassembly Flavor to Intel",
24-
"text": "-gdb-set disassembly-flavor intel",
25-
"ignoreFailures": true
26-
}
2722
],
2823
"preLaunchTask": "C/C++: g++.exe build active file"
2924
}
3025
],
3126
"version": "2.0.0"
32-
}
27+
}
28+
// {
29+
// "configurations": [
30+
31+
// {
32+
// "name": "C/C++: g++.exe build and debug active file",
33+
// "type": "cppdbg",
34+
// "request": "launch",
35+
// "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
36+
// "args": [],
37+
// "stopAtEntry": false,
38+
// "cwd": "D:\\msys64\\ucrt64\\bin",
39+
// "environment": [],
40+
// "externalConsole": false,
41+
// "MIMode": "gdb",
42+
// "miDebuggerPath": "D:\\msys64\\ucrt64\\bin\\gdb.exe",
43+
// "setupCommands": [
44+
// {
45+
// "description": "Enable pretty-printing for gdb",
46+
// "text": "-enable-pretty-printing",
47+
// "ignoreFailures": true
48+
// },
49+
// {
50+
// "description": "Set Disassembly Flavor to Intel",
51+
// "text": "-gdb-set disassembly-flavor intel",
52+
// "ignoreFailures": true
53+
// }
54+
// ],
55+
// "preLaunchTask": "C/C++: g++.exe build active file"
56+
// }
57+
// ],
58+
// "version": "2.0.0"
59+
// }

.vscode/out.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
19 85 43

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
"xhash": "cpp",
6161
"xtree": "cpp",
6262
"string": "cpp",
63-
"iomanip": "cpp"
63+
"iomanip": "cpp",
64+
"fstream": "cpp",
65+
"sstream": "cpp"
6466
}
6567
}

.vscode/tasks.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"type": "cppbuild",
55
"label": "C/C++: g++.exe build active file",
6-
"command": "D:\\msys64\\ucrt64\\bin\\g++.exe",
6+
"command": "D:\\msys64\\mingw64\\bin\\g++.exe",
77
"args": [
88
"-fdiagnostics-color=always",
99
"-g",
@@ -12,7 +12,7 @@
1212
"${fileDirname}\\${fileBasenameNoExtension}.exe"
1313
],
1414
"options": {
15-
"cwd": "D:\\msys64\\ucrt64\\bin"
15+
"cwd": "D:\\msys64\\mingw64\\bin"
1616
},
1717
"problemMatcher": [
1818
"$gcc"

code_in_book/13.3/file_stream.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <fstream>
4+
#include <iomanip>
5+
int main() {
6+
std::ofstream fout;
7+
std::cout<<"Program started.\n";
8+
fout.open("out.txt", std::ios_base::out);
9+
if(!fout.is_open())
10+
std::cerr<<"Didn't opened the file."<<std::flush;
11+
fout << 12345 << std::endl;
12+
fout.close();
13+
std::cout<<"Program ended."<<std::flush;
14+
return 0;
15+
}

code_in_book/13.4/getpath.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <iostream>
2+
#include <filesystem> //filesystem相关的内容在这里
3+
namespace fs = std::filesystem; //因为名字太长了,所以起个命名空间别名
4+
int main() {
5+
fs::path ph {fs::current_path()}; //获取“当前文件夹”的路径
6+
std::cout << ph << std::endl; //直接输出路径对象
7+
std::cout << ph.generic_string() << std::endl; //以通用格式输出
8+
std::cin.get(); //可能需要这句
9+
return 0;
10+
}

code_in_book/13.5/in.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
iqgia qwei 2 15321 qwunl

code_in_book/13.5/out.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lnuwq 12351 2 iewq aigqi

code_in_book/13.5/reverse_copy.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <filesystem>
4+
#include <stack>
5+
namespace fs = std::filesystem; //命名空间别名
6+
int main() {
7+
try {
8+
fs::path ph {fs::current_path()}; //ph就是当前文件夹的路径
9+
std::ifstream fin {ph / "in.txt"}; //在ph文件夹下的in.txt中操作
10+
std::ofstream fout {ph / "out.txt"}; //在ph文件夹下的out.txt中操作
11+
if (!fin.is_open()) //输入文件异常检测
12+
throw std::ios_base::failure {"Cannot open in.txt"};
13+
if (!fout.is_open()) //输出文件异常检测
14+
throw std::ios_base::failure {"Cannot open out.txt"};
15+
std::stack<char> stk;
16+
int ch = {fin.get()}; //定义一个ch,逐个字符地接收get()输入数据
17+
while (!fin.eof()) { //如果fin还没有读到文件结尾
18+
stk.push(ch); //就说明ch是有效内容,把它填入栈中
19+
ch = fin.get(); //再接收下一个字符输入
20+
}
21+
while (stk.size()) { //只要栈中还有数据,那就说明输出还没到头
22+
fout << stk.top(); //用fout输出栈顶元素
23+
stk.pop(); //栈顶的元素弹出
24+
}
25+
std::cout << "Done" << std::endl; //提示操作完成
26+
} catch (const std::ios_base::failure &msg) {
27+
std::cerr << "An exception has been caught:\n" << msg.what();
28+
}
29+
return 0;
30+
}

generalized_parts/13_input_output_streams_overview/03_sstream.tex

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ \section{字符串输入/输出\texttt{sstream}}
3030
std::cout << sin.get() << ' '; //52
3131
\end{lstlisting}
3232
在这里,当我们进行重定向之后,\lstinline@std::cin@ 和 \lstinline@sin@ 就指向了同一个缓冲区,所以它们就会共用输入内容。\par
33-
字符串输入/输出的内容不多,本节只是做一个过渡,接下来就让我们看看文件输入/输出的相关知识。\par
33+
字符串输入/输出的内容不多,本节只是作一个过渡,接下来就让我们看看文件输入/输出的相关知识。\par

0 commit comments

Comments
 (0)