Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

weird ofstream issue (stops outputting) c++


  • Please log in to reply
3 replies to this topic

#1 fearmyawesome

fearmyawesome

  • Members
  • 15 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Canada
  • Local time:10:52 AM

Posted 26 March 2010 - 04:04 PM

Hey there,

I am experiencing a really weird issue when using the ofstream to output text to a text file.

So I opened a text file with:
ofstream myfile;
myfile.open("log.txt");

Which works fine, and I can write to it to, in the first loop I create.

myfile << "----------------------------------------Run---------------------------------------";
myfile << "\n";
myfile << "Name									 Location of Executable \n";
myfile << "\n";
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_QUERY_VALUE, &phkResult) == ERROR_SUCCESS)
{
do
{
valuenamesize = 64;
valuedatasize = 64;
enumresult = RegEnumValue(phkResult, index, valuename, &valuenamesize, 0,
NULL,
valuedata,
&valuedatasize); 
myfile << setw(30)<< setiosflags(ios::left) << valuename << "		   " << valuedata << endl;
index ++;
} while (enumresult != ERROR_NO_MORE_ITEMS);

However, as soon as I exit that do while loop, ofstream seems to lose touch with the file. Right after that loop I have the following:

RegCloseKey(phkResult);
myfile << "\n\n";
myfile << "------------------------------------Installed Programs---------------------------------\n";
}
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"), 0, KEY_QUERY_VALUE, &phkResult) == ERROR_SUCCESS)
{
do
{
valuenamesize = 64;
valuedatasize = 64;
enumresult = RegEnumValue(phkResult, index, valuename, &valuenamesize, 0,
NULL,
valuedata,
&valuedatasize); 
myfile << setw(30)<< setiosflags(ios::left) << valuename;
index ++;
} while (enumresult != ERROR_NO_MORE_ITEMS);
RegCloseKey(phkResult);
}

Despite the next output not even being in a loop or if statement (no conditions to met, so no reason to not output) Nothing is printed after that loop to my file. Not even the installed programs line. :S

I'm a little bit confused. Can anyone spot what I might be doing wrong?

Thanks!

BC AdBot (Login to Remove)

 


#2 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:09:52 AM

Posted 27 March 2010 - 12:20 AM

I cannot say anything because you have not posted your full code in one place. I also see some errors in your usage of the Win32 Registry API.

BTW why are you using a buffer of 64 chars ? any special reason ?
The second parameter in RegEnumValue should be set to 0 before starting the enumeration.

#3 fearmyawesome

fearmyawesome
  • Topic Starter

  • Members
  • 15 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Canada
  • Local time:10:52 AM

Posted 28 March 2010 - 09:16 PM

Actually Yeah, I suppose there isn't really any reason I'm use that size of a buffer.
I'm kind of new to programming in C++, so I think I just chose it to be on the safe side. The size may be a little excessive, but I might just bring down the size a bit before I finish.

Somehow the problem kind of fixed itself. I didn't do anything to the code really, but strangely enough it started to work after just compiling and make slight adjustments.

And Ill make sure I set that index to 0!

Thanks for taking the time to help me out!

#4 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:09:52 AM

Posted 29 March 2010 - 12:15 PM

Actually Yeah, I suppose there isn't really any reason I'm use that size of a buffer.
I'm kind of new to programming in C++, so I think I just chose it to be on the safe side. The size may be a little excessive, but I might just bring down the size a bit before I finish.


No, I meant that 64 is small. Some values in the Registry are larger than that. It should be set higher like 256 :thumbsup:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users