ch7 文本数据处理¶
1. 文件操作¶
C++
#include <iostream>
#include <fstream> // 包含文件操作的头文件
using namespace std;
int main()
{
// 创建一个输入文件的操作对象 fin,并打开名为 log.txt 的文本文件
// 如果需要写入文件,则用 ofstream 定义输出文件的操作对象
ifstream fin("log.txt"); // ifstream 表示 input file stream
// 也可以用 fin.open("log.txt"); 打开文件
int count = 0; // 记录数,初始值为0
while (!fin.eof()) // eof 表示 end of file
{
int year, month, day, hour, minute, second;
char tmp, id[20], operation[10];
// 通过 fin 获取文本文件中的内容
fin >> year >> tmp >> month >> tmp >> day; // 2015/4/21
fin >> hour >> tmp >> minute >> tmp >> second; // 11:16:16
fin >> id; // 40dbae14f777cdd
fin >> operation; // LOGIN
count++; // 读到一行,记录数加1
}
fin.close(); // 关闭文件
cout << count << endl; // 输出记录数
return 0;
}
2. 字符串¶
字符数组的两种等价的初始化方法:
字符串可以整体输入输出:
常用字符串操作函数:
C++
#include <cstring> // 需要包含字符串操作相关头文件
int len = strlen(str); // 求字符串 str 的长度,注意不是数组长度
strcpy(str1, str2); // 将字符串 str2 复制到 str1 中
strcmp(str1, str2); // 比较字符串 str1 和 str2 的大小,按照字典序进行比较
strcat(str1, str2); // 将字符串 str2 拼接到 str1 后面
字符串数组:
3. 结构体¶
结构体实际上是一种自定义数据类型。