2016-09-01 5 views
1

の最後の9文字を取得します。 0); " 、私はそれらの50k行のように持っていて、各行は0,0,0のすべての行を見つけたい異なる数字を持っています。最後に、私は彼らに私はline[N].substr(0,-9);を試してみましたが、それが機能しなかったのです を見つける必要がありまず、配列からそれを削除し、私はまた、line[N].find("0, 0, 0);");を試してみましたが、これは単に「4294967295」こんにちは、文字列の最後のN文字を取得することにより、助けを必要とする文字列

完全なコードliek私の長い番号をゲーム:

const string file = "putpixel.txt"; 
const int maxlines = 60000; 
int main() 
{ 
    string lines[maxlines]; 
    fileRead(file,lines); 
    deleteBlack(lines); 
    cout << lines[0].find("0, 0, 0);"); 
    return 0; 
} 

void fileRead(const string fn,string line[]){ 
    int index = 0; 
    ifstream fin(fn.c_str()); 
    while(!fin.eof()){ 
     getline(fin,line[index],';'); 
     index++; 
    } 
    fin.close(); 
} 
+0

は、実際には文字列の一部のコメントですか?ソリューションはそれを無視するほど知的である必要がありますか? – BoBTFish

+0

** end ** - 9から位置を計算して 'substr()'を使うのはどうですか? –

+0

@ yahoo5000文字列の定義とその初期化方法を表示します。 –

答えて

1

enter image description here

" 0, 0, 0"ためのファイルや検索からこのコードをロードし、テキストとに置き換えそれはsearchresult = -1までこれをやり続けるだろう

searchresult = stringFile.find(" 0, 0, 0")

substr(searchresult + search_string.length() , stringFile.length()) 

を使用。そうであれば、変更された結果をファイルに書き戻します。前putpixel.txt

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


int main(){ 

    int searchresult=1; 
    ifstream inFile; 

    // load file into string 
    inFile.open("putpixel.txt"); 
    stringstream strStream; 
    strStream << inFile.rdbuf(); 
    string stringFile = strStream.str(); 
    inFile.close(); 

    cout<<"\nBefore replace \n"; 
    cout<<""<<stringFile<<" \n"; 


    // initialize search string and replace string 
    string search_string = " 0, 0, 0"; 
    string replace_string = " "; 


    // while searchresult is still greater than zero then keep searching 
    while (searchresult > 0){ 

     searchresult = stringFile.find(search_string); 

     // if searchresult is greater than zero then keep doing this 
     if(searchresult >= 0){ 
      string tmpstring = stringFile.substr(0,searchresult); 
      tmpstring += replace_string; 
      tmpstring += stringFile.substr(searchresult+search_string.length(), stringFile.length()); 
      stringFile = tmpstring; 
     } 

    } 
    // update file after removing 
    ofstream outFile("putpixel.txt"); 
    outFile << stringFile; 
    outFile.close(); 

    cout<<"\nAfter replace \n"; 
    cout<<""<<stringFile<<" \n"; 

cout<<"\nPress ANY key to close.\n\n"; 
cin.ignore(); cin.get(); 
return 0; 
} 

出力:後putpixel.txt

PutPixel(x + N, y + N, 0, 0, 0); 
PutPixel(x + N, y + N, 0, 0, 0); 
PutPixel(x + N, y + N, 0, 0, 0); 
PutPixel(x + N, y + N, 0, 0, 0); 

出力:

PutPixel(x + N, y + N,); 
PutPixel(x + N, y + N,); 
PutPixel(x + N, y + N,); 
PutPixel(x + N, y + N,);  
+0

私はより良い解決策を見つけました、ファイルを読み込みます - >条件が真であるかどうかをチェック - >ベクトル - >書き込みファイルに保存します。まだtnx – yahoo5000

関連する問題