C++ 对文本文件按行进行编辑:先从输入文件中读取数据,然后根据行编辑命令处理,将结果写到输出文件中。

 行编辑命令包括:
序号 行编辑命令格式 功能
1 *L m,n 显示从第m至n行的文本
2 *I m
……
^Z 插入文本(……)在第m行后
3 *D m,n 删除从第m至n行的文本
4 *R m,n
……
^Z 用文本(……)替换第m至n行的文本
5 *X 保存并退出编辑程序
6 *Q 放弃并退出程序

求大神帮忙啊,感觉C++好难

这个处理起来还挺麻烦,我把命令格式稍微的改了一下,把m和n中间的逗号改成了空格,这样处理起来简单很多,不然还得增加代码。

我已经尽量把每段代码都写的比较短了,希望能看懂= =。。。个人代码风格比较烂,有看不懂的地方可以问

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <conio.h> // for getch()

using namespace std;

class File {
private:
        string filename;
        vector<string> lines;
public:
        // 构造函数,读取文件内容把每一行内容存到lines变量里
        File(const char *szFileName): filename(szFileName) {
                ifstream infile(szFileName);
                if (!infile) {
                        cout<<szFileName<<" file not exists"<<endl;
                }
                string line;
                while(getline(infile, line) > 0) {
                        lines.push_back(line);
                }
        }

        // 打印m到n行, 从0计数
        void print(int m, int n) {
                for(int i=m; i<=n && i<lines.size(); i++) {
                        cout<<lines[i]<<endl;
                }
        }

        // 在第m行插入多行文本,默认认为text中包含多行文本
        void insert(int m, const char * text) {
                istringstream textstream(text);
                string line;
                while(getline(textstream, line) > 0) {
                        lines.insert(lines.begin()+m, line);
                        m++;
                }
        }

        // 删除m到n行,从0计数
        void del(int m, int n) {
                lines.erase(lines.begin()+m, lines.begin()+n+1);
        }

        // 将m到n行替换为text中的文本,默认认为text中有多行文本
        void replace(int m, int n, const char * text) {
                istringstream textstream(text);
                string line;
                while( getline(textstream, line) > 0 && (m <= n) && (m < lines.size()) ) {
                        lines[m] = line;
                        m++;
                }
        }

        // 将修改后的内容存到文件中
        void save() {
                ofstream outfile(filename.c_str());
                for(int i=0; i<lines.size(); i++) {
                        outfile<<lines[i]<<endl;
                }
        }
};

// 要处理^Z,就得用getch()函数
// 但getch()函数只读取输入,而不把我们的输入显示到屏幕上,所以需要自己控制显示
// getch()读取换行时只能读到'\r',但windows上的换行是\r\n,所以需要特殊处理
char readChar() {
        char ch=getch();
        if (ch != 26) {
                putchar(ch);
                if (ch == '\r') {
                        putchar('\n');
                        ch = '\n';
                }
        }
}

#define MAX_TEXT_SIZE 2048
void readText(char text[]) {
        int i=0;
        char ch = 0;
        while( (ch=readChar()) != 26 && (i < MAX_TEXT_SIZE) ) { // ^Z 的ASCII码是26
                text[i] = ch;
                i++;
        }
        text[i] = '\0'; // C++字符串以'\0'结束
}

int main() {
        File file("file.txt");

        char cmd = 0;
        int m, n;
        char text[MAX_TEXT_SIZE+1]; // 多留一个字符来存结束符'\0'
        bool shouldExit = false;
        while(!shouldExit) {
                cout<<'*';
                cmd = getchar();
                switch(cmd) {
                case 'L':
                        cin>>m>>n;
                        file.print(m, n);
                        break;
                case 'I':
                        cin>>m;
                        readText(text);
                        file.insert(m, text);
                        break;
                case 'D':
                        cin>>m>>n;
                        file.del(m, n);
                        break;
                case 'R':
                        cin>>m>>n;
                        readText(text);
                        file.replace(m, n, text);
                        break;
                case 'X':
                        file.save();
                        shouldExit = true;
                        break;
                case 'Q':
                        shouldExit = true;
                        break;
                default:
                        break;
                } // switch
                fflush(stdin);
        }// while

        return 0;
}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答