在C++中,可以使用iostream库中的输入输出流对象来进行输入输出操作。
- 输入流对象cin:用于从控制台或其他输入设备读取数据。
int num;std::cout << "Enter a number: ";std::cin >> num;- 输出流对象cout:用于向控制台或其他输出设备输出数据。
int num = 10;std::cout << "The number is: " << num << std::endl;- 格式化输出流对象:可以使用setprecision、setw等来进行格式化输出。
double pi = 3.14159;std::cout << std::setprecision(3) << pi << std::endl;- 文件输入流对象ifstream和文件输出流对象ofstream:用于读取和写入文件。
std::ofstream outfile("example.txt");outfile << "Hello, World!";outfile.close();std::ifstream infile("example.txt");std::string line;while (std::getline(infile, line)) {std::cout << line << std::endl;}infile.close();需要包含头文件#include
上一篇:如何使用java打印三角形
下一篇:c#如何去除字符串指定字符
C++









