Advertisement
Guest User

lab 1

a guest
Aug 22nd, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include<io.h>
  2. #include<sys\stat.h>
  3. #include<fcntl.h>
  4. #include<string.h>
  5. #include<stdio.h>
  6. #include<iostream.h>
  7. using namespace std;
  8. int fd; //file descriptor
  9. void escribir(){
  10.     char mens[50];
  11.     if((fd=creat("b.txt",S_IWRITE|S_IREAD))<0){
  12.         cout<<"no se pudo crear el archivo"<<endl;
  13.         return;
  14.     }
  15.     cout<<"mensaje: "<<endl;
  16.     gets(mens);
  17.     write(fd,mens,strlen(mens));
  18.     close(fd);
  19. }
  20. void leer(){
  21.     int k;
  22.     if((fd=open("b.txt",O_RDONLY))<0){
  23.         cout<<"no se pudo abrir"<<endl;
  24.         return;
  25.     }
  26.     while(read(fd,&k,1)>0){
  27.         cout<<k;
  28.     }
  29.     close(fd);
  30. }
  31. int main(){
  32.     int opc;
  33.     do{
  34.         cout<<"1. escribir  2. leer  3. salir"<<endl;
  35.         cin>>opc;
  36.         switch(opc){
  37.             case 1: escribir(); break;
  38.             case 2: leer(); break;
  39.     }
  40.         }while(opc!=3);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement