Below is the code that I have to find the Mac address of any pc (physical host ID) I struggle to make it print to a file but now i have a problem... It prints MAC Address: XX-XX-XX-XX-XX-XX to C:\\Physical-Address.txt but I only want it to print the XX-XX-XX etc part... and since I cant get it to do that I decided I want to erase the "MAC Address: " part of the file and print the XX-XX to a new file inorder to put it into a string... ive been working on this for 3 days now and cant find an answer =( I tried getline, fstream commands so i left those headers in the code still incase they are needed... Also I had to redirect the mac address to a text file since i dont know how to get it into a string any help of any sort is appreciated but pls remember Im very new to programming and i self teach... been about a 3 months of straight studying...

#include "stdafx.h"
#include <Windows.h>
#include <lm.h>
#include <assert.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <winuser.h>
#include <fstream>
#pragma comment(lib, "Netapi32.lib")

using namespace std;

// Prints the MAC address stored in a 6 byte array to stdout
static void PrintMACaddress(unsigned char MACData[])
	printf("MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n", 
		MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);

// Fetches the MAC address and prints it
static void GetMACaddress(void)
	unsigned char MACData[8];						// Allocate data structure for MAC (6 bytes needed)

	WKSTA_TRANSPORT_INFO_0 *pwkti;					// Allocate data structure for Netbios
	DWORD dwEntriesRead;
	DWORD dwTotalEntries;
	BYTE *pbBuffer;
	// Get MAC address via NetBios's enumerate function
	NET_API_STATUS dwStatus = NetWkstaTransportEnum(
		NULL,						// [in]  server name
		0,							// [in]  data structure to return
		&pbBuffer,					// [out] pointer to buffer
		MAX_PREFERRED_LENGTH,		// [in]  maximum length
		&dwEntriesRead,				// [out] counter of elements actually enumerated
		&dwTotalEntries,			// [out] total number of elements that could be enumerated
		NULL);						// [in/out] resume handle
	assert(dwStatus == NERR_Success);

	pwkti = (WKSTA_TRANSPORT_INFO_0 *)pbBuffer;		// type cast the buffer

	for(DWORD i=1; i< dwEntriesRead; i++)			// first address is 00000000, skip it
	{												// enumerate MACs and print
		swscanf((wchar_t *)pwkti[i].wkti0_transport_address, L"%2hx%2hx%2hx%2hx%2hx%2hx", 
			&MACData[0], &MACData[1], &MACData[2], &MACData[3], &MACData[4], &MACData[5]);

	// Release pbBuffer allocated by above function
	dwStatus = NetApiBufferFree(pbBuffer);
	assert(dwStatus == NERR_Success);


int _tmain(int argc, _TCHAR* argv[])
	FILE *stream ;		//Redirect Output from console to a TextFile
	if((stream = freopen("C:\\Physical-Address.txt", "w", stdout)) == NULL)

	GetMACaddress();							// Obtain MAC address of adapters
	stream = freopen("CON", "w", stdout);  //Redirect Output to the Screen
	cout<<"Physical Address Found, C:\\Physical-Address.txt\n\n";

                                          //dont know what to put here


Edited by triumphost: n/a

7 Years
Discussion Span
Last Post by triumphost

I changed that part I didnt see that I was making it print MAC Address: instead of just the result but now I need help to read that from the file and put it in a string to replace in another file

static void PrintMACaddress(unsigned char MACData[])
		MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);

If you want to read a string from a file (I assume you meant a text file?), then you could use this:

ifstream in("C:\\Physical-Address.txt");// are you sure it's C:\\ and not C:\?
    if (!in)
        cout << "There was a problem with opening the file for reading." << endl;
         string AString;
         cin >> AString;

ok so on my computer the path to the Physical-Address file is C:\ but if I put that it doesnt write to the file so I put C:\\ and then the file shows up on the local disk, I have also edited the code and now I have this piece added and it reads it and inputs it to the string

string HostID;
	ifstream myfile ("C:\\Physical-Address.txt");
	if (myfile.is_open())
		while (! myfile.eof() )
			getline (myfile,HostID);
			cout << HostID << endl;
	else cout << "Unable to open file";

Ok but now I need more help lol I hope u guys dont mind... but yea I need help on creating a directory in the my documents folder with the code but everyone's my documents folder is a different path.
Im assuming I use CreateDirectory()? but is that for console applications? But how can i create a directory in their my documents folder if I dont know the path...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.