-1

我不知道从这里去哪里。我知道有些事情需要去做ifstr.get(c)。它复制了我在名为的文本文件中的确切单词,project.txt但我只需要删除任何具有字符<>?任何帮助都会很棒。谢谢:)

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    string line;
    char c;

    ifstream ifstr("project.txt");
    ofstream ofstr("past.txt");
    if(ifstr.fail()){
        cout<<"error!"<<endl;
    } // if

    ifstr.get(c);
    while(!ifstr.eof()) {
        cout<<c;
        ifstr.get(c);

        ofstr<<line<<endl;
    } // while

    cout<<endl<<"copy complete"<<endl;

    ifstr.close();
    ofstr.close();

    system ("pause");
    return 0;
} // main
4

3 回答 3

0

我不确定,这就是你想要的。请看代码!

//we create a boolean value, to know if we started skipping
bool skipStarted = false;

while(ifstr.get(c))
{
    //if its a '<' and we havent started skipping jet,
    //then we start skipping, and continue to the next char.
    if(c=='<' && !skipStarted)
    {
        skipStarted = true;
        continue;
    }

    //if its a '>' and we started skipping,
    //then we finish skipping, and continue to the next char.
    if(c=='>' && skipStarted)
    {
        skipStared = false;
        ifstr.get(c);
        if(c==' ')
            continue; 
    }

    //if we are skipping, then we go to the next char.
    if(skipStarted)
        continue;

    //otherwise we simply output the character.
    ofstr<<c;

}
于 2012-08-09T18:09:10.997 回答
0

只是在黑暗中的另一个镜头:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {

    ifstream ifstr("project.txt");
    ofstream ofstr("past.txt");
    if(ifstr.fail()){
        cout << "error!" << endl;
    } // if

    bool skipOutput = false;
    do
    {
        string word;
        ifstr >> word;
        if(!word.empty() && word[0] == '<')
        {
            skipOutput = true;
        }
        if(!skipOutput)
        {
            ofstr << word << " ";
            // replicate the output to stdout
            cout << word;
        }
        if(word[word.length()-1] != '>')
        {
            skipOutput = false;
        }
    } while(!ifstr.eof());
    cout << endl << "copy complete" << endl;

    ifstr.close();
    ofstr.close();

    //system ("pause"); Doesn't compile with my system
    return 0;
} // main

如果您真的只是想过滤掉包含在 '<' 和 '>' 字符中的单词,这应该足够了。如果您的<>标签有更复杂的解析规则,您应该详细说明您的问题。

于 2012-08-09T18:23:32.437 回答
0

标题中问题的伪代码(iostream-esque 条件)(也删除了尖括号):

char c;
while (read_char_succeeded(&c))
    if (c == '<')
        while (read_char_succeeded(&c) && c != '>')
            ;
    else
        write_char(c);
于 2012-08-09T18:35:37.517 回答