By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

IP Address, File Parsing - Help

P: n/a
Rob
I am trying to write a program with VC++ 6.0 to read a txt file which
looks like this:

Network Destination Netmask Gateway Interface
Metric
0.0.0.0 0.0.0.0 10.155.12.1 10.155.12.188
1
10.155.12.0 255.255.255.0 10.155.12.188 10.155.12.188
1
10.155.12.188 255.255.255.255 127.0.0.1 127.0.0.1
1
10.255.255.255 255.255.255.255 10.155.12.188 10.155.12.188
1
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1
1
224.0.0.0 224.0.0.0 10.155.12.188 10.155.12.188
1

Then have it pull two different variables (IP Addresses) from the file
and put them as printed char's in another command line to be used with
the system() command. The end command would look something like
this...

system("route add x.x.x.x mask 255.0.0.0 y.y.y.y metric 1")

The x.x.x.x would be one IP Address obtained from the file, and
y.y.y.y would be the second. The Line in the file would be constant,
but the x.x.x.x and y.y.y.y would vary...

Any suggestions?

As always, any help is greatly appreciated.

Thanks,
Rob

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Rob" <ro******@yahoo.com> wrote in message
news:34*************************@posting.google.co m...
I am trying to write a program with VC++ 6.0 to read a txt file which
looks like this:

Network Destination Netmask Gateway Interface
Metric
0.0.0.0 0.0.0.0 10.155.12.1 10.155.12.188
1
10.155.12.0 255.255.255.0 10.155.12.188 10.155.12.188
1
10.155.12.188 255.255.255.255 127.0.0.1 127.0.0.1
1
10.255.255.255 255.255.255.255 10.155.12.188 10.155.12.188
1
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1
1
224.0.0.0 224.0.0.0 10.155.12.188 10.155.12.188
1

Then have it pull two different variables (IP Addresses) from the file
and put them as printed char's in another command line to be used with
the system() command. The end command would look something like
this...

system("route add x.x.x.x mask 255.0.0.0 y.y.y.y metric 1")

The x.x.x.x would be one IP Address obtained from the file, and
y.y.y.y would be the second. The Line in the file would be constant,
but the x.x.x.x and y.y.y.y would vary...

Any suggestions?


Yes. Post your code, and we'll offer assistance.
And express your goal a bit more clearly, please.

-Mike
Jul 22 '05 #2

P: n/a
Hi,

Open a stream

read them

string Destination, Netmask, Gateway, Interface;

File >> Destination >> Netmask >> Gateway >> Interface

stringstream Command;

Command << "route add " << Destination << " mask 255.0.0.0 " << Gateway <<
" metric 1";

system( Command.str().c_str() );

Regards Ron AF Greve.
"Rob" <ro******@yahoo.com> wrote in message
news:34*************************@posting.google.co m...
I am trying to write a program with VC++ 6.0 to read a txt file which
looks like this:

Network Destination Netmask Gateway Interface
Metric
0.0.0.0 0.0.0.0 10.155.12.1 10.155.12.188
1
10.155.12.0 255.255.255.0 10.155.12.188 10.155.12.188
1
10.155.12.188 255.255.255.255 127.0.0.1 127.0.0.1
1
10.255.255.255 255.255.255.255 10.155.12.188 10.155.12.188
1
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1
1
224.0.0.0 224.0.0.0 10.155.12.188 10.155.12.188
1

Then have it pull two different variables (IP Addresses) from the file
and put them as printed char's in another command line to be used with
the system() command. The end command would look something like
this...

system("route add x.x.x.x mask 255.0.0.0 y.y.y.y metric 1")

The x.x.x.x would be one IP Address obtained from the file, and
y.y.y.y would be the second. The Line in the file would be constant,
but the x.x.x.x and y.y.y.y would vary...

Any suggestions?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3

P: n/a

"Rob" <ro******@yahoo.com> wrote in message
news:34*************************@posting.google.co m...
I am trying to write a program with VC++ 6.0 to read a txt file which
looks like this:

