在 Perl 或 Javascript 中,它是单行的:
my($net, $bits) = split('/', $data, 2);
或者
let [net, bits] = data.split('/');
Python中有单行吗?据我所知,它需要几行。例如:
res = data.split('/')
ip, bits = res[0], None
if len(res) == 2:
bits = res[1]
Run Code Online (Sandbox Code Playgroud)
或更好,
res = data.split('/')
ip, bits = res if len(res) == 2 else res[0], None
Run Code Online (Sandbox Code Playgroud) 在 popen() 之后,fread() 总是返回 0。将 read() 与 fileno(fp) 一起使用是可行的。这是怎么回事?
这是代码。
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
FILE *fp = popen("echo hello", "r");
if (!fp) {
perror("echo hello");
} else {
char buffer[128];
for (;;) {
int n;
if (argc < 2) {
n = fread(buffer, sizeof buffer, 1, fp);
} else {
n = read(fileno(fp), buffer, sizeof buffer);
}
printf("read %d bytes\n", n);
if (n <= 0) break;
fwrite(buffer, n, 1, stdout);
}
pclose(fp); …Run Code Online (Sandbox Code Playgroud)