简体   繁体   中英

How to split a line read from a file into multiple ints?

I'm given a file with strings of numbers. my goal is to reformat them by adding stuff like commas and / because the lines of numbers represent information about items off a machine line.

Here's what I have but I'm not sure where to go from here:

void main()
{
    int num_dat_pts = 0, k;
    int id1, id2, day, month, year,
    shift, hour, minute, width, fraction, numbs;

    FILE * machine_samples_1;
    machine_samples_1 = fopen(filename, "r");
    fscanf(machine_samples_1, "%d%d%d%d%d%d%d%d", &id1, &id2, &day, &month, &year, &shift, &hour, &minute);

    fclose(machine_samples_1);
    getch();
}

Updated answer see below

To split just one line using just the standard library, use a stringstream:

#include <vector> // vector
#include <algorithm> // std::copy
#include <iterator>  // std::back_inserter and std::istream_iterator
#include <sstream>   // std::istringstream
#include <iostream>  // std::cout

int main()
{
    std::string line = "1 2 3 4 5 6 8 120";
    std::istringstream iss(line);

    std::vector<int> ints;
    std::copy(std::istream_iterator<int>(iss), {}, std::back_inserter(ints));

    std::cout << "parsed " << ints.size() << " integers: ";
    std::copy(ints.begin(), ints.end(), std::ostream_iterator<int>(std::cout, ";"));
}

See it Live on Coliru

Update

In case you wanted to parse multiple lines into structs of id1, id2 etc.:

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <fstream>
#include <iterator>

namespace qi = boost::spirit::qi;

struct DataPoint
{
    int id1;
    int id2;
    int day;
    int month;
    int year;
    int shift;
    int hour;
    int minute;
//  int width;
//  int fraction;
//  int numbs;
};

BOOST_FUSION_ADAPT_STRUCT(DataPoint, (int, id1)(int, id2)(int, day)(int, month)(int, year)(int, shift)(int, hour)(int, minute))

      //(int, width)
      //(int, fraction)
      //(int, numbs)

int main()
{
    std::ifstream machine_samples_1("filename");
    machine_samples_1.unsetf(std::ios::skipws);

    boost::spirit::istream_iterator first(machine_samples_1), last; 

    using qi::int_;
    std::vector<DataPoint> datapoints;
    bool ok = qi::phrase_parse(
            first, last, // input range
            (int_ >> int_ >> int_ >> int_ >> int_ >> int_ >> int_ >> int_) % qi::eol, // grammar
            qi::blank, // skipper
            datapoints);

    std::cout << "Parsed " << datapoints.size() << " data points";
}

See that Live on coliru as well

Sample output:

cat<<HERE >filename; clang++ -std=c++11 -Os main.cpp  && ./a.out
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
HERE
Parsed 3 data points

Since you have this tagged as C++, if your file contents look like this:

1 2 3 4 5 6 7 8 9

Then you can simply write code like this:

std::ifstream fin("myfile.txt");
std::string line;
if (std::getline(fin, line))
{
    int id1, id2, id3, id4, id5, id6, id7, id8, id9;
    std::istringstream iss(line);
    if (iss >> id1 >> id2 >> id3 >> id4 >> id5 >> id6 >> id7 >> id8 >> id9)
    {
        // do something with ids
    }
}

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