Network Destination Netmask Gateway Interface
Metric
0.0.0.0 0.0.0.0 10.155.12.1 10.155.12.188
1
10.155.12.0 255.255.255.0 10.155.12.188 10.155.12.188
1
10.155.12.188 255.255.255.255 127.0.0.1 127.0.0.1
1
10.255.255.255 255.255.255.255 10.155.12.188 10.155.12.188
1
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1
1
224.0.0.0 224.0.0.0 10.155.12.188 10.155.12.188
1

Then have it pull two different variables (IP Addresses) from the file
and put them as printed char's in another command line to be used with
the system() command. The end command would look something like
this...

system("route add x.x.x.x mask 255.0.0.0 y.y.y.y metric 1")

The x.x.x.x would be one IP Address obtained from the file, and
y.y.y.y would be the second. The Line in the file would be constant,
but the x.x.x.x and y.y.y.y would vary...

Any suggestions?

As always, any help is greatly appreciated.


You could create a class which resembles & stores that actual data records
you want to read. This class should of course have access functions as well
as an inserter and extractor operator. Use stream iterators & a collection
to read the records from a file and store them in the container. Then you
just have to iterate over the container accessing the respective information
you need to assemble the string.

If you need more help then post some code which you already have.

HTH
Chris


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #4

P: n/a
Hi.

ro******@yahoo.com (Rob) wrote in message news:<34*************************@posting.google.c om>...

Then have it pull two different variables (IP Addresses) from the file
and put them as printed char's in another command line to be used with
the system() command. The end command would look something like
this...

system("route add x.x.x.x mask 255.0.0.0 y.y.y.y metric 1")

The x.x.x.x would be one IP Address obtained from the file, and
y.y.y.y would be the second. The Line in the file would be constant,
but the x.x.x.x and y.y.y.y would vary...

Any suggestions?


The first queston is why to use C++ to perform this task?
For this task I'll recommed you one of more script languages,
such as Perl, or Awk.
But if You need the C++ realization of this code, than I suggest
you to define an ADT (Abstract Data Type) IPAddress such as

