C++ 大学MOOC 北大课程(郭炜老师)听课整理 第七周_01(输入输出文件操作)

输入输出相关类

1)cin对应于标准输入流,用于从键盘读取数据,也可以重定向为从文件中读取数据
2)cout对应于标准输出流,用于向屏幕输出数据,也可以重定向为向文件中写入数据
3)cerr与clog都对应于标准错误输出流,用于向屏幕输出出错信息
例1:输入重定向

int main(){
 double f;
 int n;
 freopen("t.txt","r",stdin);  //重定向输入语句
 cin >> f >> n;
 cout << f << ',' << n;
 return 0;
}

假设 t.txt 文件中存入数据 1 2
则会输出:

1 2

例2:输出重定向

int main(){
 int x, y;
 cin >> x >> y;
 freopen("o.txt", "w", stdout);  //重定向输出语句
 if (y == 0)  //如果 y 等于0
  cerr << "error." << endl;  //则用cerr在屏幕上显示错误信息
 else
  cout << x / y;  //否则用cout将计算结果写入到文件 o.txt 中
 return 0;
}

4)用如下方式来判断输入流是否结束

int x;
while (cin >> x){ }

若用freopen(“t.txt”,“r”,stdin);重定向输入语句,那么读到文件尾部就结束输入
若从键盘输入,则用 Ctrl + Z 代表输入结束
5)读入字符串
istream& getline (char* s, streamsize n );
从输入流中读取 n-1 个字符存入 s 中,读到’\n’或读入字符数达到 n 个为止
istream& getline (char* s, streamsize n, char delim );
从输入流中读取 n-1 个字符存入 s 中,读到字符 delim 或读入字符数达到 n 个为止
两个函数都会在结束时自动在 s 结尾添加’\0’
‘\n’ 或 delim 字符不会读入但是会从输入流中取走
可以使用 if(!cin.getline(…));语句判断读入
例如:

int main(){
 int x;
 char s[100];
 cin >> x;
 cin.getline(s, 90);
 cout << s << endl;
 return 0;
}

输入:

12xing

输出:

xing

文件操作

创建文件

1)首先包含头文件:#include
2)直接创建语句:ofstream outfile(“out.txt”,ios::out|ios::binary)
“out.txt” // 创建文件名,也可以时文件路径
ios::out //文件打开方式
ios::out //输出到文件,删除原有内容
ios::app //输出到文件,保留原有内容,总是在尾部添加
ios::binary //以二进制格式打开文件
3)先创建ofstream对象,在使用open函数
ofstream fin;
fin.open(“out.txt”,ios::out|ios::binary);
4)判断问价是否成功创建打开的方式
if(!fin){cout<<“file open error”}

文件路径

1)绝对路径:
“c:\tmp\pro\out.txt”
2)相对路径
“\tmp\pro\out.txt” // 以当前盘符根目录下建立文件
“tmp\pro\out.txt” //当前文件夹
“…\tmp\pro\out.txt” //当前文件夹的父文件夹
“…\…\tmp\pro\out.txt” //当前文件夹的父文件夹的父文件夹

文件读写指针

1)标志文件当前操作位置,该指针在哪里,读或写操作就在哪里进行
例1:写指针
ofstream outfile(“out.txt”,ios::app) //已添加方式打开文件写指针开始指向文件尾部
long location=outfile.tellp(); //取得写指针的位置
location=10;
outfile.seekp(location); //将写指针移动到第十的字节处
outfile.seekp(location,ios::beg); //从头数location个字节 为正
outfile.seekp(location,ios::cur); //从当前位置数location个字节 为负/正
outfile.seekp(location,ios::end); // 从尾数location个字节 为负
例2:读指针
ifstream outfile(“out.txt”,ios::ate) //打开文件 定位文件指针到文件尾
long location=outfile.tellg(); //取得读指针的位置
location=10;
outfile.seekg(location); //将写指针移动到第十的字节处
outfile.seekg(location,ios::beg); //从头数location个字节 为正
outfile.seekg(location,ios::cur); //从当前位置数location个字节 为负/正
outfile.seekg(location,ios::end); // 从尾数location个字节 为负

字符文件读写

示例代码:

