與修改後的代碼更新SCREENSHOTS上的Sybase表運行在C++
有Sybase數據庫的設置和所提供的ISQL腳本連接到它的工作原理SQL命令。我現在想要做的是能夠連接到數據庫以在其上運行一些sql命令(使用isql腳本或不使用)並將輸出存儲在數據結構中以便在C++中處理每一行。我在網上找到了一小段代碼,它應該允許我通過以c風格的字符串傳遞命令來運行isql腳本,但是嘗試編譯cpp文件會導致popen和pclose錯誤,而這些錯誤並未在範圍中聲明。如果有另一種連接數據庫的方式,或者您知道如何解決該錯誤,請告訴我。
原始代碼:
#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
std::string exec(const char* cmd) {
char buffer[128];
std::string result = "";
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose());
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer, 128, pipe.get()) != NULL)
result += buffer;
}
return result;
}
Compiling code with GNU++14 in response to ildjarn
Compiling code with lambda function for pipe in response to max66
嘗試用編譯'-std = GNU ++ 14'代替'-std = C++ 14',和變化'pclose函數()''到或pclose''&pclose'。 – ildjarn
更新了錯誤消息和代碼的屏幕截圖 –