C++ 实现的文本编辑器 编译都通过了,就是运行不了啊!求牛人指点一二!

_江南 发布于 2011/11/26 12:26
阅读 898
收藏 0

#ifndef TEXTEDITOR_H
#define TEXTEDITOR_H

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

class TextEditor
{
public:
 TextEditor(string,string);
 ~TextEditor(void);
 void run();
 void insert(string ,string );
 void erase(string str);
 void replace(string ,string );
 void next();
 void quit();
 void showMunu();

private:
 ifstream inStream;
 ofstream outStream;
 string currentLine;
};
#endif

//---吃掉cin中空白字符的工具函数
eatBlanks()
{
 char blank;
 while(cin.peek() == ' ')
  cin.get(blank);
}

#include"TextEditor.h"

//---构造函数定义
TextEditor::TextEditor(string inFileName,string outFileName)
{
 //inStream.open(inFileName.c_str());
 //outStream.open(outFileName.c_str());
 inStream.open(inFileName.data());
 outStream.open(outFileName.data());
 if ( !inStream.is_open() || !outStream.is_open())
 {
  //cout<<"Fail to Open file"<<endl;
  cerr<<"Fail to Open file"<<endl; 
  exit(-1);
 }
 else
 {
  cout<<"Open File Succseeful..\n";
 }

}

void TextEditor::run()
{
 showMunu();
 cout<<"Enter an editing command following each prompt >\n\n";
 getline(inStream,currentLine);
 cout<<"TEXT: "<<currentLine<<endl;
 char command;
 string str1,str2;
 for(;;)
 {
  if(inStream.eof()) break;
  cout << '>';
  cin >> command;
  cin.ignore(1,'\n');
  switch(toupper(command))
  {
  case 'I':
   eatBlanks();
   getline(cin,str1);
   cout << "Insert before what string : ";
   getline(cin,str2);
   insert(str1,str2);
   break;
  case 'D':
   eatBlanks();
   getline(cin,str1);
   erase(str1);
   break;
  case 'R':
   eatBlanks();
   getline(cin,str1);
   cout << "With What? ";
   getline(cin,str2);
   replace(str1,str2);
   break;
  case 'N':
   next();
   break;
  case 'Q':
   quit();
   break;
  default :
   cout << "\n*** Illegal command ***\n";
   showMunu();
   cout << "TEXT: "<<currentLine<<endl;
  }//End of switch
  if(!inStream.eof())
   cout << "TEXT: "<<currentLine<<endl;
 }//End of for
 cout<< "\n*** Editing complete ***\n";
}

//---显示command
void TextEditor::showMunu()
{
 //cout<<"showMunu"<<endl;
 cout<<"Editing commands are:\n"
  "I str: Insert string str before another string\n"
  "D str: Delete string str\n"
  "R str: Replace string str with another string\n"
  "N : Get next line of text\n"
  "Q : Quit editing\n";
}

//---插入 str1 在str2之前
void TextEditor::insert(string str1,string str2)
{
 size_t position = currentLine.find(str2);
 if( position != string::npos )
  currentLine.insert(position,str1);
 else
  cout<<"\""<< str2<<"\"" <<" not found\n";
}
//---清除str
void TextEditor::erase(string str)
{
 size_t position = currentLine.find(str);
 if (position != string::npos)
  currentLine.erase(position,str.length());
 else
  cout<<"\""<< str<<"\"" <<"not found\n";
}
//---用str2替换str1
void TextEditor::replace(string str1,string str2)
{
 size_t position = currentLine.find(str1);
 if (position != string::npos)
  currentLine.replace(position,str1.length(),str2);
 else
  cout<<"\""<< str1 <<"\"" <<" not found\n";
}
//---读取下一行
void TextEditor::next()
{
 outStream << currentLine<<endl;
 getline(inStream,currentLine);
 cout << "\nNext Line:\n";
}
//---退出程序
void TextEditor::quit()
{
 outStream << currentLine<<endl;
 for(;;)
 {
  getline(inStream,currentLine);
  if(inStream.eof()) break;
  outStream << currentLine <<endl;
 }//End of for
}

#include"TextEditor.h"

int main()
{
 string inFileName;
 string outFileName;
 cout<<"--------- Welcome TextEdit ----------"<<endl;
 cout<<"Enter the name of the input file:";
 getline(cin,inFileName);
 outFileName = inFileName + ".txt";
 cout<<"The Output File is "<<outFileName<<endl;

 TextEditor textEditor(inFileName,outFileName);
 textEditor.run();

 return 0;
}

加载中
0
宿命
宿命
敢问 您是怎么调试的...debug一下看看吧...
_江南
_江南
谢谢你啊!我只是编译了!在EDV C++上编译的!就是运行不了!
0
_江南
_江南
谢谢你啊!我只是编译了!在EDV C++上编译的!就是运行不了!
0
ChenQi
ChenQi

我没仔细看,直接复制编译了下。编译不过。

改正如下后编译通过并且运行。(不过运行时的功能有点小问题,因为你的函数写的很有问题。你自己看吧)

1. 没有虚构函数定义。补上。

2.没有exit定义,#include <cstdlib>

3.eatblank没有返回类型,补上void

吐槽下,你的编译器竟然能过,你重装开发环境吧。直接用g++编译都更好一点。

 

ChenQi
ChenQi
@_江南 : 有什么错误编译器会提醒啊。你把错误都改了就能运行。你这个程序没有什么复杂的机制,一般编译过了就能跑 。
_江南
_江南
我试了,不行啊!
_江南
_江南
太谢谢了!我试试吧!
0
宿命
宿命
...ls居然还编译了还改正了,佩服
返回顶部
顶部