要用c++++制作一个简易笔记应用,核心在于文件读写与字符串处理。1. 定义结构体note用于存储标题、内容和时间戳;2. 使用分隔符(如###)将每条笔记组织成一行文本存入文件;3. 利用std::fstream进行文件i/o操作,std::string进行字符串解析;4. 增删改操作通过加载文件至内存修改后再重写文件实现;5. 查找功能通过对内存中的vector遍历并使用std::string::find判断关键词匹配;6. 注意规避getline与>>混用陷阱、空行处理及文件路径问题等细节。

用C++制作一个简易笔记应用,主要就是围绕文件读写和字符串处理这两大核心。说白了,就是把你的笔记内容变成一串串文字,存到文件里,需要的时候再从文件里读出来,然后对这些文字进行增删改查。这个过程听起来简单,但里面有些小细节还挺有意思的。

要制作一个这样的应用,我们得先想好笔记怎么存。我个人比较倾向于每条笔记占据文件中的一行,或者是一个固定格式的块,这样方便读取和解析。比如,我们可以把笔记的标题、内容和创建时间用一个特定的分隔符(比如
###
核心的C++组件会是
std::fstream
std::string
立即学习“C++免费学习笔记(深入)”;

具体步骤构想:
struct Note
string title;
string content;
string timestamp;
标题###内容###时间戳
ofstream
ifstream
std::string::find
std::string::substr
Note
std::vector<Note>
std::vector<Note>
std::string::find
std::vector<Note>
vector
vector
高效地组织笔记数据,其实更多的是在思考“数据模型”和“序列化”的问题。对于一个简易笔记应用,我不会去考虑什么复杂的二进制序列化或者数据库,那太重了。最直接、最易于理解的方式就是文本格式。

在我看来,最实用的文本组织方式是分隔符连接的单行记录。就像我前面提到的
标题###内容###时间戳
std::getline
std::string
当然,你也可以选择多行格式,比如每条笔记是一个多行块,用一个特殊的标记(例如
---END_NOTE---
// 概念性的笔记结构
struct Note {
std::string title;
std::string content;
std::string timestamp;
// 将Note对象转换为可存储的字符串格式
std::string toString() const {
// 这里需要注意,如果内容中包含###,会出问题。
// 实际应用中需要更健壮的转义或选择更特殊的不可见分隔符。
return title + "###" + content + "###" + timestamp;
}
// 从字符串解析出Note对象
static Note fromString(const std::string& line) {
Note note;
size_t pos1 = line.find("###");
if (pos1 == std::string::npos) return note; // 错误处理
note.title = line.substr(0, pos1);
size_t pos2 = line.find("###", pos1 + 3); // 从第一个分隔符之后开始找第二个
if (pos2 == std::string::npos) return note;
note.content = line.substr(pos1 + 3, pos2 - (pos1 + 3));
note.timestamp = line.substr(pos2 + 3);
return note;
}
};你看,这个
toString
fromString
做文件I/O,尤其是文本文件,总会遇到一些让人头疼的小问题。这些问题往往不是代码逻辑上的大错,而是细节处理不到位。
fileStream.is_open()
getline()
>>
cin >> someInt;
getline(cin, someString);
>>
getline
cin.ignore()
getline
getline
我个人在写这种小程序时,最常遇到的就是
getline
>>
这部分是真正考验字符串处理能力的地方,也是用户体验的关键。
1. 查找笔记: 查找相对简单。我们已经把所有笔记加载到
std::vector<Note>
vector
// 假设 notes 是 std::vector<Note>
void searchNotes(const std::vector<Note>& notes, const std::string& keyword) {
std::cout << "搜索结果:" << std::endl;
bool found = false;
for (const auto& note : notes) {
// 将关键词和笔记内容都转为小写进行不区分大小写的查找
std::string lowerTitle = note.title;
std::string lowerContent = note.content;
std::string lowerKeyword = keyword;
std::transform(lowerTitle.begin(), lowerTitle.end(), lowerTitle.begin(), ::tolower);
std::transform(lowerContent.begin(), lowerContent.end(), lowerContent.begin(), ::tolower);
std::transform(lowerKeyword.begin(), lowerKeyword.end(), lowerKeyword.begin(), ::tolower);
if (lowerTitle.find(lowerKeyword) != std::string::npos ||
lowerContent.find(lowerKeyword) != std::string::npos) {
std::cout << "标题: " << note.title << std::endl;
std::cout << "内容: " << note.content << std::endl;
std::cout << "时间: " << note.timestamp << std::endl;
std::cout << "--------------------" << std::endl;
found = true;
}
}
if (!found) {
std::cout << "没有找到匹配的笔记。" << std::endl;
}
}这里用到
std::string::find
std::string::npos
2. 编辑笔记: 编辑操作就如我之前所说,是“读入-修改-写回”模式的典型应用。
std::vector<Note>
vector
Note
title
content
std::ofstream
std::ios::trunc
vector
Note
void editNote(std::vector<Note>& notes, int indexToEdit, const std::string& newTitle, const std::string& newContent) {
if (indexToEdit >= 0 && indexToEdit < notes.size()) {
notes[indexToEdit].title = newTitle;
notes[indexToEdit].content = newContent;
// 也可以更新时间戳
// notes[indexToEdit].timestamp = getCurrentTimestamp();
std::cout << "笔记编辑成功!" << std::endl;
// 此时需要调用一个函数来将 notes 重新写入文件
// 例如: saveNotesToFile(notes, "notes.txt");
} else {
std::cout << "无效的笔记编号。" << std::endl;
}
}3. 删除笔记: 删除和编辑的逻辑非常相似,也是“读入-修改-写回”。
std::vector::erase()
vector
Note
void deleteNote(std::vector<Note>& notes, int indexToDelete) {
if (indexToDelete >= 0 && indexToDelete < notes.size()) {
notes.erase(notes.begin() + indexToDelete);
std::cout << "笔记删除成功!" << std::endl;
// 此时同样需要调用一个函数来将 notes 重新写入文件
// 例如: saveNotesToFile(notes, "notes.txt");
} else {
std::cout << "无效的笔记编号。" << std::endl;
}
}你看,不管是编辑还是删除,核心都是对内存中的
std::vector<Note>
以上就是怎样用C++制作简易笔记应用 文件存储与字符串处理的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号