在C++中,可以使用iostream庫中的輸入輸出流對象來進行輸入輸出操作。
int num;
std::cout << "Enter a number: ";
std::cin >> num;
int num = 10;
std::cout << "The number is: " << num << std::endl;
double pi = 3.14159;
std::cout << std::setprecision(3) << pi << std::endl;
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