Ftp上传和文件阅读

时间:2015-07-26 05:46:01

标签: c++ file file-upload

我的程序正在运行,但它保留了x的旧命令值和y的旧ID值,并继续执行'command 1'。我正在尝试让程序继续检查另一个'commands.txt',存储x / y,比较值,最后再循环。出于某种原因,一旦我通过上传commands.txt文件来提供程序,它就会保留这些值。我不能让它停止这样做。

这是我的代码。请记住,我对C ++很陌生。

#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <lmcons.h>
#include <ctime>
#include <wininet.h>
#include <stdio.h>

#pragma comment(lib, "wininet.lib")

using namespace std;

int startup(string IDpath){

    ifstream file;
    file.open(IDpath);
    bool TF = file.is_open();

    if (TF == FALSE){ // checks if the ID file exists
        int RN;       // creates a random ID 
        srand(time(0));
        RN = rand() % 1000;

        ofstream create; // stores ID in a file
        create.open(IDpath);
        create << RN << endl;
        create.close();
    }

    int x;       // passes ID to main         
    ifstream wID;
    wID.open(IDpath);
    wID >> x;
    wID.close();
    return x;

}

int check (int w, int z, int cID){

    if (w = 1 && z == cID){
        cout << "command 1 executed!" << w << z <<endl;
        return 0;


    }
    else
        return 0;


}

int main(){

    int x;
    int y;
    int ID;

    TCHAR username[UNLEN + 1]; // gets username
    DWORD size = UNLEN + 1;
    GetUserName((TCHAR*)username, &size);
    string name = username; // stores username

    string path = "C://Users//"; // creates universal path for ID
    string path2 = "//Downloads//ID.txt";
    string finalpathID = path + name + path2;

    string pathRC = "C://Users//"; // creates universal path for reading the commands
    string path2RC = "//Downloads//readcommands.txt";
    string finalpathRC = pathRC + name + path2RC;
    LPCSTR FPRC = finalpathRC.c_str();

    startup(finalpathID); // passes the ID path
    ID = startup(finalpathID); // stores ID value in 'ID'

    while (true){


        HINTERNET hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); // internet handle
        HINTERNET hFtpSession = InternetConnect(hInternet, "ftp.YOURHOST.com", INTERNET_DEFAULT_FTP_PORT, "PASSWORD", "USERNAME", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0); // ftp handle
        FtpSetCurrentDirectory(hFtpSession, "htdocs"); // sets ftp directory


        FtpGetFile(hFtpSession, "commands.txt", FPRC, FALSE, INTERNET_FLAG_RELOAD, FTP_TRANSFER_TYPE_BINARY, 0);
        FtpDeleteFile(hFtpSession, "commands.txt");

        ifstream file;
        file.open(finalpathRC);
        file >> x >> y; // takes in the command and ID value
        file.close();

        InternetCloseHandle(hFtpSession); // closes ftp handle
        InternetCloseHandle(hInternet); // closes internet handle 

        if (check(x, y, ID) == 0){

            remove(FPRC);
            continue;


        }


    }
    return 0;

}

1 个答案:

答案 0 :(得分:0)

该程序似乎下载commands.txt然后尝试删除它。它不会检查下载或删除的返回代码,因此您无法确定它是否已完成。文件IO也未选中,因此无法保证文件已成功读取。

程序可能已下载但未能删除,因此继续下载和处理同一文件。

或者它可能已删除该文件,后续下载未经检查失败。程序无法打开文件,无法读取x和y并且似乎正在读取同一文件。

解决方案:检查错误代码并相应地修改程序行为。