小编Emp*_*uin的帖子

这个输出的原因是什么?

我有以下代码.

    int x=80;
    int &y=x;
    x++;
    cout<<x<<" "<<--y;
Run Code Online (Sandbox Code Playgroud)

输出结果是80 80.我不明白怎么做.我认为x的输出是81,尽管我对y一无所知.参数变量如何受减量运算符的影响.有人可以解释一下吗?

c++

14
推荐指数
2
解决办法
361
查看次数

如何检查文件是否被锁定?

我有以下代码,我想检查文件是否被锁定.如果没有,那么我想写信给它.我通过在两个终端上同时运行它来运行此代码但是我每次都在两个选项卡中都处于"锁定"状态,即使我没有锁定它.代码如下:

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main()
{
    struct flock fl,fl2;
    int fd;

    fl.l_type   = F_WRLCK;  /* read/write lock */
    fl.l_whence = SEEK_SET; /* beginning of file */
    fl.l_start  = 0;        /* offset from l_whence */
    fl.l_len    = 0;        /* length, 0 = to EOF */
    fl.l_pid    = getpid(); /* PID */

    fd = open("locked_file", O_RDWR | O_EXCL | O_CREAT);
    fcntl(fd, F_GETLK, &fl2);
    if(fl2.l_type!=F_UNLCK)
    {
        printf("locked");
    }
    else
    {
        fcntl(fd, F_SETLKW, &fl); /* set lock */
        write(fd,"hello",5);
        usleep(10000000); …
Run Code Online (Sandbox Code Playgroud)

c locking file fcntl

6
推荐指数
1
解决办法
4811
查看次数

为什么tcflush不能用于scanf?

我有这个简单的代码接受3个字符,:

char a,b,c;
scanf("%c",&a);
scanf("%c",&b);
scanf("%c",&c);
printf("%c",a);
printf("%c",b);
printf("%c",c);
Run Code Online (Sandbox Code Playgroud)

我明白为什么这只会接受2个字符,因为第二个scanf接受回车.但是,如果__fpurge(stdin);在每个scanf之间使用代码,则代码按预期工作.但是,如果我使用read(STDIN_FILENO,&a,1);而不是scanf,它不起作用.对于read(),只能tcflush(STDIN_FILENO,TCIOFLUSH);工作,但它与scanf失败.有人能解释一下为什么吗?

c linux linux-device-driver

4
推荐指数
1
解决办法
242
查看次数

标签 统计

c ×2

c++ ×1

fcntl ×1

file ×1

linux ×1

linux-device-driver ×1

locking ×1