2016-06-20 13 views
-1

C++を使用してHTMLファイルの特定の文字列(行全体ではない)を置き換える方法を探しています。例えば、私が含まれているhtmlファイルがある場合:C++を使用してテキストファイルに単語を置き換えます。

</span><br><span class=text>morning<br></span></td> 

をそして私はとして編集にそれをしたい:

</span><br><span class=text>night<br></span></td> 

私は「夜」で「朝」を交換する必要があり、これが私ですコード:

string strReplace = "morning"; 
    string strNew = "night"; 
    ifstream filein("old_file.html"); 
    ofstream fileout("new_file.html"); 
    string strTemp; 
    while(filein >> strTemp) 
    { 
    if(strTemp == strReplace){ 
     strTemp = strNew; 
    } 
    strTemp += "\n"; 
    fileout << strTemp; 
    } 

このコードは、私のファイルには何の影響もかかりませんでした、と私は理由はそれではなく、部分文字列の、唯一の行全体を変更することができるということであると思います。誰か私に正しい実装をするための提案を与えることができますか?前もって感謝します。

+0

filein >>はトークンを読み込みます。トークンは空白で区切られます。 'class = text> morning
'を試してください。 –

+0

http://www.cplusplus.com/reference/string/string/getline/でファイルを1行ずつ読み込み、http://www.cplusplus.com/reference/string/string/find/で部分文字列を探してください。見つけたら、http://www.cplusplus.com/reference/string/string/replace/に置き換えてください。 – Marandil

答えて

0

を、あなたは「夜」を使用してファイルの「朝」のすべてのインスタンスを交換する必要がなく、1つのインスタンスのみ与えられた上でライン。ファイル全体を文字列に読み込むことから始めます。

std::string getfile(std::ifstream& is) { 
    std::string contents; 
    // Here is one way to read the whole file 
    for (char ch; is.get(ch); contents.push_back(ch)) {} 
} 

次に、find_and_replace機能を行う:

void find_and_replace(std::string& file_contents, 
    const std::string& morn, const std::string& night) { 
    // This searches the file for the first occurence of the morn string. 
    auto pos = file_contents.find(morn); 
    while (pos != std::string::npos) { 
    file_contents.replace(pos, morn.length(), night); 
    // Continue searching from here. 
    pos = file_contents.find(morn, pos); 
    } 
} 

そして主に、

std::string contents = getfile(filein); 
find_and_replace(contents, "morning", "night"); 
fileout << contents; 

EDIT:find_and_replace()は、CONSTであるために、そのfile_contents文字列パラメータを宣言してはなりません。ちょうど気づいて固定しました。

+0

詳細な回答ありがとうございました! – Saintycer

1

行全体を取り出し、部分文字列を検索して置換することができます。この方法で、あなたもループ全体をスキップ:私は元の質問を読んでどのようにから

std::string line; 

//Get line in 'filein' 
std::getline(filein, line); 

std::string replace = "morning"; 

//Find 'replace' 
std::size_t pos = line.find(replace); 

//If it found 'replace', replace it with "night" 
if (pos != std::string::npos) 
    line.replace(pos, replace.length(), "night"); 
+0

ありがとう!それは問題を完全に解決しました。 – Saintycer

0

複数の方法で対応できます。かなり一般的なのは、新しい文字列を含むファイルを作成し、古いファイルを削除して、新しいファイルを古いファイル名に名前変更することです。

これがあなたに合っていれば、このコンソールアプリケーションを邪魔することができます。


パラメータ1は

パラメータ2検索する文字列である

パラメータ3を使用するための置換テキストは、ファイルパス、基本的


です私が作ってるんです新しいファイルへのいくつかのSTDストリームと古いファイルから、問題の文字列があればそれを探し、そうであればその文字列を置き換えて、置換が発生しても結果をパイプする新しいファイルの新しい行として編集します。

次にストリームを閉じてファイルサイズを確認し、空でないファイルを作成できたら、古いファイルを削除して新しいファイルの名前を古いファイル名に変更します。

エラーコードが2の場合、入力ストリームが機能しないことを意味します。それが-1なら、つまり、新しいファイルを見つけたり、置き換えたりすることができなかったことを意味します。 1の場合は、正しいパラメータを指定しなかったことを意味します。ゼロ出口だけが成功を示します。

これを使用する場合は、例外処理と、おそらくより堅牢なファイルバックアップソリューションを追加する必要があります。

#include <iostream> 
#include <fstream> 
#include <string> 
#include <sys\stat.h> 

using namespace std; 

int main(int argc, char * argv[]) 
{ 
    if (argc != 4) { return 1; } 
    int exit_code = -1; 

    string textToFind = string(argv[1]); 
    string replacementText = string(argv[2]); 
    string fileToParse = string(argv[3]); 
    string fileToWrite = fileToParse+".rep"; 

    ifstream inputFileStream(fileToParse, ifstream::in); 
    if (!inputFileStream.good()) { return 2; } 

    ofstream outputFileStream(fileToWrite); 

    string fileLine; 
    while (getline(inputFileStream, fileLine)) 
    { 
     size_t substringPos = fileLine.find(textToFind); 
     if (substringPos != string::npos) 
     { 
      fileLine.replace(substringPos, textToFind.size(), replacementText); 
     }  
     outputFileStream << fileLine << '\n'; 
    } 
    outputFileStream.close(); 
    inputFileStream.close(); 

    struct stat st; 
    stat(fileToWrite.c_str(), &st); 
    int new_file_size = st.st_size; 

    if (new_file_size > 0) 
    { 
     if (remove(fileToParse.c_str())==0) 
     { 
      if (rename(fileToWrite.c_str(), fileToParse.c_str())==0) 
      { 
       exit_code = 0; 
      } 
     } 
    } 

    return exit_code; 
} 
関連する問題