我可以为您提供一个基本的C++记事本程序,它可以实现以下功能:

  1. 新建文档
  2. 打开文档
  3. 保存文档
  4. 另存为文档
  5. 编辑文档
  6. 退出程序

以下是程序代码:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
    string filename;
    char choice;
    ofstream out;
    ifstream in;
    string text;

    cout<<"Welcome to the Notepad!"<<endl;

    while(1)
    {
        cout<<"Please choose an option:"<<endl;
        cout<<"1. New Document"<<endl;
        cout<<"2. Open Document"<<endl;
        cout<<"3. Save Document"<<endl;
        cout<<"4. Save As Document"<<endl;
        cout<<"5. Edit Document"<<endl;
        cout<<"6. Exit"<<endl;
        cin>>choice;

        switch(choice)
        {
            case '1':
                cout<<"Please enter the filename: ";
                cin>>filename;
                out.open(filename.c_str());
                out.close();
                break;

            case '2':
                cout<<"Please enter the filename: ";
                cin>>filename;
                in.open(filename.c_str());
                if(in.fail())
                {
                    cout<<"Error: File not found."<<endl;
                    in.close();
                    break;
                }
                while(getline(in, text))
                {
                    cout<<text<<endl;
                }
                in.close();
                break;

            case '3':
                cout<<"Please enter the filename: ";
                cin>>filename;
                out.open(filename.c_str());
                cout<<"Please enter the text: ";
                cin.ignore();
                getline(cin, text);
                out<<text<<endl;
                out.close();
                break;

            case '4':
                cout<<"Please enter the filename: ";
                cin>>filename;
                out.open(filename.c_str());
                cout<<"Please enter the text: ";
                cin.ignore();
                getline(cin, text);
                out<<text<<endl;
                out.close();
                break;

            case '5':
                cout<<"Please enter the filename: ";
                cin>>filename;
                out.open(filename.c_str(), ios::app);
                cout<<"Please enter the text: ";
                cin.ignore();
                getline(cin, text);
                out<<text<<endl;
                out.close();
                break;

            case '6':
                cout<<"Thank you for using the Notepad!"<<endl;
                return 0;

            default:
                cout<<"Invalid option. Please try again."<<endl;
                break;
        }
    }

    return 0;
}

该程序使用了ofstream和ifstream两个类来实现文本的读写操作。其中,ofstream用于写入文本,ifstream用于读取文本。在程序中,我们使用了一个while循环来不断接收用户的输入,并根据用户的选择执行相应的操作。当用户选择退出程序时,程序结束。

请注意,在程序中,当用户选择打开文档时,程序会尝试打开用户输入的文件名。如果文件不存在,程序会输出错误信息并返回主菜单。当用户选择保存文档时,程序会将当前文本覆盖到原文件中。当用户选择另存为文档时,程序会将当前文本保存到一个新文件中。当用户选择编辑文档时,程序会将当前文本追加到原文件末尾。

此外,为了防止用户输入空白字符,程序在输入文本时使用了cin.ignore()函数来忽略之前输入的回车符。

用C++写一个可以保存的记事本

原文地址: https://www.cveoy.top/t/topic/m32 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录