一、從C Stream讀取
C Stream是C語言中進行標準輸入輸出操作的方式。C++在繼承C語言的同時也繼承了這種方式,通過使用C Stream,我們可以向同一個設備中進行不同格式的輸入輸出。
C++ Stream提供了iostream頭文件,通過讀寫字符序列來執行各種操作。
#include
using namespace std;
int main() {
char str[50];
cout <> str;
cout << "你輸入的字符串是: " << str << endl;
return 0;
}
以上代碼中,我們使用了cin和cout流來進行輸入輸出。cin用於輸入,它會等待用戶輸入,直到按下回車鍵後結束輸入。cout用於輸出,在這個例子中,我們輸入一個字符串,然後使用cout輸出它。
二、C Stream Reader類
在C++中,我們可以通過定義C Stream Reader類來使用C stream。這一類提供了一個基本的方法,可以讀取從流中輸入的字符。
以下是一個實現從流中讀取的示例代碼:
#include
#include
#include
using namespace std;
int main() {
ifstream myfile("example.txt");
string line;
if (myfile.is_open()) {
while (getline(myfile,line)) {
cout << line << '\n';
}
myfile.close();
}
else cout << "無法打開文件";
return 0;
}
在上面的代碼中,我們定義了一個名為myfile的對象,並使用它打開了一個名為”example.txt”的文件。
在程序中,我們使用了ifstream類來進行文件輸入,對於這個類,我們還可以使用ofstream類來進行文件輸出。
三、C Stream函數
C++ Stream還提供了大量的函數幫助我們進行流操作。常見的流函數包括put、get、write、read、istringstream、ostringstream等。
以下是一個C++ Stream函數的示例代碼:
#include
#include
#include
using namespace std;
int main() {
int x = 5, y = 10, z;
stringstream ss;
ss << x << ' ' <> z;
cout << z <> z;
cout << z << endl;
return 0;
}
在上面的代碼中,我們使用了stringstream 定義了一個名為ss的對象。然後將一個整型數x和一個空格字符‘ ‘寫入到該對象中。接着從該對象中取出兩個整數,並分別賦值給變量z,最後將它們輸出。
四、C Stream轉換JSON
C++ Stream還可以將C++對象轉換成JSON字符串。在這個過程中,我們可以使用nlohmann_json庫,它是一個基於模板的JSON庫,支持C++ 11。
以下是一個將class對象轉換成JSON字符串的示例代碼:
#include
#include
#include
#include "json.hpp"
using json = nlohmann::json;
class Person {
public:
Person() {}
Person(std::string name, int age) : name(name), age(age) {}
std::string getName() const { return name; }
void setName(std::string name) { this->name = name; }
int getAge() const { return age; }
void setAge(int age) { this->age = age; }
private:
std::string name;
int age;
};
int main() {
Person p("John Doe", 32);
json j;
j["name"] = p.getName();
j["age"] = p.getAge();
std::string jsonStr = j.dump(4);
std::cout << jsonStr << std::endl;
return 0;
}
以上代碼中,我們定義了一個Person類和一個名為j的json對象。通過使用j對象將Person對象(p)的數據導入json中,最後使用j.dump(4) 方法將json轉換為具有縮進的JSON字符串。
五、C Stream存儲
我們可以通過文件將C++對象存儲到本地磁盤。使用二進制文件進行存儲的優點是文件大小較小,但它們只能在相同的機器上使用,在另一台機器上使用文件可能會出現不兼容的問題。使用文本文件進行存儲的優點是它們是可移植的。
以下是一個二進制文件存儲的示例代碼:
#include
#include
#include
struct Person {
std::string name;
int age;
};
int main() {
std::vector people {
{ "Alice", 22 },
{ "Bob", 31 },
{ "Charlie", 45 }
};
std::ofstream file("people.bin", std::ios::binary);
for (const auto& person : people) {
file.write(reinterpret_cast(&person), sizeof(Person));
}
return 0;
}
以上代碼中,我們定義了一個Person結構體和一個人物向量。然後使用文件輸出流將people向量中的Person對象存儲到一個名為people.bin的文件中。
六、C Stream原理
C++ stream是一個系統級別的類庫,使用了一對單向管道,一端為輸入,另一端為輸出。在輸入端,我們可以使用istream來進行各種形式的輸入,而在輸出端,我們則可以使用ostream進行各種形式的輸出。在實際操作中,我們可使用std::cout來進行輸出(代替stdout)。在C++ stream內部,它會根據輸出緩衝的大小決定何時輸出數據。
七、C Stream沒有長度
C++ stream沒有一個固定的長度,我們可以根據需要動態的分配內存。
以下是一個動態分配內存的示例代碼:
#include
#include
#include
class Person {
public:
Person(const std::string &name, int age) : name(name), age(age) {}
std::string toString() const {
std::ostringstream oss;
oss << "Name: " << name << "; Age: " << age << '\n';
return oss.str();
};
private:
std::string name;
int age;
};
int main() {
Person p1("John Doe", 32);
Person p2("Jane Doe", 28);
std::ostringstream oss;
oss << p1.toString() << p2.toString();
std::cout << oss.str();
return 0;
}
在上面的代碼中,我們使用std::ostringstream順序地將兩個Person對象(p1和p2)的數據保存在內存中。最後,我們使用std::cout將其輸出到屏幕上。
八、C Stream限速
C++ Stream支持限速,我們可以使用std::this_thread::sleep_for()函數來對流的流量進行限制。
以下是一個限制文件輸出速度的示例代碼:
#include
#include
#include
#include
struct Person {
std::string name;
int age;
};
int main() {
std::vector people {
{ "Alice", 22 },
{ "Bob", 31 },
{ "Charlie", 45 }
};
std::ofstream file("people.txt");
for (const auto& person : people) {
auto str = person.name + " " + std::to_string(person.age) + "\n";
file.write(str.c_str(), str.size());
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}
以上代碼中,我們使用std::this_thread::sleep_for()函數對文件的輸出速率進行限制。每一秒鐘輸出一條記錄。
九、C Stream保存到數據庫
C++ Stream 與數據庫的結合使用可以讓我們輕鬆地將數據從C++對象導入到數據庫中。我們可以使用數據庫提供的API,並且可以在C++中使用不同的庫來與不同類型的數據庫交互。
以下是一個使用sqlite3數據庫進行操作的示例代碼:
#include
#include
#include
#include
struct Person {
std::string name;
int age;
};
int main(int argc, char* argv[]) {
std::vector people{
{"Alice", 22},
{"Bob", 31},
{"Charlie", 45}
};
sqlite3* db;
char* errMsg;
int rc = sqlite3_open("test.db", &db);
if (rc != SQLITE_OK) {
std::cerr << "無法打開數據庫: " << sqlite3_errmsg(db) << '\n';
sqlite3_close(db);
return -1;
}
rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS people(name TEXT, age INT)", nullptr, nullptr, &errMsg);
if (rc != SQLITE_OK) {
std::cerr << "無法執行語句: " << errMsg << '\n';
sqlite3_free(errMsg);
sqlite3_close(db);
return -1;
}
for (const auto& person : people) {
std::string sql = "INSERT INTO people VALUES('" + person.name + "', " + std::to_string(person.age) + ")";
rc = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg);
if (rc != SQLITE_OK) {
std::cerr << "無法執行語句: " << errMsg << '\n';
sqlite3_free(errMsg);
sqlite3_close(db);
return -1;
}
}
sqlite3_close(db);
return 0;
}
以上代碼中,我們使用了sqlite3開源數據庫與C++ Stream進行交互。在程序中,我們創建了一個名為People的std::vector可以從中導入數據。然後使用sqlite3庫在一個名為test.db的數據庫中創建了一個名為people的表,並將people向量中的Person對象插入到該表中。
原創文章,作者:GBSLO,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/372846.html
微信掃一掃
支付寶掃一掃