Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Commented data file

Reply
Thread Tools

Commented data file

 
 
Moin
Guest
Posts: n/a
 
      09-27-2012
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++?
 
Reply With Quote
 
 
 
 
Kevin McCarty
Guest
Posts: n/a
 
      09-27-2012
On Sep 27, 8:25*am, Moin <(E-Mail Removed)> 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;
}
 
Reply With Quote
 
 
 
 
Rui Maciel
Guest
Posts: n/a
 
      09-27-2012
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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-28-2012
Moin <(E-Mail Removed)> wrote:
> Is it possible to include such comments in an input data file for C/C++?


Define "input data file for C/C++".
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
commented properties in ejb file. swun2010@gmail.com Java 0 01-31-2008 12:09 AM
commented out form tag causing error Rod Snyder ASP .Net 2 09-14-2005 12:42 AM
problem: writing out an XML file always includes the commented DTD in the beginning. Why? bronski Java 0 02-16-2005 11:43 AM
BUG: commented out server side controls in html still run =?Utf-8?B?YWRvbGYgZ2FybGlj?= ASP .Net 1 11-22-2004 12:34 PM
Commented Code Not Treated As Commented Code Jim Heavey ASP .Net 1 12-02-2003 02:25 PM



Advertisments