2016-11-09 76 views
0

是否可以在服务器/网页上运行EXE(C++)并使EXE返回一个响应,还是必须将此EXE(C++)转换为JavaScript的?在html中执行.exe(C++)或转换为javascript

如果我必须将代码转换为JavaScript,我该怎么做?我知道javascript的基础知识,但我不知道如何读写.txt文件。

基本上我的代码是我想用于我的网站的登录系统,所以我不必使用mysql或数据库。我知道这样做很慢,可能是一种愚蠢的做法,但这是一个项目。任何帮助表示赞赏!

编辑:您按登录按钮,然后我提示您使用EXE文件,如上所见。然后,如果登录成功并且信息,则EXE文件将发送回服务器。 EXE文件必须在服务器上,所以客户端不必下载它。

代码:

#include <iostream> 
#include <string> 
#include <fstream> 
#include <time.h> 

using namespace std; 
void cls() { system("cls"); } 

bool mainPage(string &username, string &password) { 
    cls(); 
    cout << "Username: "; getline(cin, username); 
    cout << "Password: "; getline(cin, password); 
    if(username != "" && password != "") { 
     return true; 
    } 
    else { 
     return false; 
    } 
} 

bool checkLogin(string &username, string &password, string &email, string &firstName, string &lastName) { 
    string pw = ""; 

    ifstream ifile(username + ".txt"); 
    if(ifile.is_open()) { 
     ifile >> pw >> email >> firstName >> lastName; 
     if(pw == password) { 
      return true; 
     } 
     else { 
      return false; 
     } 
    } 
    else { 
     return false; 
    } 
} 

bool loginFail() { 
    string fail = ""; 
    string undef = ""; 

    cls(); 
    cout << "'reg'" << endl; 
    cout << "'main'" << endl; 
    getline(cin, fail); 
    if(fail == "reg") { 
     return true; 
    } 
    else if(fail == "main") { 
     return false; 
    } 
    else { 
     cout << "\nCould not understand you." << endl; 
     getline(cin, undef); 
     return loginFail(); 
    } 
} 

bool regist() { 
    string un; 
    string pw; 
    string em; 
    string fn; 
    string ln; 
    string undef = ""; 

    cls(); 
    cout << "'main'" << endl; 
    cout << "\nNew username: "; getline(cin, un); 
    cout << "New password: "; getline(cin, pw); 
    if((un != "" && pw != "") && (un != "main" && pw != "main")) { 
     ifstream ifile(un + ".txt"); 
     if (ifile.is_open()) { 
      cout << "\nThis username is already taken." << endl; 
      getline(cin, undef); 
      return regist(); 
     } 
     else { 
      cout << "\nEmail: "; getline(cin, em); 
      cout << "\nFirst name: "; getline(cin, fn); 
      cout << "Last name: "; getline(cin, ln); 
      if(em != "" && fn != "" && ln != "") { 
       ofstream ofile; 
       ofile.open(un + ".txt"); 
       if (ofile.is_open()) { 
        ofile << pw << endl; 
        ofile << em << endl; 
        ofile << fn << endl; 
        ofile << ln << endl; 
        ofile.close(); 
        cout << "\nFile was successfully written to." << endl; 
        getline(cin, undef); 
        return true; 
       } 
       else { 
        cout << "\nFile could not open." << endl; 
        getline(cin, undef); 
        return regist(); 
       } 
      } 
      else { 
       cout << "\nYou didn't enter one of the above." << endl; 
       getline(cin, undef); 
       return regist(); 
      } 
     } 
    } 
    else if(un == "main" || pw == "main") { 
     return false; 
    } 
    else { 
     cout << "\nYou didn't do it correct." << endl; 
     getline(cin, undef); 
     return regist(); 
    } 
} 

void loginSuccess(string username, string password, string email, string firstName, string lastName) { 
    string undef = ""; 

    cls(); 
    cout << "You have successfully logged in." << endl; 
    cout << "\nUsername: " << username << endl; 
    cout << "Password: " << password << endl; 
    cout << "\nEmail: " << email << endl; 
    cout << "\nFrist name: " << firstName << endl; 
    cout << "Last name: " << lastName << endl; 
    getline(cin, undef); 
} 

int main(void) { 
    system("color a"); 
    string username = ""; 
    string password = ""; 
    string email = ""; 
    string firstName = ""; 
    string lastName = ""; 

    if(!mainPage(username, password)) { 
     return main(); 
    } 
    else { 
     if(!checkLogin(username, password, email, firstName, lastName)) { 
      if(!loginFail()) { 
       return main(); 
      } 
      else { 
       if (!regist()) { 
        return main(); 
       } 
       else { 
        return main(); 
       } 
      } 
     } 
     else { 
      loginSuccess(username, password, email, firstName, lastName); 
     } 
    } 
} 
+1

Google“CGI C++”初学者页面:https://www.tutorialspoint.com/cplusplus/cpp_web_programming.htm –

+0

但我想这样。你按登录按钮,然后你会看到上面提到的EXE文件。然后,如果登录成功并且信息,则EXE文件将发送回服务器。 EXE文件必须在服务器上,所以客户端不必下载它。 –

+0

CGI程序运行服务器端。一个C++程序可以是一个CGI程序。查看我链接的页面上的第一个示例。 –

回答

0

不能在文件系统从Javascript在浏览器中编写。我认为你应该使用客户端 - 服务器架构来完成你的应用程序。例如,客户端将是一个带有用户名和密码输入的简单HTML页面,服务器将保存并检查用户的凭证。在这种情况下,您可以使用Javascript将数据从页面发送到服务器。

+1

但是这需要一个mysql数据库,因为我希望服务器在注册后记住帐户。所以即使我关闭服务器,它仍然有创建的帐户。 –