这个文件管理系统示例是一个简单的命令行程序,允许用户进行文件的创建、读取、追加内容和删除操作。这个示例涉及了一些基本的文件操作和用户交互。
创建文件 (createFile()
):
读取文件 (readFile()
):
追加文件内容 (appendToFile()
):
删除文件 (deleteFile()
):
文件输入输出 (
):
std::ifstream
和 std::ofstream
实现文件读取和写入功能。文件流的打开和关闭:
is_open()
和 close()
函数用于检查文件流是否打开以及关闭文件。命令行交互:
std::cin
和 std::cout
实现与用户的交互。文件操作函数:
std::remove()
函数用于删除文件。循环和条件语句:
do-while
循环处理菜单选项,根据用户输入的选择执行相应的功能。通过这个示例,初学者可以了解如何使用 C++ 实现基本的文件操作、用户交互和函数封装,以及如何处理文件的创建、读取、追加和删除等操作。同时也涉及了条件语句、循环等基本的程序控制结构。
#include
#include
#include
void createFile() {
std::string filename, content;
std::cout << "Enter filename: ";
std::cin >> filename;
std::ofstream file(filename);
if (file.is_open()) {
std::cout << "Enter file content (type 'end' to finish):\n";
while (true) {
std::cin.ignore(); // Ignore newline character from previous input
std::getline(std::cin, content);
if (content == "end") {
break;
}
file << content << std::endl;
}
file.close();
std::cout << "File created successfully!" << std::endl;
} else {
std::cout << "Error! Unable to create file." << std::endl;
}
}
void readFile() {
std::string filename, line;
std::cout << "Enter filename to read: ";
std::cin >> filename;
std::ifstream file(filename);
if (file.is_open()) {
std::cout << "File content:" << std::endl;
while (std::getline(file, line)) {
std::cout << line << std::endl;
}
file.close();
} else {
std::cout << "Error! Unable to open file." << std::endl;
}
}
void appendToFile() {
std::string filename, content;
std::cout << "Enter filename to append: ";
std::cin >> filename;
std::ofstream file(filename, std::ios::app);
if (file.is_open()) {
std::cout << "Enter content to append (type 'end' to finish):\n";
while (true) {
std::cin.ignore(); // Ignore newline character from previous input
std::getline(std::cin, content);
if (content == "end") {
break;
}
file << content << std::endl;
}
file.close();
std::cout << "Content appended to file successfully!" << std::endl;
} else {
std::cout << "Error! Unable to open file." << std::endl;
}
}
void deleteFile() {
std::string filename;
std::cout << "Enter filename to delete: ";
std::cin >> filename;
if (std::remove(filename.c_str()) != 0) {
std::cout << "Error! Unable to delete file." << std::endl;
} else {
std::cout << "File deleted successfully!" << std::endl;
}
}
int main() {
char choice;
do {
std::cout << "\nFile Management System\n";
std::cout << "1. Create File\n";
std::cout << "2. Read File\n";
std::cout << "3. Append to File\n";
std::cout << "4. Delete File\n";
std::cout << "5. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case '1':
createFile();
break;
case '2':
readFile();
break;
case '3':
appendToFile();
break;
case '4':
deleteFile();
break;
case '5':
std::cout << "Exiting...\n";
break;
default:
std::cout << "Invalid choice!\n";
break;
}
} while (choice != '5');
return 0;
}
当我们拆分讲解这个文件管理系统示例时,可以按照功能模块来逐步解释每个部分的作用和实现。
createFile()
):void createFile() {
std::string filename, content;
std::cout << "Enter filename: ";
std::cin >> filename;
std::ofstream file(filename);
// 检查文件是否成功打开
if (file.is_open()) {
std::cout << "Enter file content (type 'end' to finish):\n";
while (true) {
std::cin.ignore(); // 忽略上一个输入中的换行符
std::getline(std::cin, content);
if (content == "end") {
break;
}
file << content << std::endl;
}
file.close();
std::cout << "File created successfully!" << std::endl;
} else {
std::cout << "Error! Unable to create file." << std::endl;
}
}
std::ofstream
打开文件,使用 file.is_open()
检查文件是否成功打开,然后读取用户输入的内容,并将内容写入文件。readFile()
):void readFile() {
std::string filename, line;
std::cout << "Enter filename to read: ";
std::cin >> filename;
std::ifstream file(filename);
// 检查文件是否成功打开
if (file.is_open()) {
std::cout << "File content:" << std::endl;
while (std::getline(file, line)) {
std::cout << line << std::endl;
}
file.close();
} else {
std::cout << "Error! Unable to open file." << std::endl;
}
}
appendToFile()
):void appendToFile() {
std::string filename, content;
std::cout << "Enter filename to append: ";
std::cin >> filename;
std::ofstream file(filename, std::ios::app);
// 检查文件是否成功打开
if (file.is_open()) {
std::cout << "Enter content to append (type 'end' to finish):\n";
while (true) {
std::cin.ignore(); // 忽略上一个输入中的换行符
std::getline(std::cin, content);
if (content == "end") {
break;
}
file << content << std::endl;
}
file.close();
std::cout << "Content appended to file successfully!" << std::endl;
} else {
std::cout << "Error! Unable to open file." << std::endl;
}
}
std::getline()
是 C++ 标准库
头文件中的函数,用于从输入流中获取一行文本并存储到字符串中。
std::getline()
接受两个参数:输入流和字符串。std::cin
,标准输入)中读取一行文本,并将读取的内容存储到字符串 line
中,直到遇到换行符 \n
或文件结束符。std::getline() 的特点:
读取整行文本: 不像 std::cin >> variable
会在遇到空格或换行符时停止读取,std::getline()
会读取整行文本。
可以指定定界符(可选): 可以传递第三个参数作为定界符,以指定特定的字符作为终止符号,例如 std::getline(std::cin, line, '\t')
将在遇到制表符时停止读取。
常用于读取用户输入: 在命令行交互中,特别适用于读取用户输入的完整一行文本,例如文件名、描述等信息。
std::getline()
是处理输入流中文本数据时常用的函数,能够方便地读取整行文本并存储到字符串中,适用于许多交互式的输入场景。
deleteFile()
):void deleteFile() {
std::string filename;
std::cout << "Enter filename to delete: ";
std::cin >> filename;
// 删除文件
if (std::remove(filename.c_str()) != 0) {
std::cout << "Error! Unable to delete file." << std::endl;
} else {
std::cout << "File deleted successfully!" << std::endl;
}
}
main()
):主函数提供了用户和程序的交互菜单,根据用户的选择调用相应的功能函数。
整个程序通过以上几个模块,实现了文件的创建、读取、追加和删除等功能,并通过命令行菜单和用户输入来控制程序的执行。
本文就到这里了,感谢您的阅读,明天还有更多的实例学习文章等着你 。别忘了点赞、收藏~ Thanks♪(・ω・)ノ 。