0

Windows ソケットを使用していくつかのファイルを FTP サーバーにアップロードしています。特定のキーワードを持つファイルを検索し、ファイルの名前にそのキーワードが含まれている場合はサーバーに送信します。コードは次のようになります。

void FTPUpload(const char* path, const char* name)
{
    SOCKET s1 = ConnectFTP("111.111.111.111", 21);
    Receive(s1);
    Send(s1, "USER user", true);
    Receive(s1);
    Send(s1, "PASS password", true);
    Receive(s1);
    Send(s1,"PWD", true);
    Receive(s1);
    Send(s1,"TYPE I", true);
    Receive(s1);
    Send(s1, "PASV", true);
    char szString[1000];
    Receive(s1, szString);
    char szIP[40];
    char* start = strchr(szString, '(' );
    char* end   = strchr(szString, ')' );
    int num = end - start;
    char str[30] = {'\0'};
    strncpy( str, start + 1, num - 1 );
    char* token = strtok( str , "," );
    strcpy(szIP, "");
    strcat(szIP, token);
    strcat(szIP, ".");
    token = strtok( NULL, "," );
    strcat(szIP, token);
    strcat(szIP, ".");
    token = strtok( NULL, "," );
    strcat(szIP, token);
    strcat(szIP, ".");
    token = strtok( NULL, "," );
    strcat(szIP, token);
    token = strtok( NULL, "," );
    int intA = atoi(token);
    token = strtok( NULL, "," );
    int intB = atoi(token);
    int port = (intA * 256) + intB;
    sprintf(buf, "IP %s, Port %d", szIP, port);

//connect to FTP Server Data Connection
SOCKET s2 = ConnectFTP(szIP, port);
string command = "STOR " + string(name);
Send(s1, &command[0], true);
Receive(s1);

//Send the found file
long Begin;
long End;
char * block;
ifstream myfile;
myfile.open(path, ios::in | ios::binary);
Begin = myfile.tellg();
myfile.seekg(0,ios::end);
End = myfile.tellg();
unsigned long size = End - Begin;
int Div = (int)size / 1024;
int Mod = (int)size % 1024;
for (int i=0; i<Div; i++)
{
    block = new char[1024];
    myfile.seekg(i*1024);
    myfile.get(block,1024+1);
    Send(s2,block, false);
}
if (Mod != 0)
{
    block = new char[Div];
    myfile.seekg(Div*1024);
    myfile.get(block,Mod+1);
    Send(s2,block, false);
}
closesocket(s2);
myfile.close();
Receive(s1);
Send(s1, "QUIT", true);
Receive(s1);
closesocket(s1);
}

void FindFile()
{
    string keyword = "key";
    string path = "C:\\";
    string ipath = path+"*";
    WIN32_FIND_DATA file;
    HANDLE search_handle = FindFirstFile(ipath.c_str(),&file);
    if (search_handle != INVALID_HANDLE_VALUE)
    {
        do
        {
            string cpath = path + file.cFileName;
            DWORD attr = GetFileAttributes(cpath.c_str());
            if ((attr & (0x10)) != (0x10))
            {
                if ((cpath.find(keyword)) != (string::npos))
                    FTPUpload(cpath.c_str(), file.cFileName);
            }
        }
        while(FindNextFile(search_handle,&file));
        FindClose(search_handle);
    }
}

キーワードに一致するディレクトリに 2 つのファイルがある場合、プログラムは正常に動作します。3 番目のファイルで、dbgheap.c で bad_alloc の例外が発生しています。つまり、ヒープからのメモリの割り当てと割り当て解除に問題があります。親切に助けてください。

4

1 に答える 1

0

これは間違っています

block = new char[1024];
myfile.seekg(i*1024);
myfile.get(block,1024+1);

ブロックは 1024 バイトですが、get は 1024 文字を読み取り、'\0' 文字を追加します。おそらく、readnotを使用するつもりでしgetた。

これは間違っています

block = new char[Div];
myfile.seekg(Div*1024);
myfile.get(block,Mod+1);

ブロックは Div バイトですが、Mod バイトを読み取ります。

この問題には役立たなかったかもしれませんが、配列を割り当てる習慣から抜け出すようにしてください。std::stringandを使用するとstd::vector、より安全で簡単なオプションになります。例えば

myfile.seekg(i*1024);
vector<char> block(1024);
myfile.read(&block[0],block.size());
于 2013-03-23T17:18:52.543 に答える