2012-05-13 4 views
0
#include<fstream> 
#include<iostream> 
#include<string> 
#include<algorithm> 
using namespace std; 

int main(){ 
ifstream infile; 
ofstream outfile; 
infile.open("oldfile.txt"); 
outfile.open("newfile.txt"); 
while(infile){ 
    string str,nstr; 
    infile>>str; 
    char charr[10]; 
    charr[0]='<';charr[1]='\0'; 
    nstr=str.substr(0,str.find_first_of(charr)); 

    outfile<<nstr<<' '; 
} 
} 

このプログラムでは、substr(0、string.find_first-of(部分文字列の始点であるcharcter配列))を使用して各単語の不要なサブストリング別のファイルに書き込むときに行番号を保持しません。あなたはそれを修正できますか?単語単位でファイルワードをシーケンシャルに書き込みます。コードはこれではなく、単一の空白文字でsperatingの行ずつ記述しますファイル内の単語を切り取り、C++で行番号を保存して書き込む

outfile<<nstr<<' '; 

outfile<<nstr<<endl; 

+0

VTC:Plzはコードを送信します。 –

答えて

0

文字列入力は行境界を気にせず、\ n、\ t、\ vなどを処理します。

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

int main() 
{ 
    string line,word; 
    char foo[] = "<"; 
    while (getline(cin,line)) { 
     string newline; 
     for (istringstream words(line) 
      ; words >> word ;) { 
       newline+=word.substr(0,word.find_first_of(foo))+' '; 
     } 
     cout << newline << '\n'; 
    } 
} 
+0

ありがとう、それは私のために働いています。 –

-1

変更し、行ごとに保持されませんでした。

+0

あなたはそれを試しましたか? – jthill

+0

私は問題を見る。ファイルは文字列に一度に読み込まれます。 – allesmi

関連する問題