gogoWebsite

C++ method to read numbers in strings

Updated to 8 hours ago

Code example:

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

int main()
{
    string str("55.5818061829 119.6388702393 22.33");
    double t;
    istringstream iss;
    iss.str(str);
    while(iss>>t) {
        cout<<t<<endl;
    }
    return 0;
}

Note that there are no extra symbols in the string except numbers, otherwise it will fail. This is a great way to do it when you are not familiar with regular expressions, especially when reading data from a file.

The following is a reading operation. The file is composed of numbers. The data of the file is first given.

1.467284085736863 9.376511821522355 1.3649498766482537 8.088857549330717 2.3698882782758313 0.5225871352552025 0.6013677353526695 0.9871267670749978 0.7305225913374725 8.77 8.14 2.95 1.00 
6.041632454180849 4.535001565129676 3.3419336714886585 1.7315455144763214 9.220522172161063 9.793391925366667 0.2878894971039667 0.7862232406898116 0.72268766617564 5.56 15.43 8.38 1.00 
0.6158453075118819 7.724459956144301 0.5463212721843924 9.418369709345624 3.497235547312484 3.8047032003875527 0.734118480988751 0.4060951140458058 0.5793220401522091 8.59 7.85 8.11 1.00 
4.7685414389496525 3.5561636748781944 0.7380024353322878 6.738748696346324 9.993213301145895 6.607137410686869 0.2675133058358534 0.7928485178120843 0.6138402863139218 8.13 14.66 4.31 1.00 
0.07521491667869085 3.3382590774260956 7.8307214874066124 2.4160863939202546 5.347946916067755 1.3302075851573736 0.08288941437376163 0.3486248641364451 0.43751931623649365 3.73 7.53 8.12 1.00 
7.188894531065696 9.63371912799707 0.3937955195872622 3.4064493716649604 0.7316971537091621 2.3915790814548954 0.6686869130889267 0.380859571965775 0.5119530902547114 7.86 10.32 4.97 1.00 
3.2765247206521178 7.961855941273122 9.64131927218312 8.043113936024561 5.594476179791733 7.052954362787583 0.02976772757227908 0.9069645286116979 0.2781294472977186 15.02 12.89 10.36 1.00 
8.9009368950318 4.304817904602576 0.5774683034004457 5.1252895259247815 0.5573906664234685 3.8393256771269457 0.01789943667291516 0.34684869384120254 0.21521074875512458 11.66 6.17 1.19 1.00 
3.4688770006932534 6.7391094786095405 2.968489720797179 4.404010211807334 7.347207015679417 7.328173698226389 0.8430421343154131 0.29390949142436906 0.8157082348902395 6.28 11.24 12.02 1.00 
1.7647372666533434 7.95513095522135 0.4604028337109789 3.3475624805081674 9.57207772646172 7.98047012813255 0.3955382216600083 0.1154991722055756 0.6186918869242125 1.36 15.41 10.54 1.00 

All data are separated by spaces, and the number of data in each line is the same. The following is a sample code for reading and output:

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

int main() {
    // Read each word, separated by spaces
    ifstream fin("");
    string s;
    while(fin>>s) {
        cout<<s<<endl;
    }
    fin.close();
    s.clear();

    cout<<"==============================="<<endl;

    // Read by line, the end of each line is the entry distinction
    fin.open("");
    while(getline(fin,s)) {
        cout<<s<<endl;
    }
    return 0;
}

Combining the code that reads the data before, you can generally read the test data in combination like this:

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

int main() {
    ifstream fin;
    istringstream iss;
    string s;
    double t;
    // Read by line, the end of each line is the entry distinction
    fin.open("");
    while(getline(fin, s)) {
        iss.clear();
        iss.str(s);
        while(iss>>t) {
            cout<<t<<" ";
        }
        cout<<endl;
    }
    return 0;
}

This method is especially suitable for reading test data, especially when testing unit.