必看!C++教程大全(收藏级)
作者:互联网
一、前言
这套C++教程能够很好的帮助你入门,让你掌握C++基础并且打开通向高级C++工程师的大门,通俗易懂深入浅出是这套教程最大的特点,让你能够很轻松地学习C++,还有更多详细进阶教程等你!
二、前面的相关教程
1.https://www.jianshu.com/p/8daec4f04d05
2.https://www.jianshu.com/p/f0b1f52abf77
3.https://www.jianshu.com/p/06dd41023471
4.https://www.jianshu.com/p/c443b461c9c4
5.https://www.jianshu.com/p/d02ba7ec3f30
6.https://www.jianshu.com/p/f93168dd6e2f
7.https://www.jianshu.com/p/491125176a1c
8.https://www.jianshu.com/p/6e1b4db53b24
9.更多详细进阶教程也可以关注微信公众号 “C和C加加” 回复“ZXC”即可获取
三、文件操作
文件-指存储在外部介质上的数据集合,文件按照数据的组织形式不一样,分为两种:ASCII文件(文本/字符),二进制文件(内部格式/字节)ASCII文件输出还是二进制文件,数据形式一样,对于数值数据,输出不同.
1.文件类和对象
C++ 标准类库中有三个类可以用于文件操作,它们统称为文件流类。这三个类是:
ifstream:输入流类,用于从文件中读取数据。
ofstream:输出流类,用于向文件中写人数据。
fstream:输入/输出流类,既可用于从文件中读取数据,又可用于 向文件中写人数据。
文件流对象定义:
#include <fstream>
ifstream in;
ofstream out;
fstream inout;
2.打开文件
打开文件的目的:建立对象与文件的关联,指明文件使用方式
打开文件的两种方式:open函数和构造函数
open函数:void open(const char* szFileName, int mode);
ios::binary 可以和其他模式标记组合使用,例如:
ios::in | ios::binary表示用二进制模式,以读取的方式打开文件。
ios::out | ios::binary表示用二进制模式,以写入的方式打开文件。
流类的构造函数
eg:ifstream::ifstream (const char* szFileName, int mode = ios::in, int);
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile("c:\\tmp\\test.txt", ios::in);
if (inFile)
inFile.close();
else
cout << "test.txt doesn't exist" << endl;
ofstream oFile("test1.txt", ios::out);
if (!oFile)
cout << "error 1";
else
oFile.close();
fstream oFile2("tmp\\test2.txt", ios::out | ios::in);
if (!oFile2)
cout << "error 2";
else
oFile.close();
return 0;
}
3.文本文件的读写
对于文本文件,可以使用 cin、cout 读写。
eg:编写一个程序,将文件 i.txt 中的整数倒序输出到 o.txt。(12 34 56 78 90 -> 90 78 56 34 12)
#include <iostream>
#include <fstream>
using namespace std;
int arr[100];
int main()
{
int num = 0;
ifstream inFile("i.txt", ios::in);//文本模式打开
if (!inFile)
return 0;//打开失败
ofstream outFile("o.txt",ios::out);
if (!outFile)
{
outFile.close();
return 0;
}
int x;
while (inFile >> x)
arr[num++] = x;
for (int i = num - 1; i >= 0; i--)
outFile << arr[i] << " ";
inFile.close();
outFile.close();
return 0;
}
4.二进制文件的读写
用文本方式存储信息不但浪费空间,而且不便于检索。
二进制文件中,信息都占用 sizeof(对象名) 个字节;文本文件中类的成员数据所占用的字节数不同,占用空间一般比二进制的大。
ostream::write 成员函数:ostream & write(char* buffer, int count);
class Person
{
public:
char m_name[20];
int m_age;
};
int main()
{
Person p;
ofstream outFile("o.bin", ios::out | ios::binary);
while (cin >> p.m_name >> p.m_age)
outFile.write((char*)&p, sizeof(p));//强制类型转换
outFile.close();
//heiren 烫烫烫烫烫烫啼
return 0;
}
istream::read 成员函数:istream & read(char* buffer, int count);
Person p;
ifstream inFile("o.bin", ios::in | ios::binary); //二进制读方式打开
if (!inFile)
return 0;//打开文件失败
while (inFile.read((char *)&p, sizeof(p)))
cout << p.m_name << " " << p.m_age << endl;
inFile.close();
文件流类的 put 和 get 成员函数
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile("a.txt", ios::binary | ios::in);
if (!inFile)
return 0;
ofstream outFile("b.txt", ios::binary | ios::out);
if (!outFile)
{
inFile.close();
return 0;
}
char c;
while (inFile.get(c)) //每读一个字符
outFile.put(c); //写一个字符
outFile.close();
inFile.close();
return 0;
}
一个字节一个字节地读写,不如一次读写一片内存区域快。每次读写的字节数最好是 512 的整数倍
5.移动和获取文件读写指针
- ifstream 类和 fstream 类有 seekg 成员函数,可以设置文件读指针的位置;
- ofstream 类和 fstream 类有 seekp 成员函数,可以设置文件写指针的位置。
- ifstream 类和 fstream 类还有 tellg 成员函数,能够返回文件读指针的位置;
- ofstream 类和 fstream 类还有 tellp 成员函数,能够返回文件写指针的位置。
函数原型
ostream & seekp (int offset, int mode);
istream & seekg (int offset, int mode);
//mode有三种:ios::beg-开头往后offset(>=0)字节 ios::cur-当前往前(<=0)/后(>=0)offset字节 ios::end-末尾往前(<=0)offect字节
int tellg();
int tellp();
//seekg 函数将文件读指针定位到文件尾部,再用 tellg 函数获取文件读指针的位置,此位置即为文件长度
举个栗子:折半查找文件,name等于“Heiren”
#include <iostream>
#include <fstream>
//#include <vector>
//#include<cstring>
using namespace std;
class Person
{
public:
char m_name[20];
int m_age;
};
int main()
{
Person p;
ifstream ioFile("p.bin", ios::in | ios::out);//用既读又写的方式打开
if (!ioFile)
return 0;
ioFile.seekg(0, ios::end); //定位读指针到文件尾部,以便用以后tellg 获取文件长度
int L = 0, R; // L是折半查找范围内第一个记录的序号
// R是折半查找范围内最后一个记录的序号
R = ioFile.tellg() / sizeof(Person) - 1;
do {
int mid = (L + R) / 2;
ioFile.seekg(mid *sizeof(Person), ios::beg);
ioFile.read((char *)&p, sizeof(p));
int tmp = strcmp(p.m_name, "Heiren");
if (tmp == 0)
{
cout << p.m_name << " " << p.m_age;
break;
}
else if (tmp > 0)
R = mid - 1;
else
L = mid + 1;
} while (L <= R);
ioFile.close();
system("pause");
return 0;
}
6.文本文件和二进制文件打开方式的区别
UNIX/Linux 平台中,用文本方式或二进制方式打开文件没有任何区别。
在 UNIX/Linux 平台中,文本文件以\n(ASCII 码为 0x0a)作为换行符号;而在 Windows 平台中,文本文件以连在一起的\r\n(\r的 ASCII 码是 0x0d)作为换行符号。
在 Windows 平台中,如果以文本方式打开文件,当读取文件时,系统会将文件中所有的\r\n转换成一个字符\n,如果文件中有连续的两个字节是 0x0d0a,则系统会丢弃前面的 0x0d 这个字节,只读入 0x0a。当写入文件时,系统会将\n转换成\r\n写入。
用二进制方式打开文件总是最保险的。
7. 更多详细进阶教程可以关注微信公众号 “C和C加加” 回复“ZXC”即可获取
未完待续~~~~
标签:文件,教程,ifstream,必看,int,ios,C++,outFile,inFile 来源: https://blog.51cto.com/u_15174292/2928218