0

Following is my code. In this program I use system function and passe a command line argument to get the mac address of the pc and then write it into the txt file. txt file is creating successfully. But When I try to open the txt file which was created it wont show anything. It show letter M and some blank spaces. Any idea why is this happening ? Thank you.

#include<stdio.h>

 int main()
 {

  system("wmic nic where (AdapterTypeId=0 AND netConnectionStatus=2) get MACAddress >macaddress.txt");
  FILE * fpointer=fopen("macaddress.txt","r");
  char buffer[500];
  while(!feof(fpointer)){
    fgets(buffer,500,fpointer);
    puts(buffer);
  }

  fclose(fpointer);
 }
Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Kalana Mihiranga
  • 488
  • 7
  • 18
  • Do you really have a newline in the argument to `system()`, or is that a copying error? It will probably cause the command to be executed incorrectly. – Barmar Jan 12 '19 at 19:41
  • 3
    [Why `while (!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Jan 12 '19 at 19:41
  • Do you see the correct output if you open the file with a text editor instead of C? – Barmar Jan 12 '19 at 19:42
  • it is a copying error.Mac address are copied to the txt file successfully.I can see all the mac address when I open the txtfile.But when when I open it using c It show letter M and some blank spaces. – Kalana Mihiranga Jan 12 '19 at 19:44
  • 1
    The file that is written is in UCS-2 LE BOM format. You can't read it as ascii text, you need to read wide characters. – Retired Ninja Jan 12 '19 at 19:47
  • There is no text but encoded text. You can't read a text file unless you know its character encoding. UTF-16 is used in the Windows API. Not sure why wmic would use it for output, though, especially to stdout. Many C and C++ programs (via the startup code [that which calls your `main`] provided by the vendor) try to initialize standard library functions to the user's console encoding. – Tom Blodget Jan 12 '19 at 22:28

2 Answers2

1

This will do what you want, but if instead of just printing the contents of the file you actually want to do something with it and you need the text as ASCII you'll need to perform that conversion yourself from wide characters.

Since this particular file is just normal letters and numbers text you can convert the wide string to narrow with sprintf.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    system("wmic nic where (AdapterTypeId=0 AND netConnectionStatus=2) get MACAddress > macaddress.txt");
    //Binary mode tells fgetws to read wide characters instead of multi-byte.
    FILE * fp = fopen("macaddress.txt", "rb"); 
    if (fp)
    {
        wchar_t buffer[500] = { 0 };
        fread(buffer, 1, 2, fp); //read and discard BOM
        while (fgetws(buffer, 500, fp))
        {
            // %ls means the parameter is a wide string.
            // %S also works in Visual Studio
            printf("%ls", buffer);
            //Convert wide characters to ASCII
            //This assumes the wide characters are all in the ASCII range
            char ascii[500] = { 0 };
            sprintf(ascii, "%ls", buffer);
            printf("%s", ascii);
        }
        fclose(fp);
    }
    return 0;
}
Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
0

It is not an ASCII encoded file. Here is a dump

            0  1  2  3   4  5  6  7   8  9  A  B   C  D  E  F
0000:0000  FF FE 4D 00  41 00 43 00  41 00 64 00  64 00 72 00   ■M.A.C.A.d.d.r.
0000:0010  65 00 73 00  73 00 20 00  20 00 20 00  20 00 20 00  e.s.s. . . . . .
0000:0020  20 00 20 00  20 00 20 00  0D 00 0A 00

etc so as not to reveal my MAC address.

Note too it contains zeros which will terminate any string output after reading with fgets.

My text editor (Notepad++) shows the correct content because it sets the right text format automatically.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56