|
i'm writing a simple program using Ansi C++. I'm not using Microsoft C++. that doesnot applies to me. what's your suggestion?
|
|
|
|
|
You cannot do that.
You're trying to serialize an object (see "Serialization" topic at Wikipedia [^]) in a wrong way.
Since std::string is not a scalar type, the object instance (objdata ) holds just a reference to it (and you're saving just such a reference, i.e. storing an address to disk, clearly it makes no sense).
So you cannot save the class as a whole using
file.write((char*)&objdata,sizeof(Data));
instead you have to store each of its members in order to restore appropriately them, for instance:
file.write((char*)&(objdata.id),sizeof(objdata.id));
string::size_type len = objdata.name.length();
file.write((char*) &len, sizeof(len));
file.write((char*)(objdata.name.c_str(), len);
Can you spot the code for reading back the class?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
so it was only the issue of std::string class, thanks i succeed with the following code.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class Data
{
public:
char name[20];
int id;
public:
Data(){}
Data(char *n,int id){strcpy(name,n);this->id=id;}
};
int main() {
Data objdata;
fstream file("c:\\test.txt",ios::in | ios::binary);
if(file.is_open())
{
file.read((char*)&objdata,sizeof(Data));
cout<<objdata.name<<objdata.id;
cout<<"Success";
file.close();
}
else
{
cout<<"Error";
}
return 0;
}
|
|
|
|
|
Hi All,
I need to pass data from my program to an active notepad window. I got the handle to notepad and copied the data to clipboard but nothing is getting pasted in notepad window after sending WM_PASTE message to notepad window. Here is the code
if(!OpenClipboard(hWnd))
errorExit();
HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, extractedTime.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(extractedTime));
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
if(notepadWnd != NULL)
{
SendMessage(notepadWnd, WM_PASTE, 0, 0);
}
CloseClipboard();
I used this article as reference. Any pointers what am I doing wrong?
Thanks
|
|
|
|
|
I don't know the answer to your question, but I can break it into two parts:
1/ Am I putting my text successfully on the clipboard...
2/ Is notepad getting my WM_PASTE message?
You can check (1) yourself... Just run notepad, and use the Edit | Paste menu. If that doesn't work, then your clipboard code is wrong.
If you can manually paste into notepad, then maybe notepad does not respond to WM_PASTE, or maybe you are sending the message to the wrong window.
Using Spy++, I get three windows making up notepad - the app window, a child Edit window, and a child status bar.
Maybe you are sending WM_PASTE to the app window, not the child edit one?
Another thing to consider... Notepad could be different on different OS's...
Good luck,
Iain.
I have now moved to Sweden for love (awwww).
|
|
|
|
|
lazy_panther wrote: Any pointers what am I doing wrong?
Sure; you're assuming way too much. For instance, what makes you think that NotePad's mainframe is going to respond to a WM_PASTE message in just the way you want?
Why do you need to paste text to an open NotePad window, anyway? If you need to gather textual input, show a dialog with a text window and collect it that way.
And don't use the clipboard to pass data internally in your program. It's very bad style; suppose, for instance, the user had text already on the clipboard that he'd copied from a browser window with the intention of pasting it later into a Word document. Don't you think that user is going to be a little irritated to find the text he'd placed there is overwritten with something your program did internally, without user consent?
L u n a t i c F r i n g e
|
|
|
|
|
And don't use the clipboard to pass data internally in your program. It's very bad style; suppose, for instance, the user had text already on the clipboard that he'd copied from a browser window with the intention of pasting it later into a Word document. Don't you think that user is going to be a little irritated to find the text he'd placed there is overwritten with something your program did internally, without user consent?
In my application, we can assume that user won't be doing Copy-Paste.
Let me briefly describe my applcation
My program will be running in background with a icon residing in notification area. Based on user signal, I need to process some information without prompting user for it and paste it in word document. The information required has already been pre-processed. User should press a short cut key and pre-processed information should get pasted at the current cursor position of MS word.
I tried sending WM_PASTE to notepad first because it seemed much simpler than operating with MS word.
Can you please suggest some alternate routes for pasting information to word document from win32 program?
Really appreciate your help.
|
|
|
|
|
Word can be automated; that is, it can be programatically started, it's document contents manipulated, etc. Data could be inserted without resorting to clipboard functions.
I've done this sort of thing from C# before, but not C++. That being said, if you really want to do it from C++, it should be possible. You can either use managed C++ and .NET classes to interact with Word, or I think it can also be done from native C++ via COM. (You'll have to check on that.) The COM thing is probably moot anyway; managed C++ would be a lot easier, although it would involve .NET dependencies...
Not a very definitive answer - hopefully someone else can give more direction.
L u n a t i c F r i n g e
|
|
|
|
|
|
That sounds like just what you're looking for.
L u n a t i c F r i n g e
|
|
|
|
|
#include <iostream><br />
#include <fstream><br />
using namespace std;<br />
<br />
int main() {<br />
<br />
char data[5];
fstream file("c:\\test.txt",ios::in | ios::binary);<br />
if(file.is_open())<br />
{<br />
while(!file.eof())<br />
{<br />
file.read(data,sizeof(data));<br />
cout<<data;<br />
}<br />
file.close();<br />
}<br />
else<br />
{<br />
cout<<"Error";<br />
}<br />
<br />
return 0;<br />
}
I've written "abcf" to text.txt file in binary mode. i'm reading back the data into the variable "data". why the heck the output is "abcfabcf". I should be "abcf" but what happened to this one. and another question do anybody knows how to write and read string data-type in binary mode. please help.
|
|
|
|
|
This works for me (error handling left to the reader)
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream file("C:\\test.txt", ios::out | ios::binary);
char out[]="hello";
file.write(out, sizeof(out));
file.close();
file.open("C:\\test.txt", ios::in | ios::binary);
char in[sizeof(out)];
file.read(in, sizeof(in));
cout << out << ", " << in << endl;
file.close();
return 0;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
What's wrong with just using std::string and using the library provided operators, like here?
fstream file("c:\\test.txt",ios::in | ios::binary);
if(file.is_open())
{
std::string s;
file >> s;
std::cout << s << std::endl;
file.close();
}
else
{
cout<<"Error";
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
modified on Wednesday, February 3, 2010 8:27 AM
|
|
|
|
|
The format...
On the serious side, while there is, of course, nothing wrong in your approach, there is, as well, nothing wrong in his one.
Disclaimer: this is going on my...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I always like using idiomatic C++ rather than not
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
fstream::eof() - returns true if the 'eofbit' has been set by a previous i/o operation.
fstream::read(s, n) - sets 'eofbit' if the End-of-File is reached before n characters have been read.
The file has only 5 characters and the while loop will execute twice. In the first pass you try to read 5 bytes and it will be success. In the second pass read will fail and sets the 'eofbit'. Since you are not checking how many bytes read, you cout the old buffer contents again.
This is the reason why you got "abcfabcf".
The code can be changed to print "abcf" once, by checking the bytes read using fstream::gcount() as
.................
if(file.is_open())
{
//file.write(data,sizeof(data));
while(!file.eof())
{
file.read(data,sizeof(data));
if(file.gcount())
cout<<data;
}
file.close();
}
..................
|
|
|
|
|
Hai,
Can Anyone tell me how to create a setup project.I want to add an licensing page also.
Thanks
Jannath Ali
|
|
|
|
|
|
I want to add a Licencing page in the deployment.this article does not explain about adding the licensce page
|
|
|
|
|
If you have already created the Setup and Deployment project, then you can take Solution Explorer (View->Solution Explorer).
You can find few small icons on the top of Solution Explorer window. One of those icons is 'User Interface Editor' (Fifth icon from the left in VS 2005).
Click on that icon to open the User Interface Editor. Now you can see a Tree of dialogs on the right side pane.
Now right click on the 'Start' node to see a popup menu with 'Add Dialog' option. 'Add Dialog' brings you a list of dialog (including relicense page)
|
|
|
|
|
I have changed the caption of third party exe through FindWindow and SetWindowtext but this application changes its caption on Atl+Ctrl, How can I set my own caption instead of pressing atl+ctrl.
|
|
|
|
|
If the program changes its own caption internally using SetWindowText() then there is nothing you can do directly. However, I believe there is a function that allows you to set a hook process which can monitor all messages, so you could intercept the WM_SETTEXT and act appropriately. But it sounds like a lot of hard work for no discernible benefit.
MVP 2010 - are they mad?
|
|
|
|
|
You may hack the executable file.
Of course, I never told you about
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
And the third possibility (after API hooking and editing the executable in a hex editor) would be:
Contact the author and ask for it, for example with a special build or a new runtime setting. If the application is open source you could even do it yourself, otherwise check what the author/vendor thinks about it. Some are happy to help, especially when you can explain why this feature would improve the end user experience for (one of) their customers.
/M
|
|
|
|
|
In VS2008 neither /IGNORE nor #pragma warning(disable:4099) work for that message.
I'd like to compile debug build and I'm using a lot of libs they garbage the output window too much with those 4099 warning
Чесноков
|
|
|
|