(´>∀)人(´・ω・)ノヽ(・ε・*)人(-д-`)
Ruby @Sprout 2022
(´>∀)人(´・ω・)ノ日主ヽ(・ε・*)人(-д-`)
Ruby @Sprout 2022
(´>∀)人(´・ω・)ノ今日主題ヽ(・ε・*)人(-д-`)
Ruby @Sprout 2022
人(´・ω・)ノ 今日主題 ヽ(・ε・*)人
Ruby @Sprout 2022
ω・)ノ 今日主題 ヽ(・ε
Ruby @Sprout 2022
)ノ 今日主題 ヽ(
Ruby @Sprout 2022
今日主題
Ruby @Sprout 2022
C++ String & FileIO
Ruby @Sprout 2022
C-string Review
std::string
Applications of std::string
std::stringstream
FileIO
Applications of FileIO
C-string Review
std::string
Applications of std::string
std::stringstream
FileIO
Applications of FileIO
C-string 本質上是 char
陣列
利用 <cstring>
(<string.h>
) 中的函式
是 Stack 嗎?還是 Heap 呢?答案是:都不是!
因為 C-string 本質是陣列,所以如同其他型態的陣列一樣,超出範圍不一定會報錯,是未定義行為!
C-string Review
std::string
Applications of std::string
std::stringstream
FileIO
Applications of FileIO
std::string
更多、更詳細的說明文件 (Documentation) 請參考
Any questions?
C-string Review
std::string
Applications of std::string
std::stringstream
FileIO
Applications of FileIO
Solution
哈哈不給你看,上課講解!
Any questions?
C-string Review
std::string
Applications of std::string
std::stringstream
FileIO
Applications of FileIO
What is "stream"
Stream ,即「流」。
ostream
cout
cerr
clog
istream
cin
Sample code
w/o stringstream
w/ stringstram
C-string Review
std::string
Applications of std::string
std::stringstream
FileIO
Applications of FileIO
Create (Overwrite) a file
#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
#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
#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;
}
C-string Review
std::string
Applications of std::string
std::stringstream
FileIO
Applications of FileIO
Loading testcases
#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;
}
感謝參與!
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing