[英]Input from a text file to get a char C++
我在.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*
(除了它是一般的好樣式)的原因是std::string
的c_str()
方法返回一個const char*
而不是char*
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.