How to effectively represent and Increase MAC address

R

RThaden

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
 
M

mlimber

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/misc-technical-issues.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
 
V

VJ

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/
 
A

Amit

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?
 
R

RThaden

Amit said:
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
 

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,776
Messages
2,569,603
Members
45,186
Latest member
vinaykumar_nevatia

Latest Threads

Top