一道c++编程题,请将代码写完整?谢谢……!

xchfriend 2003-09-14 11:08:35
请用c++编程:

将键盘上敲打的字符存放到文件中去(该文件为C:\temp.txt)!
...全文
84 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
xchfriend 2003-09-14
  • 打赏
  • 举报
回复
我就是只要用c++编就行了!
williamVII 2003-09-14
  • 打赏
  • 举报
回复
nod
TinyAnt 2003-09-14
  • 打赏
  • 举报
回复
不用搞得这么复杂,这道题出的意图就是让你熟悉 iostream 的用法。
Aofa_lee 2003-09-14
  • 打赏
  • 举报
回复
WINDOWS 的就是这么复杂的,但光用C 或 C++就没有这么复杂了!
xchfriend 2003-09-14
  • 打赏
  • 举报
回复
对,我就是要用文件操作函数,不用怎么复杂的!
chenkuizhong 2003-09-14
  • 打赏
  • 举报
回复
我觉得好像不用这么复杂
用点文件操作函数就行了
比如说fwrite之类的
fireseed 2003-09-14
  • 打赏
  • 举报
回复

lpfn
[in] Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a dynamic-link library (DLL). Otherwise, lpfn can point to a hook procedure in the code associated with the current process.
hMod
[in] Handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.
dwThreadId
[in] Specifies the identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.
Return Values
If the function succeeds, the return value is the handle to the hook procedure.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks
An error may occur if the hMod parameter is NULL and the dwThreadId parameter is zero or specifies the identifier of a thread created by another process.

Calling the CallNextHookEx function to chain to the next hook procedure is optional, but it is highly recommended; otherwise, other applications that have installed hooks will not receive hook notifications and may behave incorrectly as a result. You should call CallNextHookEx unless you absolutely need to prevent the notification from being seen by other applications.

Before terminating, an application must call the UnhookWindowsHookEx function to free system resources associated with the hook.

The scope of a hook depends on the hook type. Some hooks can be set only with global scope; others can also be set for only a specific thread, as shown in the following table.

Hook Scope
WH_CALLWNDPROC Thread or global
WH_CALLWNDPROCRET Thread or global
WH_CBT Thread or global
WH_DEBUG Thread or global
WH_FOREGROUNDIDLE Thread or global
WH_GETMESSAGE Thread or global
WH_JOURNALPLAYBACK Global only
WH_JOURNALRECORD Global only
WH_KEYBOARD Thread or global
WH_KEYBOARD_LL Global only
WH_MOUSE Thread or global
WH_MOUSE_LL Global only
WH_MSGFILTER Thread or global
WH_SHELL Thread or global
WH_SYSMSGFILTER Global only


For a specified hook type, thread hooks are called first, then global hooks.

The global hooks are a shared resource, and installing one affects all applications in the same desktop as the calling thread. All global hook functions must be in libraries. Global hooks should be restricted to special-purpose applications or to use as a development aid during application debugging. Libraries that no longer need a hook should remove its hook procedure.

Windows 95/98/Me: SetWindowsHookExW is supported by the Microsoft Layer for Unicode. However, it does not make conversions. To see Unicode messages, notifications, and so forth, you must subclass the window. To use this version of the API, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Example Code
For an example, see Installing and Releasing Hook Procedures.

Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.

See Also
Hooks Overview, Hook Functions, CallNextHookEx, CallWndProc, CallWndRetProc, CBTProc, DebugProc, ForegroundIdleProc, GetMsgProc, JournalPlaybackProc, JournalRecordProc, LowLevelKeyboardProc, LowLevelMouseProc KeyboardProc, MouseProc, MessageProc, ShellProc, SysMsgProc, UnhookWindowsHookEx

Platform SDK Release: August 2001 What did you think of this topic?
Let us know. Order a Platform SDK CD Online
(U.S/Canada) (International)



Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.
See Also
Hooks Overview, Hook Functions, CallNextHookEx, CallWndProc, CallWndRetProc, CBTProc, DebugProc, ForegroundIdleProc, GetMsgProc, JournalPlaybackProc, JournalRecordProc, LowLevelKeyboardProc, LowLevelMouseProc KeyboardProc, MouseProc, MessageProc, ShellProc, SysMsgProc, UnhookWindowsHookEx
fireseed 2003-09-14
  • 打赏
  • 举报
回复
你可以使用键盘钩子:


Platform SDK: Windows User Interface
SetWindowsHookEx
The SetWindowsHookEx function installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread.