class IPAddress
{
public:
IPAddress(unsigned char i1 = 0,
unsigned char i2 = 0,
unsigned char i3 = 0,
unsigned char i4 = 0); // simplest way to create
// an IP address
IPAddress(const char* value); // create address from string
// such as "0.0.0.0"
// another constructors
IPAddress(unsigned char* addr);
// copy constructor & assigment operator
const char* getString() const
{
static char buff[16]; // three numbers
sprintf(buff, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
return buff;
}

// here presenta a short variant of
// thist function, but You can add error handling operations,
// and port this to standard C++ Library
// also as function above
bool readString(const char* str)
{
return sscanf(str, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);
}

// next functions can be such as insertors and extractors
// (if you working with Std C++ IO)
friend ostream& operator<< ( ostream& os, const Date& dt );
friend istream& operator>> ( istream& os, Date& dt );
private:
unsigned char ip[4];
};

Also You can add other service functions such as mask generating and
constants such as BroadcastIP, LocalhostIP etc.

File parses will be very simple:

ofstream ofs("file.name");
if(ofs)
{
IPAddress addr;
ofs>>addr; // sure it works only You defined extractor :-)
}

C-ya
-----------

Basil Pasternak pa********@mail.ru

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #5

P: n/a
"Moonlit" <al******@jupiter.universe> wrote in message news:3fbde35b$0$1505
Hi,

Open a stream

read them

string Destination, Netmask, Gateway, Interface;
Good solution. To extend it you can make a class, and write operator<< and
operator>> for it.

class Network {
public:
private:
std::string Destination;
std::string Netmask;
std::string Gateway;
std::string Interface;
};

File >> Destination >> Netmask >> Gateway >> Interface
Move the above code into std::istream& operator>>(std::istream&, Network&).

stringstream Command;

Command << "route add " << Destination << " mask 255.0.0.0 " << Gateway << " metric 1";

system( Command.str().c_str() );
BTW, what's the difference between stringstream and ostringstream?

"Rob" <ro******@yahoo.com> wrote in message
news:34*************************@posting.google.co m...
I am trying to write a program with VC++ 6.0 to read a txt file which
looks like this:

Network Destination Netmask Gateway Interface
Metric
0.0.0.0 0.0.0.0 10.155.12.1 10.155.12.188
1
10.155.12.0 255.255.255.0 10.155.12.188 10.155.12.188
1
10.155.12.188 255.255.255.255 127.0.0.1 127.0.0.1
1
10.255.255.255 255.255.255.255 10.155.12.188 10.155.12.188
1
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1
1
224.0.0.0 224.0.0.0 10.155.12.188 10.155.12.188
1


--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #6

P: n/a

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:Wc*********************@bgtnsc05-news.ops.worldnet.att.net...
"Moonlit" <al******@jupiter.universe> wrote in message news:3fbde35b$0$1505
Hi,

Open a stream

read them

string Destination, Netmask, Gateway, Interface;
Good solution. To extend it you can make a class, and write operator<<

and operator>> for it.

class Network {
public:
private:
std::string Destination;
std::string Netmask;
std::string Gateway;
std::string Interface;
};

File >> Destination >> Netmask >> Gateway >> Interface
Move the above code into std::istream& operator>>(std::istream&,

Network&).
stringstream Command;

Command << "route add " << Destination << " mask 255.0.0.0 " <<
Gateway <<
" metric 1";

system( Command.str().c_str() );


BTW, what's the difference between stringstream and ostringstream?


'ostringstream' is an output stream. 'stringstream'
is bidirectional.

-Mike
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #7

P: n/a
ro******@yahoo.com (Rob) writes:

|> I am trying to write a program with VC++ 6.0 to read a txt file
|> which looks like this:

|> Network Destination Netmask Gateway Interface
|> Metric
|> 0.0.0.0 0.0.0.0 10.155.12.1 10.155.12.188
|> 1
|> 10.155.12.0 255.255.255.0 10.155.12.188 10.155.12.188
|> 1
|> 10.155.12.188 255.255.255.255 127.0.0.1 127.0.0.1
|> 1
|> 10.255.255.255 255.255.255.255 10.155.12.188 10.155.12.188
|> 1
|> 127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1
|> 1
|> 224.0.0.0 224.0.0.0 10.155.12.188 10.155.12.188
|> 1

|> Then have it pull two different variables (IP Addresses) from the
|> file and put them as printed char's in another command line to be
|> used with the system() command. The end command would look something
|> like this...

|> system("route add x.x.x.x mask 255.0.0.0 y.y.y.y metric 1")

|> The x.x.x.x would be one IP Address obtained from the file, and
|> y.y.y.y would be the second. The Line in the file would be
|> constant, but the x.x.x.x and y.y.y.y would vary...

|> Any suggestions?

The most obvious solution is to use getline, then Boost::regex to split
the line up into the separate fields. I'd do this second bit in two
steps: first matching fields like x.x.x.x, then splitting each field up
into its constituent subfields. Except that in your case, you probably
don't even need to parse the subfields (except maybe for error
handling).

--
James Kanze mailto:ka***@gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France +33 1 41 89 80 93

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #8

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:rOOvb.10884
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
"Moonlit" <al******@jupiter.universe> wrote in message
stringstream Command;

Command << "route add " << Destination << " mask 255.0.0.0 " << Gateway
BTW, what's the difference between stringstream and ostringstream?

'ostringstream' is an output stream. 'stringstream'
is bidirectional.


So when you create a stringstream do you have to specify which modes you are
using?
stringstream Command;


Should that be

stringstream Command(std::ios::out);
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #9

P: n/a

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:25**********************@bgtnsc05-news.ops.worldnet.att.net...
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:rOOvb.10884
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
"Moonlit" <al******@jupiter.universe> wrote in message stringstream Command;
>
> Command << "route add " << Destination << " mask 255.0.0.0 " << Gateway

BTW, what's the difference between stringstream and ostringstream?
'ostringstream' is an output stream. 'stringstream'
is bidirectional.


So when you create a stringstream do you have to specify which modes you

are using?
Only if you don't want the default.

The std::basic_stringstream class constructors have default
'open mode' argument of ios_base::out|ios_base::in.
(See 27.7.4)
stringstream Command;


Should that be

stringstream Command(std::ios::out);


You could do that, which would mean you couldn't use it for
input. But in that case why not just use an ostringstream?

-Mike
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.