如何在C++代码中从shell重定向获取文件名?ie ./MyProgram <myfile,我想逐行获取myfile作为文件名及其内容.
**编辑:我设法从文件中获取输入.谢谢您的帮助.但是,在循环遍历文件内容之后,我想用cin保持用户输入.就像这样:
while (true)
{
if (cin.eof() == false)
{
getline(cin, line);
cout << line;
}else{
cin >> choice;
}
}
Run Code Online (Sandbox Code Playgroud) 例如,我定义了一个宏:
#ifdef VERSION
//.... do something
#endif
Run Code Online (Sandbox Code Playgroud)
如何检查VERSION
目标文件中是否存在?我试着用它拆解它objdump
,但发现我的宏没有实际价值VERSION
.VERSION
在Makefile中定义.
在Lisp中(我仍在使用SBCL学习Lisp),声明了局部变量let
,范围仅在该表达式中.这是为什么?与其他命令式语言(如C/C++/Java ...)不同,我们可以在其功能范围内的任何位置自由使用局部变量.
如何将当前文件及其路径传递到 org-babel 中的源代码块之一?例如:
#+name: cflow
#+header: var file=<what to put here?>
#+begin_src sh :exports none
# output posix format
# -i <symbol> include name start with <symbol> file
# brief input
cflow --format=posix -i _ --brief
#+end_src
Run Code Online (Sandbox Code Playgroud)
我希望该命令在缓冲区的当前目录中执行。
假设我有一个指针*p,它指向堆上的10个内存块.现在,我用NULL(或'\ 0')手动清除它,而不是free()它,如下所示:
for (int i = 0; i < length; ++i{
p[i] = '\0';
}
Run Code Online (Sandbox Code Playgroud)
这被认为是当每个位被清零时释放的存储器块?或者,我必须使用free()来告诉操作系统内存块真正被释放并且可用(我猜)?
所以,例如,我想链接一个boost :: thread库,我必须在命令行中键入-lboost_thread.但是,我如何知道其他库的名称,所以我不必猜测要放在命令行中的名称.例如,为了链接线程库,我把-lboost_thread作为随机猜测来自另一个我看到的boost库的链接示例.有没有办法一般知道要放入命令行的库的名称?
Boost有这样的事情:
void OnAccept( const boost::system::error_code & ec)
{
if( ec )
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
error_code
是一个班级.要覆盖哪个运算符来创建类对象
所以,我想保留的两个信息short
到一个int
用于在网络上发送后(这只是一个演示,这里没有网络代码).我将每个short
数字的两个字节复制到prim
(原始)变量中.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
int main()
{
unsigned int prim = 0;
short major = 0x0001;
short minor = 0x0000;
prim = 0;
memcpy(&prim, &major, 2);
memcpy(&prim+2,&minor, 2);
std::cout << "Prim: " << prim << std::endl;
return EXIT_SUCCESS
}
Run Code Online (Sandbox Code Playgroud)
如果我复制第major
一个,然后minor
(如上面的代码所示),我会得到正确的输出:
Prim: 1
Run Code Online (Sandbox Code Playgroud)
因为我的CPU是小端,二进制表示应该是:
10000000 00000000 00000000 00000000
Run Code Online (Sandbox Code Playgroud)
但是,如果我改变顺序,minor
首先意味着更major
晚,就像这样:
memcpy(&prim, &minor, 2);
memcpy(&prim+2,&major, 2);
Run Code Online (Sandbox Code Playgroud)
我得到0.
Prim: 0
Run Code Online (Sandbox Code Playgroud)
为什么?它应该是:
00000000 00000000 …
Run Code Online (Sandbox Code Playgroud) 通常我们必须这样做以从函数指针调用函数:
int foo()
{
}
int main()
{
int (*pFoo)() = foo; // pFoo points to function foo()
foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在Linux内核代码中,sched_class有许多函数指针:
struct sched_class {
const struct sched_class *next;
void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
void (*yield_task) (struct rq *rq);
bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
.....
}
Run Code Online (Sandbox Code Playgroud)
在pick_next_task函数中,它定义了一个本地的sched_class
named 实例class
,并直接调用其中的函数,而不分配具有相同签名的外部函数(从...开始for_each_class
):
static …
Run Code Online (Sandbox Code Playgroud) 你能解释一下这些选项在下面的g ++命令中构建boost.python对象的含义吗?
# location of the Python header files
PYTHON = /usr/include/python2.7
# location of the Boost Python include files and library
BOOST_INC = /usr/include
BOOST_LIB = /usr/lib
# compile mesh classes
TARGET = ex1
$(TARGET).so: $(TARGET).o
g++ -shared -Wl,--export-dynamic \
$(TARGET).o -L$(BOOST_LIB) -lboost_python \
-L/usr/lib/python2.7/config -lpython2.7 \
-o $(TARGET).so
$(TARGET).o: $(TARGET).c
g++ -I$(PYTHON) -I$(BOOST_INC) -c $(TARGET).c
Run Code Online (Sandbox Code Playgroud)
做什么:
意思?
我应该学习哪些其他概念来充分利用gcc/g ++编译器以及构建实用程序?编辑:我不需要所有这些,但最常用的功能是什么?
有没有更好的方法来确切地知道,我需要链接到构建所需的库?目前,我只能做猜测来弄清楚要链接的内容,即如果我使用Boost的日期时间库,我做了-lboost_date_time这样的事情,它只是起作用,但有时不适用于其他库.
另外,对于boost.python,我不想使用Bjam,因为它需要花费很多时间来学习,文档似乎含糊不清.make实用程序对我来说似乎更普遍.但是,有没有一个ide可以自动化构建过程,如Windows中的MSVS?代码::块?手动编写makefile比IDE管理的优点是什么?它似乎可以节省大量的构建自动化时间.