简体   繁体   中英

How c++ reads the values from txt file by specific n characters

I have a text file with many values inside, and I want to read this file. The each value is 20 characters in length, eg -5.3815657119783E-04 with minus is 20 characters, and 3.4684258817593E-02 with one whitespace before 3 is also 20 characters in lengh. In my original method, i want to use the std::stringstream to realize this function, however stringstream splits the values by whitespace.

Most of my data is connected together. Therefore, I want to read this file every 20 characters to form one value, which means I want to split the data by 20 characters by c++, then produce 1 D vector.

Here is the data file:

-5.3815657119783E-04-6.8673010013991E-01-7.5323285000788E-03
 3.4684258817593E-02 7.8204534345607E-02-9.0749590089286E-21
-3.2288090365673E-01 9.1882352987172E-02-3.6568412172092E-01
-1.6851899589453E-13-3.6952158259739E-06-1.7702955653531E-07
-1.3297325992783E-06-5.9693615642877E-04-3.8099426849223E-08
 3.4698984898706E-08-4.6509379769221E-12-2.2296405498928E-02
-5.2019999391601E-14-4.7969995006506E-08 5.6662120105254E-08
 8.9017338669484E-08-2.9332683813429E-06 1.0647933483993E-06
-6.7543843798968E-05-2.1529934384702E-03 2.2028879943185E-05
 1.1715465910941E+05-2.5234840649194E+05 1.2213290262328E+05
 6.1143067398521E-03 1.0479815336955E-04 7.8911962315577E-08
 7.2476042335761E-01 4.1208576787560E-03 0.0000000000000E+00
 5.3389720849081E-03 8.4526321374548E-05 4.8860066505864E-08
 7.1085208590414E-06 4.5249593432595E-01 4.1468076430511E-04
 5.6630655497271E-10 4.0969474876063E-11 9.7240386803972E-05
 6.5005706844622E-11 5.1549675717799E-04 8.1291425432847E-18
 3.4017603643097E-07 4.4928090110890E-03 1.8886378497020E-10
 6.2728934586839E-11 4.7522407515395E-08 3.3417538614997E-07
 1.9670991535049E-07 1.9522239039334E-08 2.7359845813293E-18

My original codes in c++:

std::vector<double> value;

ifstream infile;
infile.open("test-file");
int start_line_ = 0;

while (!infile.eof())
{
  string line;
  getline(infile, line);
  if(rows >= start_line_)
  {
    double number;
    std::stringstream stream(line);
    while(stream >> number)
    {
            value.push_back(number);        
    }
  }
 infile.close();
}

My codes will get some values connected together, because they are not separated by whitespace.

As you already have the line in a string:

for (int i = 0; i + 20 <= line.size(); i += 20) {
   double number;
   istringstream(line.substr(i, 20)) >> number;
   value.push_back(number);
}

If you're trying to build a vector of doubles, based on 20 character strings in the input file you could do this:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

int main()
{
    std::vector<double> values;
    std::ifstream infile;
    infile.open("test-file");
    std::string line;

    // Read each line into a string
    while (std::getline(infile, line))
    {
        // Iterate every 20 chars in the current line
        for (size_t i = 0; i < line.size(); i += 20) {

            // Get 20 char substring from current position,
            // put into a stringstream
            std::istringstream os(line.substr(i, 20));

            // Put the stringstream into a double and push this to the values vector
            double d;
            os >> d;
            values.push_back(d);
        } 
    }
    infile.close();

    // Print them to check
    for (auto& el : values)
        std::cout << el << '\n';

    return 0;
}

Loop through each line in the input, breaking the line into 20 char substrings.

Convert each substring to a double by means of std::istringstream os and push this to the std::vector<double> .

References

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM