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(部分文字列の開始点である文字配列))各単語の不要な部分文字列を使用しますが、別のファイルに書き込むときに行番号を保持しません。あなたはそれを修正できますか?ファイルを単語ごとに順番に書き込みます。コードは行ごとに保存されませんでした。

4

2 に答える 2

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';
    }
}
于 2012-05-13T23:18:43.880 に答える
-1

変化する

 outfile<<nstr<<' ';

 outfile<<nstr<<endl;

これにより、単一の空白文字で区切られる代わりに、行ごとに書き込まれます。

于 2012-05-13T20:29:06.290 に答える