扫描二维码关注公众号,回复: 9211063 查看本文章
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
 vector<int> v;  //定义可变长数组
 ifstream forfile("D:\\in.txt",ios::in);  //打开文件 in.txt 用于读操作 读指针指向开头
 ofstream tofile("D:\\out.txt",ios::out);  //创建文件 out.txt 用于写操作 写指针指向开头
 int x;
 while (forfile >> x)  //从 in.txt 文件中读数据
  v.push_back(x);  //存入数组中
 sort(v.begin(), v.end());  
 for (int i = 0; i < v.size(); ++i)  
  tofile << v[i] << " ";  //将数组中的数据写入 out.txt 文件中
 forfile.close();  //关闭文件
 tofile.close();  //关闭文件
 return 0;
}

二进制文件操作

写操作

1)用到函数:istream& write(const char* s,long n);
2)将内存s地址处n个字节的内容,写入到文件写指针指向的位置,且文件写指针向后移动n个字节
例如:从键盘中输入若干学生的姓名和成绩,逐个写入到二进制文件中

struct student{   //定义结构体 student
 char name[20];
 int score;
};
int main(){
 student s;
 ofstream tofile("D:\\student.dat", ios::out | ios::binary);  //打开文件
 while (cin >> s.name >> s.core)  //键入姓名和成绩
  tofile.write((char*)&s, sizeof(s));  //写入文件中 注意s对象的位置要进行类型转化
 tofile.close();  //必须关闭文件
 return 0;
}

示例键盘输入:
lone 82
Tome 90
Sune 94
文件打开呈现:
lone 烫烫烫烫烫烫烫蘎 Tome 烫烫烫烫烫烫烫蘘 Sune 烫烫烫烫烫烫烫蘜

读操作

1)用到函数:istream& read(char* s,long n);
2)将文件读指针指向的地方的n个字节的内容,读入到内存地址s处,然后将文件读指针向后移动n个字节
例如:读入上述文件内容并打在屏幕上

struct student{
 char name[20];
 int score;
};
int main(){
 student s;
 ifstream fromfile("D:\\student.dat", ios::in | ios::binary);  //以读方式打开文件 student.dat 规定二进制操作形式 读指针指向文件开头
 if (!fromfile){  //判断是否成功打开文件
  cout << "error" << endl;
  return 0;
 }
 while (fromfile.read((char*)&s, sizeof(s))){ //读入文件内容到对象 s 中
  int readedbytes = fromfile.gcount();  //记录刚才读了多少字节
  cout << s.name << " " << s.score << endl;  //输出读到的结果
 }
 fromfile.close();  //关闭文件
 return 0;
}

输出:

lone 82
Tome 90
Sune 94

读写操作

示例1:将文件 student.dat 中的 Sune 改成 Misko

struct student{
 char name[20];
 int score;
};
int main(){
 student s;
 fstream file("D:\\student.dat", ios::in | ios::out | ios::binary); //打开文件 能读 能写 二进制形式
 if (!file){
  cout << "error" << endl;
  return 0;
 }
 file.seekp(2 * sizeof(s), ios::beg); //将写指针的位置移动至 Sune 处
 file.write("Misko", sizeof("Misko") + 1);  //写入内容  包括字符串最后的'\0'字符
 file.seekg(0, ios::beg); //读指针位于文件开头
 while (file.read((char*)&s, sizeof(s)))  
  cout << s.name << " " << s.score << endl;
 file.close(); 
 return 0;
}

输出:

lone 82
Tome 90
Misko 94

示例2:将上述 student.dat 文件内容拷贝到另一个文件中去

struct student{
 char name[20];
 int score;
};
int main(){
 ifstream infile("D:\\student.dat", ios::in | ios::binary);  //打开文件
 if (!infile){
  cout << "Source file open error" << endl;
  return 0;
 }
 ofstream tofile("D:\\New.dat", ios::out | ios::binary);  //打开文件
 if (!tofile){
  cout << "New file open error" << endl;
  infile.close();  //要把之前打开的文件关闭
  return 0;
 }
 char c;
 while (infile.get(c)) //读入一个字节
  tofile.put(c);  //写入一个字节
 infile.close();
 tofile.close();
 return 0;
}
发布了15 篇原创文章 · 获赞 17 · 访问量 592

猜你喜欢

转载自blog.csdn.net/weixin_45644911/article/details/104313811