从C Stream读取
C Stream是C语言中进行标准输入输出操作的方式。C在继承C语言的同时也继承了这种方式,通过使用C Stream,我们可以向同一个设备中进行不同格式的输入输出。 C Stream提供了iostream头文件,通过读写字符序列来执行各种操作。
#include <iostream>
using namespace std;
int main() {
char str[50];
cout << "输入字符串: ";
cin >> str;
cout << "你输入的字符串是: " << str << endl;
return 0;
}
以上代码中,我们使用了cin和cout流来进行输入输出。cin用于输入,它会等待用户输入,直到按下回车键后结束输入。cout用于输出,在这个例子中,我们输入一个字符串,然后使用cout输出它。
C Stream Reader类
在C++中,我们可以通过定义C Stream Reader类来使用C stream。这一类提供了一个基本的方法,可以读取从流中输入的字符。 以下是一个实现从流中读取的示例代码:
#include <fstream>
#include <iostream>
#include <string>
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 <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int x = 5, y = 10, z;
stringstream ss;
ss << x << ' ' << y;
ss >> z;
cout << z << endl;
ss >> 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 <iostream>
#include <fstream>
#include <vector>
#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 <fstream>
#include <iostream>
#include <vector>
struct Person {
std::string name;
int age;
};
int main() {
std::vector<Person> people {
{ "Alice", 22 },
{ "Bob", 31 },
{ "Charlie", 45 }
};
std::ofstream file("people.bin", std::ios::binary);
for (const auto& person : people) {
file.write(reinterpret_cast<const char*>(&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 <iostream>
#include <sstream>
#include <string>
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 <chrono>
#include <fstream>
#include <iostream>
#include <vector>
struct Person {
std::string name;
int age;
};
int main() {
std::vector<Person> 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 <iostream>
#include <sqlite3.h>
#include <string>
#include <vector>
struct Person {
std::string name;
int age;
};
int main(int argc, char* argv[]) {
std::vector<Person> 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对象插入到该表中。