added pipe example

This commit is contained in:
Pau 2024-09-26 19:15:50 +02:00
parent c8bc30878e
commit a99dd50c31
3 changed files with 33 additions and 0 deletions

BIN
pipe1 Executable file

Binary file not shown.

32
pipe1.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void main(void) {
char saludo[] = "Saludos peña!!!\n";
char buffer[10];
int fd, bytesleidos;
fd = open("texto.txt",1); // abrimos para escritura
if (fd==-1) {
printf("Algo salió mal\n");
exit(-1);
}
printf("Escribo el saludo en el fichero...");
write(fd,saludo, strlen(saludo));
close(fd);
fd=open("texto.txt",0); // abrimos para lectura
printf("Contenido del Fichero: \n");
bytesleidos = read(fd,buffer,1);
while (bytesleidos!=0) {
printf("%s", buffer);
bytesleidos = read(fd,buffer,1);
}
close (fd);
}

1
texto.txt Normal file
View File

@ -0,0 +1 @@
Saludos peña!!!