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

How to effectively represent and Increase MAC address

P: n/a
Hi all,

I have a text file with a list of MAC addresses.
Each time, my program is called it reads the last MAC address entry
from the file, increases it by one, writes this new address into a 6
byte binary file and stores the new address in text representation in
the text file. Looks like this

00-0C-F1-B9-A1-11
00-0C-F1-B9-A1-12
00-0C-F1-B9-A1-13
....

My questions are:
1. How do I effectively read the last line of the file.
Right now, I read the whole file until I come to the last line. Not
very elegant but maybe the only quick way to do it?

2. How would you represent the MAC addresses in order to increase them?
I am reading them, ignoring the dashes between the bytes and convert
them with strtol into a 32 bit integer (where I should use 48 bits but
I assume that the first 16 bytes are not changed).
Then I increase this number and convert it back to chars. Sounds
horrible, doesn't it?
Is there a better way?

Thanks in advance,

Rainer Thaden

Oct 26 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
RT*****@web.de wrote:
I have a text file with a list of MAC addresses.
Each time, my program is called it reads the last MAC address entry
from the file, increases it by one, writes this new address into a 6
byte binary file and stores the new address in text representation in
the text file. Looks like this

00-0C-F1-B9-A1-11
00-0C-F1-B9-A1-12
00-0C-F1-B9-A1-13
...

My questions are:
1. How do I effectively read the last line of the file.
Right now, I read the whole file until I come to the last line. Not
very elegant but maybe the only quick way to do it?
Use std::ifstream::seekg().
2. How would you represent the MAC addresses in order to increase them?
I am reading them, ignoring the dashes between the bytes and convert
them with strtol into a 32 bit integer (where I should use 48 bits but
I assume that the first 16 bytes are not changed).
Then I increase this number and convert it back to chars. Sounds
horrible, doesn't it?
Is there a better way?
I generally prefer std::stringstreams to C-style number conversions
(cf.
http://parashift.com/c++-faq-lite/mi....html#faq-39.1
and the FAQ following). You could do a lazy conversion where you
convert the least significant byte, add one, and if and only if there
is a carry, convert the next byte and add one to it. Continue
converting and adding as long as there is a carry.

Cheers! --M

Oct 26 '06 #2

P: n/a
VJ
RT*****@web.de wrote:
Hi all,

I have a text file with a list of MAC addresses.
Each time, my program is called it reads the last MAC address entry
from the file, increases it by one, writes this new address into a 6
byte binary file and stores the new address in text representation in
the text file. Looks like this

00-0C-F1-B9-A1-11
00-0C-F1-B9-A1-12
00-0C-F1-B9-A1-13
...

My questions are:
1. How do I effectively read the last line of the file.
Right now, I read the whole file until I come to the last line. Not
very elegant but maybe the only quick way to do it?

2. How would you represent the MAC addresses in order to increase them?
I am reading them, ignoring the dashes between the bytes and convert
them with strtol into a 32 bit integer (where I should use 48 bits but
I assume that the first 16 bytes are not changed).
Then I increase this number and convert it back to chars. Sounds
horrible, doesn't it?
Is there a better way?

Thanks in advance,

Rainer Thaden


Tried using istream:: seekg?

Here is a link for ifstream:
http://www.cplusplus.com/ref/iostream/ifstream/
Oct 26 '06 #3

P: n/a
I am reading them, ignoring the dashes between the bytes and convert
them with strtol into a 32 bit integer (where I should use 48 bits but
I assume that the first 16 bytes are not changed).
Then I increase this number and convert it back to chars. Sounds
horrible, doesn't it?
This way you'll get character not supported by MAC address. How are you
handling that?

Oct 27 '06 #4

P: n/a

VJ wrote:
RT*****@web.de wrote:
Hi all,

I have a text file with a list of MAC addresses.
Each time, my program is called it reads the last MAC address entry
from the file, increases it by one, writes this new address into a 6
byte binary file and stores the new address in text representation in
the text file. Looks like this

00-0C-F1-B9-A1-11
00-0C-F1-B9-A1-12
00-0C-F1-B9-A1-13
...

My questions are:
1. How do I effectively read the last line of the file.
Right now, I read the whole file until I come to the last line. Not
very elegant but maybe the only quick way to do it?

2. How would you represent the MAC addresses in order to increase them?
I am reading them, ignoring the dashes between the bytes and convert
them with strtol into a 32 bit integer (where I should use 48 bits but
I assume that the first 16 bytes are not changed).
Then I increase this number and convert it back to chars. Sounds
horrible, doesn't it?
Is there a better way?

Thanks in advance,

Rainer Thaden

Tried using istream:: seekg?

Here is a link for ifstream:
http://www.cplusplus.com/ref/iostream/ifstream/
Yes, I tried. But what do you seek for? The end of the file? And where
to put the position after finding it? Then, you need to make
assumptions over the length of a line. Bad, if you want to allow
comments in lines.

Best regards,

Rainer

Nov 2 '06 #5

P: n/a

Amit wrote:
I am reading them, ignoring the dashes between the bytes and convert
them with strtol into a 32 bit integer (where I should use 48 bits but
I assume that the first 16 bytes are not changed).
Then I increase this number and convert it back to chars. Sounds
horrible, doesn't it?
This way you'll get character not supported by MAC address. How are you
handling that?
No, I don't

If, e.g., the last byte of the MAC address is bf, then I convert the
chars 'bf' to the number bf, increase it to get c0, save it in a binary
file, convert it back to two chars 'c0' and save that in my text file.
I see no problem here.

Best regards,

Rainer

Nov 2 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.