我无法在线找到问题的解决方案.
我想在Unix中调用一个函数,传入一个目录的路径,并知道它是否存在.opendir()如果目录不存在则返回错误,但我的目标不是实际打开,检查错误,如果没有错误则关闭它,而只是检查文件是否是目录.
有没有方便的方法呢?
我想将单个字节数转换为文件大小(具有.KB,.MB和.GB).
如果数字是0,我不想有任何单位.如果数字可以被1024的倍数(不是浮点数)整除,那么我将打印:x.否则,我想打印一度精度的浮点.
我制作了一些似乎运行良好的代码,但它非常繁琐.我正在研究如何使我的功能更清洁/更高效,请说实话,非常难看:
char *
calculateSize( off_t size )
{
char *result = (char *) malloc(sizeof(char) * 20);
static int GB = 1024 * 1024 * 1024;
static int MB = 1024 * 1024;
static int KB = 1024;
if (size >= GB) {
if (size % GB == 0)
sprintf(result, "%d GB", size / GB);
else
sprintf(result, "%.1f GB", (float) size / GB);
}
else if (size >= MB) {
if (size % MB == 0)
sprintf(result, "%d MB", …Run Code Online (Sandbox Code Playgroud) 我有一个问题.我在C文件中定义了read-line.c,一个函数print,如下所示:
void history_print(void)
{
/* some stuff */
}
Run Code Online (Sandbox Code Playgroud)
在C++文件command.cc中,我有以下内容:
extern "C" void history_print(void);
Run Code Online (Sandbox Code Playgroud)
然后我只需调用history_print().
#Use GNU compiler
cc = gcc -g
CC = g++ -g
all: shell
tty-raw-mode.o: tty-raw-mode.c
gcc -c tty-raw-mode.c
read-line.o: read-line.c
gcc -c read-line.c
lex.yy.o: shell.l
lex shell.l
$(cc) -c lex.yy.c
y.tab.o: shell.y
yacc -d shell.y
$(CC) -c y.tab.c
command.o: command.cc
$(CC) -c command.cc
shell: y.tab.o lex.yy.o tty-raw-mode.o read-line.o command.o
$(CC) -o shell lex.yy.o y.tab.o tty-raw-mode.o read-line.o command.o -ll -lgen
Run Code Online (Sandbox Code Playgroud)
在我的Makefile中链接规则输出时遇到问题:
Undefined first referenced
symbol in …Run Code Online (Sandbox Code Playgroud)