tags:

views:

39

answers:

3

hi, my code wont work and idk why. the point of my code is to find the top window and save a text file with the name the same as the text on the top menu bar (task bar i think?). then save some data to that text file. but everytime i try to use it the write fails if i set the name of the text file before hand so it wont change it will write the data to the file. but if i don't set it before hand it will make the text doc but not write anything to it. or sometimes it will just write numbers for the name (i think it's the handle number) then it will write the data. :\ it's odd can anyone help?

 #include <iostream>
 #include <windows.h>
 #include <fstream>
 #include <string>
 #include <sstream>
 #include <time.h>

 using namespace std;

 string header_str = ("NULL");

 #define DTTMFMT "%Y-%m-%d %H:%M:%S " 
 #define DTTMSZ 21 

 char buff[DTTMSZ]; 
 fstream filestr; 

 string ff = ("C:\\System logs\\txst.txt");
 TCHAR buf[255];


 int main()
{
            GetWindowText(GetForegroundWindow(), buf, 255);

            stringstream header(stringstream::in |                                    stringstream::out); 
            header.flush();

            header << ("C:\\System logs\\");
            header << buf;
            header << (".txt");

            header_str = header.str();

            ff = header_str;

            cout << header_str << "\n";

            filestr.open (ff.c_str(), fstream::in | fstream::out |                                   fstream::app | ios_base::binary | ios_base::out);
            filestr << "dfg";
            filestr.close();

            Sleep(10000);

            return 0;
  }
+1  A: 

You are not sanitizing the name of your text file. There are quite a few illegal file names. Primarily, characters such as ":", "/" and "\" are not allowed in a filename.

Yann Ramin
A: 

You are probably compiling your application in unicode mode. So buf is actually wchar_t[255], and when you execute header<<buf, you actually output the pointer value of buf instead of its contents since header is a non-unicode string stream and doesn't know how to write unicode buffers.

You can either compile your app as non-unicode (In the general project settings, set character set to multi-byte), or use the unicode alternatives to stringstream and fstream (wstringstream, wfstream).

That's the main cause of the problem. In addition, theatrus is right about needing to remove illegal characters from the filename, and you should also check if GetForegroundWindow returned NULL or if GetWindowText returned an empty string.

interjay
A: 

Your buffer containing the window title is of type TCHAR. In Visual C++ TCHAR expands to type wchar_t when the _UNICODE preprocessor macro is defined (I believe this is the default setting). You then use the non-wide version of stringstream, which when will convert a wchar_t* to a string representation of the pointer value rather than the string contents of the buffer.

You have 2 options:

  1. Change your settings to not use the "Unicode Character Set". (Look under General Project Settings).
  2. Use wstringstream and wfstream instead of stringstream and fstream. These will work with wchar_t.
zdan
im using multi-byte chars now but some windows still fail:\ but some it will save the data to but some it wont
blood