C++学习-IO流操作

本文通过三个C++程序示例介绍了如何使用文件流进行文本文件和二进制文件的操作,包括文件的读取、写入及定位等,并展示了如何利用结构体来组织数据并进行文件存储。



#include <iostream>
using namespace std;
int main()
{
	cout << 1 << endl;
	cerr << 2 << endl;
	clog << 3 << endl;
	
	cout.put('I');
	cout << endl;
	cout.write("I love you", 8);
	cout << endl;
	system("pause");
	return 0;
}


#include<iostream>
#include <fstream>     //加上这样一个头文件,文件操作
using namespace std;

int main()
{
	fstream file;
	file.open("test.txt", ios::out);
	if (!file)
	{
		cout << "打开文件失败" << endl;
		exit(0);
	}
	file << "I love you" << endl;
	file << "but you don't love me" << endl;
	file.close();
	fstream file1;
	file1.open("test.txt", ios::in);
	if (!file1)
	{
		cout << "打开文件失败" << endl;
		exit(0);
	}
	char str[100];
	while (!file1.eof())
	{
		file1.getline(str, sizeof(str));
		cout << str << endl;
	}
	file1.close();
	system("pause");
	return 0;
}


#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct student
{
	char name[10];
	int num;
	int age;
	char sex;
};


int main()
{
	student mystudent[3] = { "victor", 1001, 98, 'w',
		"我爱学习", 1002, 1, 'w',
		"落尘孤鹜", 1003, 55, 'm' };
	//fstream---ofstream---ifstream
	ofstream outfile("file.txt", ios::binary);
	if (!outfile)
	{
		cerr << "打开文件失败" << endl;
		return 0;
	}
	for (int i = 0; i < 3; i++)
	{
		//write("指针",大小)
		outfile.write((char *)&mystudent[i], sizeof(mystudent[i]));
	}
	outfile.close();
	ifstream infile("file.txt", ios::binary);
	if (!infile)
	{
		cerr << "打开文件失败" << endl;
		return 0;
	}
	student stu[3];
	for (int i = 0; i < 3; i++)
	{
		infile.read((char *)&stu[i], sizeof(stu[i]));
	}

	infile.close();
	cout << setw(10) << "姓名"
		<< setw(7) << "学号"
		<< setw(6) << "年龄"
		<< setw(6) << "性别" << endl;
	for (int i = 0; i < 3; i++)
	{
		cout << setw(10) << stu[i].name;
		cout << setw(7) << stu[i].num;
		cout << setw(6) << stu[i].age;
		cout << setw(6) << stu[i].sex << endl;
	}
	system("pause");
	return 0;
}


#include <iostream>
#include <fstream>
using namespace std;
struct student
{
	char name[10];
	int num;
	int age;
};
student stu[5] =
{
	"哇哦", 1001, 18,
	"孤鹜", 1002, 19,
	"闲者", 1003, 20,
	"路伴", 1004, 21,
	"victor", 1005, 22
};
int main()
{
	student mystudent;
	fstream file;
	//ios::binary  二进制形式  
	file.open("file.txt", ios::out | ios::in | ios::binary);
	if (!file)
	{
		cerr << "打开文件失败" << endl;
		exit(0);
	}
	for (int i = 0; i < 5; i++)
	{
		file.write((char *)&stu[i], sizeof(student));   //写的方式是怎样
	}
	file.seekp(sizeof(student)* 4);  //移动到第5个人位置
	file.read((char*)&mystudent, sizeof(student));
	cout << mystudent.name << "\t" << mystudent.num << "\t" << mystudent.age<<endl;

	file.seekp(sizeof(student)* 1, ios::beg);
	file.read((char*)&mystudent, sizeof(student));
	cout << mystudent.name << "\t" << mystudent.num << "\t" << mystudent.age << endl;

	file.close();
	system("pause"); 
	
	return 0;
}


 一、ASCII 输出   为了使用下面的方法, 你必须包含头文件(译者注:在标准C++中,已经使用取 代,所有的C++标准头文件都是无后缀的。)。这是 的一个扩展集, 提供有缓 冲的文件输入输出操作. 事实上, 已经被包含了, 所以你不必包含所有这两个 文件, 如果你想显式包含他们,那随便你。我们从文件操作类的设计开始, 我会讲解如何进行ASCII I/O 操作。如果你猜是"fstream," 恭喜你答对了! 但这篇文章介绍的方法,我们分别使用"ifstream"?和 "ofstream" 来作输入输出。   如果你用过标准控制台"cin"?和 "cout," 那现在的事情对你来说很简单。 我们现在开始讲输出部 分,首先声明一个类对象。 ofstream fout;   这就可以了,不过你要打开一个文件的话, 必须像这样调用ofstream::open()。 fout.open("output.txt");   你也可以把文件名作为构造参数来打开一个文件. ofstream fout("output.txt");   这是我们使用的方法, 因为这样创建和打开一个文件看起来更简单. 顺便说一句, 如果你要打开的文 件不存在,它会为你创建一个, 所以不用担心文件创建的问题. 现在就输出到文件,看起来和"cout"的操 作很像。 对不了解控制台输出"cout"的人, 这里有个例子。 int num = 150; char name[] = "John Doe"; fout << "Here is a number: " << num << " "; fout << "Now here is a string: " << name << " ";   现在保存文件,你必须关闭文件,或者回写文件缓冲. 文件关闭之后就不能再操作了, 所以只有在你 不再操作这个文件的时候才调用它,它会自动保存文件。 回写缓冲区会在保持文件打开的情况下保存文 件, 所以只要有必要就使用它。回写看起来像另一次输出, 然后调用方法关闭。像这样: fout << flush; fout.close();    现在你用文本编辑器打开文件,内容看起来是这样:   Here is a number: 150 Now here is a string: John Doe   很简单吧! 现在继续文件输入, 需要一点技巧, 所以先确认你已经明白了操作,对 "<>" 比较熟悉了, 因为你接下来还要用到他们。继续…   二、ASCII 输入   输入和"cin" 很像. 和刚刚讨论的输出很像, 但你要考虑几件事情。在我们开始复杂的内容之前 , 先看一个文本:   12 GameDev 15.45 L This is really awesome!   为了打开这个文件,你必须创建一个in-stream对象,?像这样。 ifstream fin("input.txt");   现在读入前四行. 你还记得怎么用"<<" 操作符往里插入变量和符号吧?好,?在 "<>" (提取) 操作符. 使用方法是一样的. 看这个代码片段.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值