<style>
.dbg {
color: white;
background: rgba(255,255,255, 0.25);
border-radius: 1rem;
text-align: center;
}
.sdbg {
color: white;
background: rgba(255,255,255, 0.25);
border-radius: 1rem;
box-shadow: 0 0 20px #5680db;
text-shadow:
0 0 80px rgb(192 219 255 / 90%),
0 0 32px rgb(65 120 255 / 90%);
text-align: center;
}
.gt {
background:
linear-gradient(
to right, #30CFD0, #c43ad6
);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.warning {
color: red;
font-weight: bold;
}
body {
font-family: 'Courier New', monospace;
}
.rd {
border-radius: 2rem;
}
</style>

<p class="dbg">(´>∀)人(´・ω・)ノヽ(・ε・*)人(-д-`)</p>
###### <p class="gt">Ruby @Sprout 2022</p>
----

<p class="dbg">(´>∀)人(´・ω・)ノ日主ヽ(・ε・*)人(-д-`)</p>
###### <p class="gt">Ruby @Sprout 2022</p>
----

<p class="dbg">(´>∀)人(´・ω・)ノ今日主題ヽ(・ε・*)人(-д-`)</p>
###### <p class="gt">Ruby @Sprout 2022</p>
----

<p class="dbg">人(´・ω・)ノ 今日主題 ヽ(・ε・*)人</p>
###### <p class="gt">Ruby @Sprout 2022</p>
----

<p class="dbg">ω・)ノ 今日主題 ヽ(・ε</p>
###### <p class="gt">Ruby @Sprout 2022</p>
----

<p class="dbg">)ノ 今日主題 ヽ(</p>
###### <p class="gt">Ruby @Sprout 2022</p>
----

<p class="dbg">今日主題</p>
###### <p class="gt">Ruby @Sprout 2022</p>
----

<p class="sdbg">C++ String & FileIO</p>
###### <p class="gt">Ruby @Sprout 2022</p>
----
## Outline
<!-- .slide: data-background="https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg" -->
----
<!-- .slide: data-background="https://i.imgur.com/0XZMqhg.jpg" -->
<p class="dbg">
C-string Review
</p>
<p class="dbg">
std::string
</p>
<p class="dbg">
Applications of std::string
</p>
<p class="dbg">
std::stringstream
</p>
<p class="dbg">
FileIO
</p>
<p class="dbg">
Applications of FileIO
</p>
---
<!-- .slide: data-background="https://i.imgur.com/0XZMqhg.jpg" -->
<p class="sdbg">
C-string Review
</p>
<p class="dbg">
std::string
</p>
<p class="dbg">
Applications of std::string
</p>
<p class="dbg">
std::stringstream
</p>
<p class="dbg">
FileIO
</p>
<p class="dbg">
Applications of FileIO
</p>
----
#### C-string 的本質
***
C-string 本質上是 `char` 陣列

[C-string Review 1](https://onlinegdb.com/cR_s2u0lG)
----
#### C-string 的操基本操作
***
利用 `<cstring>` (`<string.h>`) 中的函式
[C-string Review 2](https://onlinegdb.com/V1GRbzSDE)
----
#### 字面常數字串存在哪裡?
***
是 Stack 嗎?還是 Heap 呢?答案是:都不是!
----
<!-- .slide: data-background-color="white" -->

[C-string Review 3-1](https://onlinegdb.com/1HrCuoQO5)
[C-string Review 3-2](https://onlinegdb.com/NFSAJq52r)
----
#### 多數 Bug 的根源--未定義行為
***
因為 C-string 本質是陣列,所以如同其他型態的陣列一樣,超出範圍不一定會報錯,是未定義行為!

[C-string Review 4](https://onlinegdb.com/M0zRd22TQ)
---
<!-- .slide: data-background="https://i.imgur.com/0XZMqhg.jpg" -->
<p class="dbg">
C-string Review
</p>
<p class="sdbg">
std::string
</p>
<p class="dbg">
Applications of std::string
</p>
<p class="dbg">
std::stringstream
</p>
<p class="dbg">
FileIO
</p>
<p class="dbg">
Applications of FileIO
</p>
----
#### A peek into `std::string`
***
[C++ String 1](https://onlinegdb.com/et8zKCkDS)
[C++ String 2](https://onlinegdb.com/uyV4q6xRl)
----
#### 各種「附帶功能」(Methods)
***
[C++ String 3](https://onlinegdb.com/CW49bMBwp)
----
#### 各種「附帶功能」(Methods)
***
更多、更詳細的說明文件 (Documentation) 請參考
[C++ reference](https://www.cplusplus.com/reference/string/string/)
----

<p class="gt">
Any questions?
</p>
---
<!-- .slide: data-background="https://i.imgur.com/0XZMqhg.jpg" -->
<p class="dbg">
C-string Review
</p>
<p class="dbg">
std::string
</p>
<p class="sdbg">
Applications of std::string
</p>
<p class="dbg">
std::stringstream
</p>
<p class="dbg">
FileIO
</p>
<p class="dbg">
Applications of FileIO
</p>
----
#### 課堂練習 1
***
[Sprout OJ No. 441](https://neoj.sprout.tw/problem/441/)
----
Solution
***
哈哈不給你看,上課講解!
----

<p class="gt">
Any questions?
</p>
---
<!-- .slide: data-background="https://i.imgur.com/0XZMqhg.jpg" -->
<p class="dbg">
C-string Review
</p>
<p class="dbg">
std::string
</p>
<p class="dbg">
Applications of std::string
</p>
<p class="sdbg">
std::stringstream
</p>
<p class="dbg">
FileIO
</p>
<p class="dbg">
Applications of FileIO
</p>
----
What is "stream"
***
Stream ,即「流」。
- 輸入流:字節從設備流向內存
- 輸出流:字節從內存流向設備
[Reference](https://www.runoob.com/cplusplus/cpp-basic-input-output.html)
----
`ostream`
***
- `cout`
- `cerr`
- `clog`
----
`istream`
***
- `cin`
----
<!-- .slide: data-background-color="white" -->

----
Sample code
***
[w/o stringstream](https://onlinegdb.com/hDby79ZJL)
[w/ stringstram](https://onlinegdb.com/-kYNaxXW1)
----
#### 課堂練習 2
***
[Sprout OJ No. 442](https://neoj.sprout.tw/problem/442/)
---
<!-- .slide: data-background="https://i.imgur.com/0XZMqhg.jpg" -->
<p class="dbg">
C-string Review
</p>
<p class="dbg">
std::string
</p>
<p class="dbg">
Applications of std::string
</p>
<p class="dbg">
std::stringstream
</p>
<p class="sdbg">
FileIO
</p>
<p class="dbg">
Applications of FileIO
</p>
----
Create (Overwrite) a file
***
```cpp
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outFile;
outFile.open("out.txt");
outFile << "Some text";
outFile.close();
return 0;
}
```
----
Load an existing file
***
```cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inFile;
inFile.open("in.txt");
string s;
while (inFile >> s) cout << s << endl;
inFile.close();
return 0;
}
```
----
Load and overwrite an existing file
***
```cpp
#include <bits/stdc++.h>
using namespace std;
const string FILENAME = "last_login_time.txt";
int main() {
ifstream inFile; ofstream outFile;
inFile.open(FILENAME);
time_t curr = time(nullptr), last; inFile >> last;
inFile.close();
cout << "It has been " << curr-last << " seconds since your last login." << endl;
outFile.open(FILENAME);
outFile << curr;
outFile.close();
return 0;
}
```
---
<!-- .slide: data-background="https://i.imgur.com/0XZMqhg.jpg" -->
<p class="dbg">
C-string Review
</p>
<p class="dbg">
std::string
</p>
<p class="dbg">
Applications of std::string
</p>
<p class="dbg">
std::stringstream
</p>
<p class="dbg">
FileIO
</p>
<p class="sdbg">
Applications of FileIO
</p>
----
Loading testcases
***
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream inFile("testcase.txt");
int n, _max = -1;
while (inFile >> n)
_max = max(n, _max);
cout << "The max numbers out of all numbers in the testcase: " << _max << endl;
inFile.close();
return 0;
}
```
----
## Quick Review
----
- C-string vs. C++ string
- stream
- stringstream
- FileIO
----
<!-- .slide: data-background="#FFF" -->
感謝參與!
{"metaMigratedAt":"2023-06-17T00:43:58.187Z","metaMigratedFrom":"YAML","title":"Sprout C++ string & FileIO","breaks":true,"contributors":"[{\"id\":\"7f2869ec-74af-417d-af08-3133a06727e3\",\"add\":26825,\"del\":17968}]"}