与修改后的代码更新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
更新了错误消息和代码的屏幕截图 –