IP Address, File Parsing - Help

R

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
 
M

Mike Wahler

Rob said:
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
 
M

Moonlit

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.
 
C

Chris Theis

Rob said:
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
 
B

Basil Pasternak

Hi.

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
 
S

Siemel Naran

Moonlit said:
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?
 
M

Mike Wahler

Siemel Naran said:
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;
};



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

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

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

-Mike
 
J

James Kanze

(e-mail address removed) (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).
 
S

Siemel Naran

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

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

Should that be

stringstream Command(std::ios::eek:ut);
 
M

Mike Wahler

Siemel Naran said:
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::eek:ut|ios_base::in.
(See 27.7.4)
Should that be

stringstream Command(std::ios::eek:ut);

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top