第七周学习:文件读写

程序写入文件
  1. 用ofstream对象来管理输出流
  2. 该对象与特定的文件关联起来
  3. 使用cout一样的方法使用该对象,只不过输出将进入文件而不是screen

a. 要包含header fstream
b. 声明一个ofstream对象 (out file)
c. 使用open函数来将对象与文件关联

程序将输出的数据放入缓冲区,当缓冲区满的时候将缓冲区内容传输到目标文件。如果有两个对象,则会创建两个缓冲区。

文件输出到程序
  1. 用ifstream对象管理输出流
  2. 对象与文件connect
  3. 使用cin一样的方法使用该对象,只不过输入不是从键盘来,而是从文件来

输入流或输出流对象过期时,要关闭到文件的链接。
/// a simple program;

	ofstream fout;
	fout.open("test3.txt");
	fout<<"This is output to test3.txt";
	fout.close();  //用ifstream之前要关闭 
	
	ifstream fin;
	fin.open("test3.txt");
	char ch;
	while(fin.get(ch))
		cout<<ch;
	fin.close();

输出结果:
在这里插入图片描述

文件模式

常量 含义
ios::in 打开文件,以便读取
ios::out 打开文件,以便写入
ios::ate 打开文件,移到尾部
ios::app 追加到文件尾部
ios::binary 二进制文件
ios::trunc 截短文件

ifstream对象默认以ios::in作为参数:
ofstream对象默认以ios::out|ios::trunc作为参数

/// 追加文件

char ch;
	ifstream fin(str) ;
	if(fin) 
	{
    
    
		cout<<"Here are contents of "<<str<<endl;
		while(fin.get(ch))
			cout<<ch;
		fin.close(); 
	}
	
	ofstream fout("test4.txt",ios::out|ios::app);
	if(!fout)
	{
    
    
		cerr<<"cant open "<<str<<endl;
		exit(EXIT_FAILURE);
	}
	cout<<"Enter a name :\n";
	string name;
	while(getline(cin,name)&&name.size()>0)
	{
    
    
		fout<<name<<endl;
	}
	
	fin.clear();
	fin.open(str);
	if(fin)
	{
    
    
		cout<<"Here are new contentx of "<<str<<endl;
		while(fin.get(ch))
			cout<<ch;
		fin.close();
	}
	cout<<"DONE\n";

首先因为没有创建文件,所以不打开文件,到ofstream对象,写入,然后读取输出,然后输出,新写入,输出…

//第一次运行
在这里插入图片描述
在这里插入图片描述

//第二次运行
在这里插入图片描述
在这里插入图片描述
这就是ios::app!

//二进制文件

文字格式便于读取,可以用编译器或字处理器读取和编辑文本文件,可以很方便的将文本文件从一个system传输到另一个system。
而二进制表示得比较精确,二进制是计算机内部语言,不会有转换误差或舍入误差,且保存数据速度更快,不需要转换而且可以大块存储数据。
有时候如果有存储字符串啊什么的,如果是二进制形式也方便查找。

  • 使用二进制模式
  • 使用write、read等成员函数
    istream& write( char*, long n)
    istream& read(char* , long n)
    write就是将char*的n字节内容写入到file,read则是从文件读取n字节内容到file

/// 学生姓名和年龄

	struct Student
	{
    
    
		char name[20];
		int age;
	 } ;
	 Student s1;
	
	ofstream fout("test5.txt",ios::out|ios::binary); //二进制模式
	cout<<"Enter students' name + age\n";
	while(cin.getline(s1.name,20,' '))
	{
    
    

		cin>>s1.age;
		fout.write((char*)&s1,sizeof(s1));  //强制类型转换
		eatline();
		cout<<"Enter next\n";
	}
	fout.close();
	
	ifstream fin("test5.txt",ios::in|ios::binary);
	if(fin)
	{
    
    
		cout<<"The message in test5.txt:\n";
		while(fin.read((char*)&s1,sizeof(s1)))
			cout<<"Name: "<<s1.name<<" Age: "<<s1.age<<endl;
	}
	fin.close();

再额外加上一段代码:修改第三个人名字为handsome

fstream iofile("test5.txt",ios::in|ios::out|ios::binary) ;
	if(!iofile)
	{
    
    
		cerr<<"error";
		return 0;
	}
	iofile.seekp(2*sizeof(s1),ios::beg);  
	//将指针指向第n个位置,beg为从begin位置开始
	iofile.write("Handsome",strlen("Handsome")+1) ;
	iofile.seekp(0,ios::beg);
	cout<<"After modifying:\n\n";
	while(iofile.read((char*)&s1,sizeof(s1)))
	{
    
    
		cout<<"Name: "<<s1.name<<"\t Age:"<<s1.age<<endl;
	}

补:
seekp(long n)就是将当前指针指向n个字节位置处
tellp()取得当前指针位置
ios::beg 从文件头部开始
ios::cur从当前位置开始数
ios::end从尾部位置开始(n要为负数了就)

输入与输出结果:
在这里插入图片描述
在这里插入图片描述
字符可以显示出来的,但是数字不得行。

猜你喜欢

转载自blog.csdn.net/ZmJ6666/article/details/108697669