Home > Enterprise >  How do i write inside of a txt file with c
How do i write inside of a txt file with c

Time:05-28

I have been trying to insert the hardware id inside of a file called hardwareid2.txt, this is where the hardware id that i am extracting should be inserted, however it doesn't seem to be doing that and im not sure why, All the code seems to be doing is creating the file but not writing inside of the file. could someone help me debug this?


#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <fstream>

using namespace std;

HW_PROFILE_INFO hwProfileInfo;
std::string hwid = hwProfileInfo.szHwProfileGuid;
int main()
{

    if(GetCurrentHwProfile(&hwProfileInfo) != NULL){
        std::ofstream hwidfile { "hardwareid2.txt" };
        hwidfile.open("hardwareid2.txt");
        hwidfile <<hwid;
        hwidfile.close();
        printf("Hardware GUID: %s\n",     hwProfileInfo.szHwProfileGuid);
        printf("Hardware Profile: %s\n", hwProfileInfo.szHwProfileName);
    }else{
        return 0;
    }

    getchar();



}

CodePudding user response:

In the original code

HW_PROFILE_INFO hwProfileInfo;
std::string hwid = hwProfileInfo.szHwProfileGuid;

The call to GetCurrentHwProfile that will load the system profile into hwProfileInfo is conspicuously absent between the definition of hwProfileInfo and its usage to initialize hwid. That means hwProfileInfo; is sitting in its default state, a big block of zeros because its been declared at global scope. szHwProfileGuid will be an empty, immediately null-terminated string, and that emptiness will be used to initialize hwid.

Much later hwidfile <<hwid; will write the empty string to the file stream. printf("Hardware GUID: %s\n", hwProfileInfo.szHwProfileGuid); prints the correct value because hwProfileInfo has been updated since hwid was initialized with the empty string.

Fix: Get rid of hwid. We don't need it.

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    HW_PROFILE_INFO hwProfileInfo; // unless we have a very good reason, this should 
                                   // not be global
    if(GetCurrentHwProfile(&hwProfileInfo) != NULL)
    { // update info was a success. NOW we have a GUID and can do stuff with 
      // hwProfileInfo
        std::ofstream hwidfile { "hardwareid2.txt" };
        hwidfile.open("hardwareid2.txt");
        if (!(hwidfile << hwProfileInfo.szHwProfileGuid)) 
        { // will fail if can't write for any reason, like file didn't open
            std::cout << "File write failed\n";
            return -1;
        }
        // hwidfile.close(); don't need this. hwidfile will auto-close when it exists scope
        printf("Hardware GUID: %s\n",     hwProfileInfo.szHwProfileGuid);
        printf("Hardware Profile: %s\n", hwProfileInfo.szHwProfileName);
    }
    else
    {
        std::cout << "GetCurrentHwProfile failed\n";
        return -1;
    }

    getchar();
}

But if we do need it, it must be updated after the GUID has been successfully gotten with GetCurrentHwProfile

blah blah blah...
    if(GetCurrentHwProfile(&hwProfileInfo) != NULL)
    { 
        hwid = hwProfileInfo.szHwProfileGuid;
        std::ofstream hwidfile { "hardwareid2.txt" };
...blah blah blah
  •  Tags:  
  • c
  • Related