HHOOK SetWindowsHookEx(
int idHook, // hook type
HOOKPROC lpfn, // hook procedure
HINSTANCE hMod, // handle to application instance
DWORD dwThreadId // thread identifier
);
Parameters
idHook
[in] Specifies the type of hook procedure to be installed. This parameter can be one of the following values. Value Description
WH_CALLWNDPROC Installs a hook procedure that monitors messages before the system sends them to the destination window procedure. For more information, see the CallWndProc hook procedure.
WH_CALLWNDPROCRET Installs a hook procedure that monitors messages after they have been processed by the destination window procedure. For more information, see the CallWndRetProc hook procedure.
WH_CBT Installs a hook procedure that receives notifications useful to a computer-based training (CBT) application. For more information, see the CBTProc hook procedure.
WH_DEBUG Installs a hook procedure useful for debugging other hook procedures. For more information, see the DebugProc hook procedure.
WH_FOREGROUNDIDLE Installs a hook procedure that will be called when the application's foreground thread is about to become idle. This hook is useful for performing low priority tasks during idle time. For more information, see the ForegroundIdleProc hook procedure.
WH_GETMESSAGE Installs a hook procedure that monitors messages posted to a message queue. For more information, see the GetMsgProc hook procedure.
WH_JOURNALPLAYBACK Installs a hook procedure that posts messages previously recorded by a WH_JOURNALRECORD hook procedure. For more information, see the JournalPlaybackProc hook procedure.
WH_JOURNALRECORD Installs a hook procedure that records input messages posted to the system message queue. This hook is useful for recording macros. For more information, see the JournalRecordProc hook procedure.
WH_KEYBOARD Installs a hook procedure that monitors keystroke messages. For more information, see the KeyboardProc hook procedure.
WH_KEYBOARD_LL Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard input events. For more information, see the LowLevelKeyboardProc hook procedure.
WH_MOUSE Installs a hook procedure that monitors mouse messages. For more information, see the MouseProc hook procedure.
WH_MOUSE_LL Windows NT/2000/XP: Installs a hook procedure that monitors low-level mouse input events. For more information, see the LowLevelMouseProc hook procedure.
WH_MSGFILTER Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar. For more information, see the MessageProc hook procedure.
WH_SHELL Installs a hook procedure that receives notifications useful to shell applications. For more information, see the ShellProc hook procedure.
WH_SYSMSGFILTER Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar. The hook procedure monitors these messages for all applications in the same desktop as the calling thread. For more information, see the SysMsgProc hook procedure.

xchfriend 2003-09-14
  • 打赏
  • 举报
回复
顺便问一下,我怎么在c++builder中,验证这个程序?

我刚才试了一下,程序运行后,根本不给我从键盘上输入的界面!
Wolf0403 2003-09-14
  • 打赏
  • 举报
回复
呵呵,奶油狗大哥的代码是纯 C++ 的,放心使用。。。
另外,所谓“C++” 和 “vc.net”到底是什么关系??
呵呵
xchfriend 2003-09-14
  • 打赏
  • 举报
回复
我要c++编的,不要vc.net,请重新写一个吧,最好有注释。
货到付款!
fireseed 2003-09-14
  • 打赏
  • 举报
回复
来个更简单的

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

void main()
{
ofstream fsFile( "c:\\test.txt" );
streambuf *pOldBuf = cout.rdbuf( fsFile.rdbuf() );
string strInput;
getline( cin, strInput, '\n' );
cout << strInput.c_str();
fsFile.close();
cout.rdbuf( pOldBuf );
system( "pause" );
}
flymj 2003-09-14
  • 打赏
  • 举报
回复
原来指定位置用的是\\啊。我刚才用了//,怪不得找不到文件了。受教。
fireseed 2003-09-14
  • 打赏
  • 举报
回复
以为楼主要做一个键盘间谍,搞错了

:)

/*以下代码在VC.net环境下编译通过并正常运行*/
/*Creamdog保留所有权力 */
#include <iostream>
#include <fstream>
using namespace std;

void main()
{
ofstream file( "c:\\test.txt" );
cout.rdbuf( file.rdbuf() );
char a;
do
{
cin.get(a);
cout << a;
}
while ( a != '\n' );
system( "pause" );
}
flymj 2003-09-14
  • 打赏
  • 举报
回复
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main()
{
ofstream hp("temp.txt");//如何设绝对路径,我也不太知道,请后来人指示。
if(!hp)
{
cerr<<"the file can not open! please check it again!"<<endl;
return;
}
else
{
char x;
cin>>x;
while(x!='0') //输入0即表示结束
{
hp<<x;
cin>>x;
}
cout<<"the file is correctly edited!"<<endl;
}
}

64,633

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