1 通过fstream形式
1.1 步骤
整体的步骤大概有以下:
- 包含头文件<fstream>
- 创建对象
- 打开文件
- 读写文件
- 关闭文件
1.1.1 打开文件的选项
- ios::in 读文件
- ios::out 写文件
- ios::ate 文件尾
- ios::app 追加方式打开文件
- ios::trunc 如果文件存在,先删除再创建
- ios::binary 二进制
各种方式的组合使用或( | )操作符
1.1.2 ofstream示例
#include <stdio.h>
#include <fstream> //1.include head file
using namespace std; //it's necessary
int main () {
ofstream writeFile; //2.get file instance
writeFile.open("testFile.txt", ios::out); //3.open the file in mode xxx
writeFile << "hello world" << endl; //4.write sth in the file
writeFile.close(); //5.close the file
return 0;
}
1.1.3 ifstream示例
#include <iostream>
#include <fstream> //1.include head file
using namespace std; //it's necessary
int main () {
ifstream readFile; //2.get file instance
readFile.open("testFile.txt", ios::in); //3.open the file in mode xxx & judge the open result
if (!readFile.is_open()) {
cout << "open error" << endl;
return 0;
}
char dataBuf[1024] = { 0 }; //4.create a data buffer & read the data from the file
int dataBufSize = sizeof(dataBuf) / sizeof(dataBuf[0]);
readFileData(readFile, dataBuf, dataBufSize);
cout << dataBuf;
readFile.close(); //5.close the file
return 0;
}
读文件有四种方式, 所以写成了一个readFileData函数.
以下为读取方法:
1. 一次读一个单词, 单词结束标志是空格或者换行
void readFileData (ifstream &file, char* dataBuffer, int dataBufferSize) {
while (file >> dataBuffer) {
cout << dataBuffer << endl;
}
}
2. 一次读一行, 读到换行标志为止
void readFileData (ifstream &file, char* dataBuffer, int dataBufferSize) {
while (file.getline(dataBuffer, dataBufferSize)) {
cout << dataBuffer <<endl;
}
}
3. string法
#include <string>
void readFileData (ifstream &file, char* dataBuffer, int dataBufferSize) {
string strBuffer;
while (getline(file, strBuffer)) {
cout << strBuffer <<endl;
}
}
4. 一个字符一个字符读法
void readFileData (ifstream &file, char* dataBuffer, int dataBufferSize) {
char ch;
while ((ch = file.get()) != EOF) {
cout << ch;
}
}
1.1.3 二进制文件读写
- 写
#include <iostream>
#include <string>
#include <fstream> //1.include head file
using namespace std; //it's necessary
class People {
public:
char m_name;
int m_age;
};
int main () {
ofstream binaryWriteFile; //2.get file instance
binaryWriteFile.open("binaryTestFile.bin",
ios::write | ios::binary); //3.open the file in mode ios::write | ios::binary
People p;
p.m_age = 18;
p.m_name = 'B';
binaryWriteFile.write((const char *)&p, //4.write the data to the binary file, there are two
sizeof(People)); //params, data address(need to be transformed to
//const char *) & the data size.
binaryWriteFile.close(); //5.close the file
return 0;
}
- 读
#include <iostream>
#include <string>
#include <fstream> //1.include head file
using namespace std; //it's necessary
class People {
public:
char m_name;
int m_age;
};
int main () {
ifstream binaryReadFile; //2.get file instance
binaryReadFile.open("binaryTestFile.bin",
ios::in | ios::binary); //3.open the file in mode ios::in | ios::binary
if (!binaryReadFile.is_open()) {
cout << "open error" << endl;
return 0;
}
People p;
binaryReadFile.read((char *)&p, //4.read the data from the binary file, there are two
sizeof(People)); //params, data address(need to be transformed to char *)
//& the data size.
cout << "name: " << p.m_name << endl;
cout << "age: " << p.m_age << endl;
binaryReadFile.close(); //5.close the file
return 0;
}
读的运行结果:
2 针对目录的操作
在头文件<sys/types.h>以及<dirent.h>中.
2.1 opendir
如果要想对于目录进行相关操作, 需要目录路径的字符串, 比如是"data/files", 就可以如下操作:
DIR * dir = opendir("data/files");
DIR结构体的定义为:
struct __dirstream
{
void *__fd;
char *__data;
int __entry_data;
char *__ptr;
int __entry_ptr;
size_t __allocation;
size_t __size;
__libc_lock_define (, __lock)
};
typedef struct __dirstream DIR;
返回的DIR型结构体的指针dir可以直接用于如下函数进行后续操作:
struct dirent *readdir(DIR *dp);
void rewinddir(DIR *dp);
int closedir(DIR *dp);
long telldir(DIR *dp);
void seekdir(DIR *dp,long loc);
2.2 dirent结构体
前文中, 在通过opendir获取到了DIR结构体指针的dir之后, 通过以下方式获取到目录的详细信息, 也就是dirent结构体类型的指针:
pDirent = readdir(dir);
dirent结构体的结构为:
struct dirent
{
long d_ino; /* inode number 索引节点号 */
off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
unsigned short d_reclen; /* length of this d_name 文件名长 */
unsigned char d_type; /* the type of d_name 文件类型 */
char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */
}
需要注意的是, 这里的pDirent代表的是第一个文件, 操作完之后如果再用pDirent去接收readdir(dir)的结果就是第二个文件了, 所以可以用迭代的方式遍历目录下的所有文件的文件名.