Finding UUID in text file

Been trying to figure this out for hours.

In my code I am generating a text file by this command.

system(wmic csproduct get uuid > test.txt);

It prints in the text file as:
1
2
UUID
XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX


How do I make it so that it finds that line of the text and then place it into a if-else statement to run a certain function?

Last edited on
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#pragma warning(disable:4996) // disable microsoft rubbish (::fopen may be unsafe etc.)
#include <iostream>
#include <string>
#include <stdio.h>
#include <cctype>
#include <iomanip>

std::string get_system_uuid()
{
    if( std::system( "wmic csproduct get uuid > test.txt" ) == 0 )
    {
        auto file = ::fopen( "test.txt", "rt, ccs=UNICODE" ) ; // open the file for unicode input

        enum { BUFFSZ = 1000, UUID_SZ = 36 };
        wchar_t wbuffer[BUFFSZ] ; // buffer to hold unicode characters

        if( file && // file was succesffully opened
            ::fgetws( wbuffer, BUFFSZ, file ) && // successfully read (and discarded) the first line
            ::fgetws( wbuffer, BUFFSZ, file ) ) // successfully read the second line
        {
            char cstr[BUFFSZ] ; // buffer to hold the converted c-style string
            if( ::wcstombs( cstr, wbuffer, BUFFSZ ) > UUID_SZ ) // convert unicode to utf-8
            {
                std::string uuid = cstr ;
                while( !uuid.empty() && std::isspace( uuid.back() ) ) uuid.pop_back() ; // discard trailing white space
                return uuid ;
            }
        }
    }

    return {} ; // failed, return empty string
}

int main()
{
    const std::string smbios_uuid = get_system_uuid() ;
    std::cout << "system uuid (smbios): " << std::quoted(smbios_uuid) << '\n' ;

    // if( smbios_uuid == "..." ) { /* run certain function */ }
}
I figured there was something more to it than just trying to find a string in that kind of file.

The only problem I had was with this line:
return {} ; // failed, return empty string

I just changed it to this:
return 0; // failed, return empty string

Thanks a lot. I didn't expect anyone to answer and I learned quite a bit.

I just changed it to this:
return 0; // failed, return empty string


That results in the construction of a string from a null pointer which evokes undefined behavior. return std::string(); instead.
Thank you, that worked out fine.
Topic archived. No new replies allowed.