C++语言程序设计 (郑莉)第十一章 流类库与输入/输出














cpp 复制代码
#include <iostream>
using namespace std;
int main() {
	double values[] = { 1,23,35.36,653.7,4358.24 };
	for (int i = 0; i < 4; i++) {
		cout.width(10);
		cout << values[i] << endl;
	}
	return 0;
}
cpp 复制代码
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main() {
	double values[] = { 1.23,35.36,653.7,4358.24 };
	string names[] = { "Zoot","Jimmy","AI","Stan" };
	for (int i = 0; i < 4; i++) {
		cout << setw(6) << names[i] << setw(10) << values[i] << endl;
	}
	return 0;
}
cpp 复制代码
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main() {
	double values[] = { 1.23,35.36,653.7,4358.24 };
	string names[] = { "Zoot","Jimmy","AI","Stan" };
	for (int i = 0; i < 4; i++) {
		cout << setiosflags(ios_base::left) << setw(6) << names[i]
			<< resetiosflags(ios_base::left) << setw(10) << values[i] << endl;
	}
	return 0;
}



cpp 复制代码
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main() {
	double values[] = { 1.23,35.36,653.7,4358.24 };
	string names[] = { "Zoot","Jimmy","AI","Stan" };
	//cout << setiosflags(ios_base::fixed);
	//cout << setiosflags(ios_base::scientific);
	for (int i = 0; i < 4; i++) {
		cout << setiosflags(ios_base::left) << setw(6) << names[i]
			<< resetiosflags(ios_base::left) << setw(10) << setprecision(1) << values[i] << endl;
	}
	return 0;
}


cpp 复制代码
#include <iostream>
#include <fstream>
using namespace std;
struct Date {
	int mon, day, year;
};
int main() {
	Date dt = { 6,10,92 };
	ofstream file("data.dat", ios_base::binary);
	file.write(reinterpret_cast<char*>(&dt), sizeof(dt));
	file.close();
	return 0;
}


cpp 复制代码
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
template <class T>
inline string toString(const T& v) {
	ostringstream os;
	os << v;
	return os.str();
}

int main() {
	string str1 = toString(5);
	cout << str1 << endl;
	string str2 = toString(1.2);
	cout << str2 << endl;
	return 0;
}






cpp 复制代码
#include <iostream>
using namespace std;
int main() {
	char ch;
	while ((ch = cin.get()) != EOF) {
		cout.put(ch);
	}
	return 0;
}
cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
int main() {
	string line;
	cout << "Type a line terminated by '\t'" << endl;
	getline(cin, line, '\t');
	cout << line << endl;
	return 0;
}
cpp 复制代码
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct SalaryInfo {
	unsigned id;
	double salary;
};
int main() {
	SalaryInfo employee1 = { 600001,8000 };
	ofstream os("payroll", ios_base::out | ios_base::binary);
	os.write(reinterpret_cast<char*>(&employee1), sizeof(employee1));
	os.close();
	ifstream is("payroll", ios_base::in | ios_base::binary);
	if (is) {
		SalaryInfo employee2;
		is.read(reinterpret_cast<char*>(&employee2), sizeof(employee2));
		cout << employee2.id << " " << employee2.salary << endl;
	}
	else {
		cout << "ERROR:Cannot open file 'payroll'." << endl;
	}
	is.close();
	return 0;
}
cpp 复制代码
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main() {
	int values[] = { 3,7,0,5,4 };
	ofstream os("integers", ios_base::out | ios_base::binary);
	os.write(reinterpret_cast<char*>(values), sizeof(values));
	os.close();
	ifstream is("integers", ios_base::in | ios_base::binary);
	if (is) {
		is.seekg(3 * sizeof(int));
		int v;
		is.read(reinterpret_cast<char*>(&v), sizeof(v));
		cout << "The 4th integers in the file 'integers' is" << v << endl;
	}
	else {
		cout << "ERROR:Cannot open file 'integers'." << endl;
	}
	return 0;
}
cpp 复制代码
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main() {
	ifstream file("integers", ios_base::in | ios_base::binary);
	if (file) {
		while (file) {
			streampos here = file.tellg();
			int v;
			file.read(reinterpret_cast<char*>(&v), sizeof(int));
			if (file && v == 0) {
				cout << "Position" << here << "is 0" << endl;
			}
		}
	}
	else {
		cout << "ERROR:Cannot open file 'integers'." << endl;
	}
	file.close();
	return 0;
}


cpp 复制代码
#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
using namespace std;
template <class T>
inline T fromString(const string& str) {
	istringstream is(str);
	T v;
	is >> v;
	return v;
}

int main() {
	int v1 = fromString<int>("5");
	cout << v1 << endl;
	double v2 = fromString<double>("1.2");
	cout << v2 << endl;
	return 0;
}




相关推荐
XiaoLeisj13 小时前
Android Kotlin 全链路系统化指南:从基础语法、类型系统与面向对象,到函数式编程、集合操作、协程并发与 Flow 响应式数据流实战
android·开发语言·kotlin·协程
dapeng287014 小时前
分布式系统容错设计
开发语言·c++·算法
qq_4176950514 小时前
代码热修复技术
开发语言·c++·算法
badhope19 小时前
Mobile-Skills:移动端技能可视化的创新实践
开发语言·人工智能·git·智能手机·github
码云数智-园园20 小时前
微服务架构下的分布式事务:在一致性与可用性之间寻找平衡
开发语言
C++ 老炮儿的技术栈20 小时前
volatile使用场景
linux·服务器·c语言·开发语言·c++
hz_zhangrl20 小时前
CCF-GESP 等级考试 2026年3月认证C++一级真题解析
开发语言·c++·gesp·gesp2026年3月·gespc++一级
Liu6288821 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
IT猿手21 小时前
基于控制障碍函数的多无人机编队动态避障控制方法研究,MATLAB代码
开发语言·matlab·无人机·openclaw·多无人机动态避障路径规划·无人机编队
AI科技星21 小时前
全尺度角速度统一:基于 v ≡ c 的纯推导与验证
c语言·开发语言·人工智能·opencv·算法·机器学习·数据挖掘