我在获取 .txt 文件文本中修饰符后的输入时遇到问题。 我想要做的是,如果 .txt 文件有单词“type:”,那么之后的任何内容都将被放入一个 char 中。 到目前为止我的代码:
#include "stdafx.h"
#include <windows.h>
#include "VKH.h"
#include "Strmif.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
void GetDocumentandRead() {
string line;
ifstream myfile (line1);
if (myfile.is_open())
{
while ( !myfile.eof() )
{
getline (myfile,line);
char aline[100];
strcpy(aline, line.c_str());
printf(aline, "\n");
if (line.compare("mouseup") == 0){
MouseUp(10);
}
if (line.compare("mousedown") == 0){
MouseDown(10);
}
if (line.compare("mouseright") == 0){
MouseRight(10);
}
if (line.compare("mouseleft") == 0){
MouseLeft(10);
}
if (line.compare("mouseclick") == 0){
MouseClick();
}
if (line.compare("enter") == 0){
Enter();
}
if (line.compare("ctrltab") == 0){
CtrlTab();
}
if (line.compare("tab") == 0){
Tab();
}
if (line.compare("altf4") == 0){
AltF4(0);
}
if (line.compare("alttab") == 0){
AltTab();
}
if (line.compare("mousecenter") == 0){
MouseCenter();
}
if (line.compare(6,5,"type:") == 0){
//Don't know what to put here...
}
}
myfile.close();
}
else printf("\nUnable to open file\n\n");
}
因此,在文本文件中的“type:”之后,它将使用我称为 TypeStr() 的函数键入该内容;
void TypeStr(char *lpszString)
{
char cChar;
while((cChar=*lpszString++)) // loops through chars
{
short vk=VkKeyScan(cChar); // keycode of char
if((vk>>8)&1){keybd_event(VK_LSHIFT,0,0,0);} // hold shift if necessary
keybd_event((unsigned char)vk,0,0,0); // key in
keybd_event((unsigned char)vk,0,KEYEVENTF_KEYUP,0); // key out
if((vk>>8)&1){keybd_event(VK_LSHIFT,0,KEYEVENTF_KEYUP,0);} // release shift if necessary
}
}
如有任何帮助,我们将不胜感激。谢谢!
最佳答案
首先你应该重写你的TypeStr
函数,因此它需要一个 const char *
.像这样
void TypeStr(const char *lpszString)
{
...
}
无需其他更改。
然后你应该像这样从你的代码中调用那个函数
if (line.compare(6,5,"type:") == 0){
TypeStr(line.c_str() + 11);
}
您必须更改 TypeStr
的原因功能const char*
(除了一般的好风格之外)是 c_str()
std::string
的方法|返回 const char*
不是char*
.
关于c++ - 从文本文件输入获取char C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13329288/