Commented data file

Discussion in 'C++' started by Moin, Sep 27, 2012.

  1. Moin

    Moin Guest

    In FORTRAN 90/95, one can include comments with each input data line; for eaxmample
    19 37 59 ! A set of prime numbers
    25 64 81 225 ! A set of perfect squares
    Is it possible to include such comments in an input data file for C/C++?
    Moin, Sep 27, 2012
    #1
    1. Advertising

  2. On Sep 27, 8:25 am, Moin <> wrote:
    > In FORTRAN 90/95, one can include comments with each input data line; foreaxmample
    > 19 37 59 ! A set of prime numbers
    > 25 64 81 225 ! A set of perfect squares
    > Is it possible to include such comments in an input data file for C/C++?


    Sure. Real quick and inelegantly, here is a program that will read
    your example data if it's in a text file. But there's no higher-level
    standard library function that will do it "automatically" if that's
    what you're asking. Of course you could modify the code below
    somewhat and easily write one yourself.


    #include <fstream> // ifstream
    #include <sstream> // stringstream
    #include <iostream> // cout, endl -- for testing
    #include <string>
    #include <vector>
    #include <iterator> // istream_iterator, back_inserter
    #include <algorithm> // copy

    using namespace std;

    int main(int argc, char ** argv)
    {
    // too lazy to put in error checking, assume this works
    ifstream in(argv[1]);
    string line;
    vector<vector<int> > numbers;

    while (getline(in, line)) {
    // Strip everything at and after '!' if present
    // You could actually even omit this line and the first
    // copy() line below would still stop at '!' since it
    // can't be interpreted as an integer.
    line = line.substr(0, line.find('!'));

    // parse line -- again, no error checking
    stringstream s(line);
    numbers.emplace_back(); // C++2011
    copy(istream_iterator<int>(s), istream_iterator<int>(),
    back_inserter(numbers.back()));

    // make sure it works by printing numbers to console
    copy(numbers.back().begin(), numbers.back().end(),
    ostream_iterator<int>(cout, " "));
    cout << endl;
    }

    // now numbers[0] == { 19, 37, 59 } and
    // numbers[1] == { 25, 64, 81, 225 }

    return 0;
    }
    Kevin McCarty, Sep 27, 2012
    #2
    1. Advertising

  3. Moin

    Rui Maciel Guest

    Moin wrote:

    > In FORTRAN 90/95, one can include comments with each input data line; for
    > eaxmample 19 37 59 ! A set of prime numbers
    > 25 64 81 225 ! A set of perfect squares
    > Is it possible to include such comments in an input data file for C/C++?


    Of course. You just need to define your data format and write a parser that
    supports it. Here's a link to a small C++ parser for a INI-inspired data
    format.

    http://sourceforge.net/projects/minip/


    Rui Maciel
    Rui Maciel, Sep 27, 2012
    #3
  4. Moin <> wrote:
    > Is it possible to include such comments in an input data file for C/C++?


    Define "input data file for C/C++".
    Juha Nieminen, Sep 28, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Heavey
    Replies:
    1
    Views:
    362
    Oisín Grehan
    Dec 2, 2003
  2. =?Utf-8?B?YWRvbGYgZ2FybGlj?=

    BUG: commented out server side controls in html still run

    =?Utf-8?B?YWRvbGYgZ2FybGlj?=, Nov 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    414
    John Saunders
    Nov 22, 2004
  3. Rod Snyder

    commented out form tag causing error

    Rod Snyder, Sep 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    346
    Rod Snyder
    Sep 14, 2005
  4. bronski
    Replies:
    0
    Views:
    434
    bronski
    Feb 16, 2005
  5. Replies:
    0
    Views:
    323
Loading...

Share This Page