c++中读入一个txt文档,用链表实现可以读取并更改任意行

,怎么做啊,怎么一行一行的读入啊,或者更改某行啊,有没有什么生动例子,感觉无从下手呢

/*
http://zhidao.baidu.com/question/329457839.html?seed=0

c++中读入一个txt文档,用链表实现可以读取并更改任意行

程序编译环境:c-free 5.0
*/

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;

/*链表的节点。保存文件中一行的内容*/
struct Line
{
int id;
string content;
struct Line* next;
};

class TextEditor
{
public:

// 文件中一行最多的字符数
const static int MAX_SIZE_PER_LINE = 10000;

/*
构造函数

参数:
filePath 文件路径
*/
TextEditor(string filePath);

/*
打印文件内容
*/
void print();

/*
保存文件
*/
void save();

/*
为文件增加一行

参数:
lineIndex 要增加的行所在行号
content 要增加的行的内容

返回:
添加成功返回 true ,否则返回 false
*/
bool addLine(int lineIndex,string content);

/*
为文件删除一行

参数:
lineIndex 要删除的行所在行号

返回:
删除成功返回 true ,否则返回 false
*/
bool deleteLine(int lineIndex);

/*
为文件修改一行

参数:
lineIndex 要修改的行所在行号
content 要修改的行的修改后的内容

返回:
修改成功返回 true ,否则返回 false
*/
bool modifyLine(int lineIndex,string content);

/*
搜索文件中的行

参数:
keyword 要搜索的行含有的关键字

返回:
搜索结果,是一个链表
*/
struct Line * searchLines(string keyword);

/*
查询文件的总行数

返回:
文件的总行数
*/
int getNumOfLines();

private:
// 文件路径
string filePath;
// 链表形式,按行存储文件在内存中
struct Line * artical;
};

TextEditor::TextEditor(string filePath)
{
this->filePath = filePath;
this->artical = NULL;

ifstream fin(this->filePath.c_str());
if(fin)
{
int lineId = 0;
struct Line * pr, *p;
char *buffe = new char[MAX_SIZE_PER_LINE+1];

while(!fin.eof())
{
p = new struct Line();
// 生成行号
p -> id = ++ lineId;
// 文件读入行内容
fin.getline(buffe,MAX_SIZE_PER_LINE);
p -> content = string(buffe);
// 初始化指针
p -> next = NULL;

// 添加新的一行(新的节点)
if(this->artical == NULL)
{
this->artical = p;
pr = p;
}
else
{
pr -> next = p;
pr = p;
}
}
}

fin.close();
}

void TextEditor::print()
{
struct Line *p = this->artical;
// 输出的行号的宽度
int lineIdWidth = (int)log10(this->getNumOfLines()) + 1;

while(p)
{
// 设置行号输出的宽度
cout.width(lineIdWidth);
cout << (p -> id) << " : " << (p -> content) << endl;
p = p -> next;
}
}

void TextEditor::save()
{
ofstream fout(this->filePath.c_str());
if(fout)
{
struct Line *p = this->artical;
while(p)
{
fout << p->content;
// 不是最后一行,输出一个换行符号
if(p -> next != NULL)
{
fout << endl;
}
p = p->next;
}
}

fout.close();
}

bool TextEditor::addLine(int lineIndex,string content)
{
int numOfLines = this->getNumOfLines();
// 要插入新行的位置不合法
if(lineIndex < 1 || lineIndex > (numOfLines+1))
{
return false;
}
else
{
// 新行
struct Line * newLine = new struct Line();
// 生成行号
newLine -> id = lineIndex;
// 行内容
newLine -> content = content;
// 初始化指针
newLine -> next = NULL;

if(this->artical == NULL)
{
this->artical = newLine;
}
else
{
struct Line * pr = this->artical , *p = pr ;

// 找到要插入行的位置
while(p && (lineIndex > p->id ))
{
pr = p;
p = p -> next;
}

// 插入第一行特别处理
if(lineIndex == 1)
{
p = newLine -> next = this -> artical;
this -> artical = newLine;
}
else
{
p = newLine -> next = pr -> next;
pr -> next = newLine;
}

// 更改插入行之后所有行的行号
while(p)
{
p -> id ++;
p = p -> next;
}
}

return true;
}
}

bool TextEditor::deleteLine(int lineIndex)
{
int numOfLines = this->getNumOfLines();
// 要删除行的位置不合法
if(lineIndex < 1 || lineIndex > numOfLines)
{
return false;
}
else
{
struct Line * pr = this->artical , *p = pr ;

// 找到要删除行的位置
while(p && (lineIndex != p->id ))
{
pr = p;
p = p -> next;
}

// 删除第一行要特别处理
if(lineIndex == 1)
{
this -> artical = p -> next;
free(p);
p = this -> artical;
}
else
{
pr -> next = p -> next ;
free(p);
p = pr -> next;
}

// 更改删除行之后所有行的行号
while(p)
{
p -> id --;
p = p -> next;
}

return true;
}
}

