目录
一.向一个文本文件中写入数据:
二.从一个文本文件中读取数据,并对读取的数据做简单处理。
三.二进制文件的写入操作
四.对二进制文件的读取操作
五. 举例
六.IO操作的简单应用(代码和举例都在更新和完善中)
一.向一个文本文件中写入数据:
#include <iostream>
#include <fstream>//两个类的结合
//fstream是ifstream和ofstream的结合
//既包括了输入操作也包括输出操作
using namespace std;
int main()
{
int a[5];
//ofstream outfile("d:\\桌面\\0209实验.txt", ios::out);
//d:\\表示d盘根目录
//ios::out如果文件不存在,会按照定义的名称创建文件。
//如果原来存在文件,会清空;如果不存在,创建
ofstream outfile("d:\\桌面\\0209实验.txt", ios::app);
//以输出方式打开,数据追加到文件末尾
if (!outfile)
{
cout << "open error" << endl;
return 0;
}
else
{
cout << "请输入数据" << endl;
for (int i = 0; i < 5; i++)
{
cin >> a[i];
outfile << a[i] << " ";
}
}
outfile.close();//关闭文件输出流
return 0;
}
编译器运行结果:(例如我们输入1 2 3 4 5)
桌面上会自动创建一个txt文件。
txt文件会因为这段程序,而被写入1 2 3 4 5 这组数据,效果如下:
二.从一个文本文件中读取数据,并对读取的数据做简单处理。
#include <iostream>
#include <fstream>//两个类的结合
//fstream是ifstream和ofstream的结合
//既包括了输入操作也包括输出操作
using namespace std;
int main()
{
int a[20];
int maxs = -1, index = 0;
ifstream infile("d:\\桌面\\0209shiyan.txt", ios::in);
if (!infile)
{
cout << "oper error" << endl;
return 0;
}
else
{
for (int i = 0; i < 5; i++)
{
infile >> a[i];
cout << a[i] << " ";
if (a[i] > maxs)
{
maxs = a[i];
index = i;
}
}
cout << endl << "maxs is " << maxs << endl;
cout << "pos is " << index << endl;
}
return 0;
}
编译器运行结果:
因为是读取“一”中写入的数据而没有对0209shiyan.txt这个文本文件中的数据做任何修改,因此文本文件中的数据不变。
三.二进制文件的写入操作
#include <iostream>
#include <fstream>
using namespace std;
struct student
{
string name;
int age;
char sex;
};
int main()
{
student stu[3] = { "li",18,'F',"wu",19,'M',"zhang",17,'F' };
ofstream outfile("d:\\桌面\\0209shiyan.txt",ios::out, ios::binary);
//ios::binary 是“将数据转变为二进制的形式储存在文件中”的意思
if (!outfile)
{
cout << "open error" << endl;
exit(0);//代表终止所有程序的意思,和return 0; 功能类似
}
else
{
for (int i = 0; i < 3; i++)
{
outfile.write((char*)(&stu[i]), sizeof(stu[i]));
}
//write函数:write函数第一个参数必须是char*指针类型的数据,
//如果不是这个类型的数据,需要进行强制转换
//第二个参数是写入数据的大小。
outfile.close();
//关闭文件有终止缓冲区运行的作用,文本文件用完之后就要写close函数。
}
return 0;
}
因为程序没有任何实质性的编译器层面的输出,所以编译器显示为0输出。
由于是二进制的形式储存,所以文本文件执行显示层面展示的是一堆乱码,并且每次查看都不太一样。
四.对二进制文件的读取操作
#include <iostream>
#include <fstream>
using namespace std;
struct student
{
string name;
int age;
char sex;
};
int main()
{
student stu[3];
ifstream infile("d:\\桌面\\0209shiyan.txt", ios::out, ios::binary);
if (!infile)
{
cout << "open error" << endl;
exit(0);
}
else
{
for (int i = 0; i < 3; i++)
{
//outfile.write((char*)(&stu[i]), sizeof(stu[i]));
infile.read((char*)&stu[i], sizeof(stu[i]));
}
//read函数:与write函数参数格式要求相同。
//第一个参数 要求必须是char*类型的,第二个参数是读取数据的大小。
infile.close();//infile用完了,就要close它。
//上述代码实现的功能是将二进制文件从文件中读取出来,并储存在了程序的数组之中。
//将程序中的数组存储的内容输出,看看读入的数据是否正确?
for (int i = 0; i < 3; i++)
{
cout << "NO." << i + 1 << endl;
cout << "name:" << stu[i].name << endl;
cout << "age:" << stu[i].age << endl;
cout << "sex:" << stu[i].sex << endl;
}
}
return 0;
}
运行效果:
发现读取到的数据和“三”中给文本文件写入的数据完全一致。
由于本程序只涉及到“读取”操作,因此没有对0209shiyan.txt中的内容做任何修改。
五. 举例
从编译器的运行框中输入一串字符串,挑出其中属于字母的字符,在编译器运行框中输出的同时,将输出结果也储存在一个文本文档中;读取该文本文档中的数据,并将所有小写字母都转化为大写字母,将运行结果储存在另一个文本文档中。
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
void save_to_file()
{
ofstream outfile("d:\\桌面\\0209shiyan.txt", ios::out);
//如果不指定位置,不同编译器存储txt文件的位置不同。
if (!outfile)
{
cout << "open error" << endl;
exit(0);
}
else
{
char a[1000];
cin.getline(a, 1000, '\n');
for (int i = 0; i < strlen(a); i++)
if (a[i] >= 'a' && a[i] <= 'z' || a[i] >= 'A' && a[i] <= 'Z')
{
outfile.put(a[i]);
//outfile.put函数:将一个参数携带的数据传入到文本文件中并储存
cout << a[i] << " ";//同时将数据送达显示器
}
cout << endl;
outfile.close();
}
}
void get_from_file()
{
ifstream infile("d:\\桌面\\0209shiyan.txt", ios::in);//从0209shiyan.txt中读入数据
ofstream outfile("d:\\桌面\\0209shiyan1.txt", ios::out);//将结果的数据传出,写到0209shiyan1.txt中
if (!infile||!outfile)
{
cout << "open error" << endl;
exit(0);
}
else
{
char ch;
while (infile.get(ch))
{
if (ch >= 'a' && ch <= 'z')
{
ch -= 32;//将小写字母转化为大写字母
outfile.put(ch);//储存。
}
}
infile.close();
outfile.close();
}
}
int main()
{
save_to_file();
get_from_file();
return 0;
}
运行结果:
文本文档中的效果:
程序使得计算机自主创建了一个文本文档,被我命名为0209shiyan1.txt。
在0209shiyan.txt中
效果是这样的:
在0209shiyan1.txt中,效果是这样的:
六.IO操作的简单应用(代码和举例都在更新和完善中)
一.对《06笔记 输入输出流重载》中代码补充IO操作:
#include <iostream>
#include<fstream>
using namespace std;
class complex
{
private:
double real,imag;
public:
complex() { real = 0; imag = 0; }
complex(double a, double b) { real = a; imag = b; }
complex operator + (complex&);
void showcomplex(){ cout << "(" << real << "," << imag << "i)" << endl; }
//friend complex operator +(complex&, complex&);
friend istream& operator>>(istream&, complex&);//输入流重载的声明
friend ostream& operator<<(ostream&, complex&);//输出流重载的声明
};
inline complex complex::operator+(complex& a)
//函数的返回值类型 上一级::下一级 运算符(数据类型 变量名称)
{
complex c;
c.real = this->real + a.real;
c.imag = this->imag + a.imag;
return c;
}
//写法2
/*
complex operator + (complex& a, complex& b)
{
complex c;
c.real = a.real + b.real;
c.imag = a.imag + b.imag;
return c;
}
*/
//输入流重载
//istream 输入流 ostream 输出流
istream& operator >>(istream &input, complex& c)
{
cout << "请分别输入一个复数的实部和虚部:" << endl;
input >> c.real >> c.imag;
return input;
}
//输出流重载
ostream& operator<<(ostream& output, complex& c)
{
output << "(" << c.real << "," << c.imag << "i)" << endl;
return output;
}
int main()
{
ofstream outfile("d:\\桌面\\complex.txt", ios::out);
ifstream infile("d:\\桌面\\zhengshu.txt", ios::in);
//声明:只有ostream才可以自主创建新的文本文件,
//istream不能,需要用户提前创建并命名,并事先写入数据
complex a, b, c;
infile>> a >> b;
c = a + b;
//infile.close();
a.showcomplex();
b.showcomplex();
c.showcomplex();
cout << c;
outfile << a << endl;
outfile << b << endl;
outfile << c << endl;
infile.close();
outfile.close();
return 0;
}
声明:
//声明:只有ostream才可以自主创建新的文本文件,
//istream不能,需要用户提前创建并命名,并事先写入数据
实现部分:
由上述声明,笔者自行创建了一个文本文件叫做zhengshu.txt,写入数据 1 2 3 4
效果如下:
现在对原代码进行编译,运行效果为:
此时,文本文件complex.txt中的效果如下: