我有一个ini文件,其中包含一些示例值,如:
[Section1]
Value1 = 10
Value2 = a_text_string
Run Code Online (Sandbox Code Playgroud)
我正在尝试加载这些值并使用Boost在我的应用程序中打印它们,但我不明白如何在C++中执行此操作.
我在这个论坛中搜索,以便找到一些例子(我总是使用C,所以我在C++中不是很好)但我只找到了关于如何一次性从文件中读取值的示例.
我需要在我想要的时候只加载一个值,比如string = Section1.Value2因为我不需要读取所有的值,而只需要读取其中的一些值.
我想加载单个值并将它们存储在变量中,以便在我的应用程序中使用它们时使用它们.
使用Boost可以做到这一点吗?
目前,我正在使用此代码:
#include <iostream>
#include <string>
#include <set>
#include <sstream>
#include <exception>
#include <fstream>
#include <boost/config.hpp>
#include <boost/program_options/detail/config_file.hpp>
#include <boost/program_options/parsers.hpp>
namespace pod = boost::program_options::detail;
int main()
{
std::ifstream s("file.ini");
if(!s)
{
std::cerr<<"error"<<std::endl;
return 1;
}
std::set<std::string> options;
options.insert("Test.a");
options.insert("Test.b");
options.insert("Test.c");
for (boost::program_options::detail::config_file_iterator i(s, options), e ; i != e; ++i)
std::cout << i->value[0] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但这只是读取for循环中的所有值; 相反,我只想在需要时读取单个值,并且我不需要在文件中插入值,因为它已经用我在程序中需要的所有值写入.
我在C++和OpenCV中有一个应用程序,它试图使用用SVMLight实现的分类模型,以便在OpenCV下添加可用于HOG Calssificator的权重值.
问题是,当我运行应用程序时,我收到此错误:
./main Converting Model file...
1%3%4%5%7%8%9%11%12%13%15%16%18%19%20%22%23%24%26%27%28%30%31%32%34%35%36%38%39%40%42%43%45%46%47%49%50%51%53%54%55%57%58%59%61%62%63%65%66%67%69%70%72%73%74%76%77%78%80%81%82%84%85%86%88%89%90%92%93%94%96%97%99%100%Skipped
line
*** glibc detected *** ./main: munmap_chunk(): invalid pointer:
0x09926218 ***
======= Backtrace: ========= /lib/libc.so.6(+0x6c501)[0xb6a3d501]
/lib/libc.so.6(+0x6d77e)[0xb6a3e77e]
/usr/lib/tls/libnvidia-tls.so.260.19.26(+0xa20)[0xb4116a20]
./main(_ZN9__gnu_cxx13new_allocatorIfE10deallocateEPfj+0x11)[0x804f7ab]
./main(_ZNSt12_Vector_baseIfSaIfEE13_M_deallocateEPfj+0x25)[0x804f075]
./main(_ZNSt12_Vector_baseIfSaIfEED2Ev+0x37)[0x804e9f1]
./main(_ZNSt6vectorIfSaIfEED1Ev+0x38)[0x804e46a]
./main[0x804bf65]
/lib/libc.so.6(__libc_start_main+0xe7)[0xb69e7ce7]
./main[0x804bd11]
======= Memory map: ======== 08048000-08052000 r-xp 00000000 08:01
27272122
/home/roccog/HOGImplementation/HOGTrainer/main
08052000-08053000 r--p 00009000 08:01
27272122
/home/roccog/HOGImplementation/HOGTrainer/main
08053000-08054000 rw-p 0000a000 08:01
27272122
/home/roccog/HOGImplementation/HOGTrainer/main
098dd000-0994a000 rw-p 00000000 00:00
0 [heap] b2387000-b238d000
rw-p 00000000 00:00 0
b238d000-b23dc000 r-xp 00000000 08:01
16386856 /usr/lib/libXt.so.6.0.0
b23dc000-b23dd000 r--p 0004e000 08:01
16386856 /usr/lib/libXt.so.6.0.0
b23dd000-b23e0000 rw-p 0004f000 …Run Code Online (Sandbox Code Playgroud) 目前,我正在使用Boost Asio以通过TCP连接到服务器.
我使用条件案例来决定应用程序是否必须启动与服务器的连接; 它工作得很好但问题是,如果我在服务器关闭时尝试连接到服务器,那么应用程序崩溃会出现此错误:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
what(): Connection refused
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的代码:
case CONNECTION:
// Connect to the server
using boost::asio::ip::tcp;
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), server, boost::lexical_cast<string>(porta));
tcp::resolver::iterator iterator = resolver.resolve(query);
tcp::socket s(io_service);
s.connect(*iterator);
Run Code Online (Sandbox Code Playgroud)
我想保持我的应用程序正常行为,并只提示连接失败的警告.
我该如何处理这个异常?
我遇到了OpenCV程序的问题,该程序正确编译但是当它运行时中止发出此错误:
OpenCV Error: Assertion failed (dims <= 2 && step[0] > 0) in locateROI, file /home/user/Desktop/HOME/src/OpenCV/modules/core/src/matrix.cpp, line 647
terminate called after throwing an instance of 'cv::Exception'
what(): /home/user/Desktop/HOME/src/OpenCV/modules/core/src/matrix.cpp:647: error: (-215) dims <= 2 && step[0] > 0 in function locateROI
Aborted
Run Code Online (Sandbox Code Playgroud)
我正在使用ubuntu和最新的OpenCV库,但它在使用Eclipse的Leopard上也给出了同样的错误.
matrix.cpp文件位于我的OpenCV源文件夹中.
我无法理解为什么我会收到此错误.
你能帮我吗?
非常感谢.
我在Ubuntu上使用LibSerial在串口上读写数据.
目前,我能够通过串口写入和接收字符串,但我的代码不能很好地工作:特别是,我想控制读取功能,以便只有在有东西需要读取时才能读取并且当没有信息要读取时退出,以便发送另一个命令而不会使流程程序失败.
我想要做:
现在,我能够发送第一个命令并在while循环中使用read函数读取答案,但我无法做任何其他事情. 我无法发送第二个命令,因为while循环永远不会退出,所以程序继续读取.
你能帮我吗?
这是我正在使用的代码:(读写函数位于代码的末尾)
#include <SerialStream.h>
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <string>
int
main( int argc,
char** argv )
{
//
// Open the serial port.
//
using namespace std;
using namespace LibSerial ;
SerialStream serial_port ;
char c;
serial_port.Open( "/dev/ttyACM0" ) ;
if ( ! serial_port.good() )
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
<< "Error: Could not open serial …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个Mat文件,我想在一个带有cvShowImage的窗口中显示,该文件定义为:
void cvShowImage( const char* name, const CvArr* image )
Run Code Online (Sandbox Code Playgroud)
现在,问题是如果我直接传递Mat图像,它会给我一个错误转换:
cannot convert 'cv::Mat' to 'const CvArr*' for argument '2' to 'void cvShowImage(const char*, const CvArr*)'
Run Code Online (Sandbox Code Playgroud)
我试图在这个论坛中搜索有同样问题的人,我找到了这个opencv文档:http: //opencv.willowgarage.com/documentation/cpp/c++_cheatsheet.html
但我不明白如何使用它.
有人能给我一个如何将Mat图像转换为IplImage的例子吗?
这是我的代码:
Mat file;
Mat hogResultFrame = hogStep(temp2);
file = hogResultFrame;
cvShowImage(window_title, (const CvArr*)(file));
Run Code Online (Sandbox Code Playgroud)
但它给了我一个错误的歪曲.
我希望你能帮帮我,
非常感谢!
我有一个程序,它运行两个不同的操作,我想在它们之间共享变量.
目前,我正在使用线程而不是fork进程,但即使我将它们声明为volatile也存在共享变量的问题.
我尝试使用boost做:
boost::thread collisions_thread2(boost::bind(function_thread2);
Run Code Online (Sandbox Code Playgroud)
通过将共享变量声明为volatile,但似乎function_thread2()函数无法看到共享变量的变化.
我想做的是:
thread1:
while(true){
//..do somet stuff
check variable1
}
thread2:
while(true){
do some other stuff
check and write on variable1
}
Run Code Online (Sandbox Code Playgroud)
你能建议我在线程之间轻松共享变量的教程或方法吗?可能是boost库在这种情况下有用吗?你认为使用fork()会更好吗?
我知道我必须使用互斥锁以避免危急情况,但我从未使用它.
我在 g++ 和在 Ubuntu 下使用蓝牙库的 C/C++ 程序的编译过程中遇到了一些问题。
如果我使用 gcc,它工作正常,没有任何警告;相反,如果我使用 g++ 我会收到此警告:
警告:正在获取临时地址
即使程序编译良好并且可以工作。
报告错误涉及的行是:
bdaddr_t *inquiry(){
// do some stuff..
bacpy(&result[mote++], BDADDR_ANY);
return result;
}
//...
void zeemote(){
while (bacmp(bdaddr, BDADDR_ANY)){
/..
}
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,都涉及 BDADDR_ANY。
我该如何解决这个警告?
BDADDR_ANY 在 bluetooth.h 中定义如下:
/* BD Address */
typedef struct {
uint8_t b[6];
} __attribute__((packed)) bdaddr_t;
#define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
Run Code Online (Sandbox Code Playgroud) 我正在编写一个带有Dialog窗口和一些按钮的简单MFC应用程序.我还添加了一个编辑控件,以便让用户插入文本字符串.
我想读取编辑控件中存在的值并将其存储在字符串中,但我不知道如何执行此操作.
我没有编译错误,但我总是只读"." 标记.
我在文本编辑控件中添加了一个变量名filepath1,这是代码:
// CMFC_1Dlg dialog
class CMFC_1Dlg : public CDialogEx
{
// Construction
public:
CMFC_1Dlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_MFC_1_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
afx_msg void OnEnChangeEdit1();
CString …Run Code Online (Sandbox Code Playgroud) 我有一个带有16个继电器的电子板,它可以通过TCP / IP进行工作。主板的IP地址是192.168.1.4,端口是3000。我想在Ubuntu下用C / C ++对其进行控制。
可以使用十六进制命令列表来远程打开和关闭板上的每个继电器。这是清单:
"580112000000016C", // switch on the relay 1
"580111000000016B", // switch off the relay 1
"580112000000026D", // switch on the relay 2
"580111000000026C", // switch off the relay 2
"580112000000036E", // and so on..
Run Code Online (Sandbox Code Playgroud)
我可以通过在Ubuntu下发送命令行命令来正确地打开和关闭每个中继:
echo '580112000000016C' | xxd -r -p | nc 192.168.1.4 3000
Run Code Online (Sandbox Code Playgroud)
上面的代码正确打开了继电器。
我想对C / C ++代码做同样的事情,因为我想从WxWidgets应用程序控制开发板。目前,我是从基础开始的,我只是使用C / C ++代码来测试tcp连接。
这是我的代码:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
#include <string>
#include <unistd.h>
using namespace std;
int …Run Code Online (Sandbox Code Playgroud)