bool TextEditor::modifyLine(int lineIndex,string content)
{
int numOfLines = this->getNumOfLines();
// 要修改行的位置不合法
if(lineIndex < 1 || lineIndex > numOfLines)
{
return false;
}
else
{
struct Line * pr = this->artical , *p = pr ;

// 找到要修改行的位置
while(p && (lineIndex != p->id ))
{
pr = p;
p = p -> next;
}

// 修改行的内容
p -> content = content ;

return true;
}
}

struct Line * TextEditor::searchLines(string keyword)
{
struct Line * p = this->artical ,*pr ,*searchResult = NULL ;
while(p)
{
// 当前行含有要搜索的关键字
if(p -> content.find(keyword) != string::npos)
{
// 生成一个匹配项
struct Line * matchLine = new struct Line();
matchLine -> id = p -> id;
matchLine -> content = p -> content;
matchLine -> next = NULL;

// 添加到第一条搜素结果要特殊处理
if(searchResult == NULL)
{
searchResult = matchLine;
pr = matchLine;
}
else
{
pr -> next = matchLine;
pr = matchLine;
}
}

p = p -> next;
}

return searchResult;
}

int TextEditor::getNumOfLines()
{
int numOfLines = 0;
struct Line *p = this->artical;
while(p)
{
numOfLines++;
p = p->next;
}
return numOfLines;
}

int main(int argc, char *argv[])
{

// 菜单
while(1)
{
/*第一级菜单*/
cout<<"********【1】打开文件********"<<endl;
cout<<"********【2】退  出********"<<endl;
int choice_1;
cin >> choice_1;

switch(choice_1)
{
case 1:
{
system("cls");
cout << "输入要打开的文件路径:";
string filePath;
cin >> filePath;
TextEditor textEditor(filePath);

system("cls");
// 输出文件内容
textEditor.print();

while(1)
{
/*第二级菜单*/
cout << endl;
cout<<"********【1】添加行********"<<endl;
cout<<"********【2】删除行********"<<endl;
cout<<"********【3】修改行********"<<endl;
cout<<"********【4】搜索行********"<<endl;
cout<<"********【5】打 印********"<<endl;
cout<<"********【6】返 回********"<<endl;
int choice_2;
cin >> choice_2;

switch(choice_2)
{
// 添加行
case 1:
{
cout << "输入要添加行的位置[1~" << (textEditor.getNumOfLines()+1) << "] :";
int lineIndex;
cin >> lineIndex;

cout << "输入要添加的行的内容:" ;
string content;
cin >> content;

if(textEditor.addLine(lineIndex,content))
{
cout << "添加行成功!"<<endl;
}
else
{
cout << "添加行失败!"<<endl;
}
break;
}

// 删除行
case 2:
{
cout << "输入要删除行的位置[1~" << textEditor.getNumOfLines() << "] :";
int lineIndex;
cin >> lineIndex;

if(textEditor.deleteLine(lineIndex))
{
cout << "删除行成功!"<<endl;
}
else
{
cout << "删除行失败!"<<endl;
}

break;
}

// 修改行
case 3:
{
cout << "输入要修改行的位置[1~" << textEditor.getNumOfLines() << "] :";
int lineIndex;
cin >> lineIndex;

cout << "输入要修改的行的内容:" ;
string content;
cin >> content;

if(textEditor.modifyLine(lineIndex,content))
{
cout << "修改行成功!"<<endl;
}
else
{
cout << "修改行失败!"<<endl;
}

break;
}

// 搜索行
case 4:
{
cout << "输入要搜索的关键字:";
string keyword;
cin >> keyword;

// 搜索行
struct Line * searchResult = textEditor.searchLines(keyword) ,*p = searchResult;
// 输出搜索结果
cout << "搜索结果:"<< endl;
while(p)
{
cout << p->id << " : " << endl << p->content << endl;
p = p -> next;
}
break;
}

// 打印文件
case 5:
{
textEditor.print();
break;
}

// 返回
case 6:
{
textEditor.save();
break;
}

// 选择错误
default:
{
cout<<"命令错误,请重新选择!"<<endl;
break;
}
} // end switch

// 从二级菜单返回一级菜单
if(choice_2 == 6)
{
break;
}

}// end while(1)

break;
}

case 2:
{
exit(0);
break;
}
default:
{
cout<<"命令错误,请重新选择!"<<endl;
}

}
}

return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-15
一楼真下本。。。竟然写出来了。
我大致浏览了下,思路是比较可行的,如果想修改任意行,通过index也就是行号是个好办法。
相似回答