小编Yan*_*Yan的帖子

如何从函数内部确定函数名称

如果我有这样的BASH脚本:

#!/bin/bash

f() {
  # echo function name, "f" in this case
}
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?这可以用在诸如的帮助消息中

printf "Usage: %s: blah blah blah \n" $(basename $0) >&2; 
Run Code Online (Sandbox Code Playgroud)

只有在这种情况下我想要的不是$0,这是脚本的文件名.

bash function

142
推荐指数
4
解决办法
4万
查看次数

36
推荐指数
2
解决办法
3万
查看次数

basic_filebuf :: underflow在/ proc/pid/stat上使用ifstream读取文件时出错

为什么以下代码会抛出异常?请注意,该文件是一个/proc/pid/stat文件,因此它可能会受到内核的干扰.

// Checked that file does exist
try {
  std::ifstream file(path.c_str());
  // Shouldn't even be necessary because it's the default but it doesn't 
  // make any difference.
  file.exceptions(std::ifstream::goodbit);
  // Read the stream into many fields
  // !!!! The exception was thrown here.
  file >> _ >> comm >> state >> ppid >> pgrp >> session >> tty_nr
       /* >> ... omitted */;
  file.close();
} catch (const std::ifstream::failure& e) {
  std::cout << "Exception!!!! " << e.what();
}
Run Code Online (Sandbox Code Playgroud)

例外是"读取文件的basic_filebuf :: …

c++ linux g++

5
推荐指数
1
解决办法
7102
查看次数

标签 统计

bash ×1

bytearray ×1

c++ ×1

function ×1

g++ ×1

linux ×1

python ×1