Bryan Wong
Bryan Wong

Reputation: 633

C++ Filter Content from Text File

I have been wanting to extract a line of text once [1],[2] ... [n] is found. But it seems like I couldn't get my thinking out to store a line into a char starting with [1].

void ExtractWebContent::filterContent(){
    char str [10];
    ifstream reading;
    reading.open("file_Currency.txt");

    while (!reading.eof()){
        reading.get(str,10,'[1]');
        cout << str << endl;
    }
    cout << str;
    reading.close(); 
}

This is the file that I want to extract from..

 CAPTION: Currencies

          Name      Price    Change % Chg
       [80]USD/SGD
                  1.2606     -0.00  -0.13%

                                           USD/SGD [81]USDSGD=X
       [82]EUR/SGD
                  1.5242     0.00   +0.11%

                                           EUR/SGD [83]EURSGD=X

I am using linux, C++ programming. This is meant to filter figures obtained from HTML text file.

Any help would be very much appreciated. Thank you!

Upvotes: 0

Views: 2211

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409442

The big error you have is that you treat a single character as a string. The third argument is supposed to be a single character delimiter, i.e. a character that separates records in the file. If you add the compiler option -Wall when compiling you will get a warning about having more than one character in the single-character literal.

One way of doing what you want, is to use regular expressions.

Upvotes: 1

Related Questions