How do dev files work?

How the guys from Linux make files /dev. You can write to them, and they are immediately erased. I can imagine some program that constantly reads the dev file:

FILE *fp;
char buffer[255];
int result;
fp = fopen(fileName, "r");
if (!fp) {
    printf("Open file error");
    return;
}
while (1)
{

    result = fscanf(fp, "%254c", buffer);
    printf("%s", buffer);
    memset(buffer, 0, 255);
    fflush(stdout);
    sleep(1);
}
fclose(fp);

But how to remove content? Closing the file and opening them again in "w" mode is not the way they did, because you can do iecat > /dev/tty

+4
source share
4 answers

? - , . /home/joe/foo.txt, , ( , ), , , (, read write) , .

. /dev/foo, , , ( , !). obejct . , , read write , foo - - .

/dev/ ; " " ( mknod). , " ".

cat > /dev/tty, , "" " ". , . , cat write , , - tty . tty, , . , xterm, : - slave--tty. tty -tty-, cat > /dev/tty "": -tty, while (1) C, , , . xterm ( - ); , ..

+7

Unix (tty, printer ..), ( ), /dev , .

, , , , -, , .

+3

, " " , . .

/ , - , - (, , ), - , - (, , ). C /.

, "" . dev, - . (, udev), , . , dev dev.

+2

All Articles