Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. <?php
  2.  
  3. class Pessoa {
  4.  
  5. var $nome;
  6. var $idade;
  7. var $sexo;
  8.  
  9. }
  10.  
  11. function insere()
  12. {
  13. printf("\n C A D A S T R O D E P E S S O A\n\n");
  14. printf("\nNome:");
  15. $nome=trim(fgets(STDIN));
  16. printf("\nIdade:");
  17. $idade=trim(fgets(STDIN));
  18. printf("\nSexo:");
  19. $sexo=trim(fgets(STDIN));
  20.  
  21. $arquivo = fopen('pessoa.txt','a+');
  22. if($arquivo == false)
  23. printf("\nNão foi possível criar o arquivo.");
  24. else
  25. {
  26. fprintf($arquivo, "%s", utf8_encode($nome).";".$idade.";".$sexo."\n"); // grava formatando acentuação do nome
  27. fclose($arquivo);
  28. }
  29. }
  30.  
  31. function imprime()
  32. {
  33. printf("\n E X I B I Ç Ã O D O C A D A S T R O\n");
  34. $arquivo = @fopen("pessoa.txt", "r+");
  35. $x = 1;
  36. if( $arquivo == true ) {
  37. while( !feof( $arquivo ) ) {
  38. $linha = fgets( $arquivo );
  39. if($linha!="")
  40. {
  41. $reg = explode(";",$linha);
  42. printf("\n ----------- %dº REGISTRO ---------", $x);
  43. printf("\nNome: %s", $reg[0]);
  44. printf("\nIdade: %d", (int)$reg[1]);
  45. printf("\nSexo: %s\n", $reg[2]);
  46. $x++;
  47. }
  48. else
  49. printf("\nFIM DE CADASTRO!!!\n");
  50. }
  51. fclose($arquivo);
  52. }
  53. trim(fgetc(STDIN));
  54. }
  55.  
  56. function altera()
  57. {
  58. printf("\n A L T E R A Ç Ã O D O C A D A S T R O\n");
  59. printf("\nDigite o nome a ser alterado:");
  60. $buscanome=trim(fgets(STDIN));
  61. $string = "";
  62. $arquivo = @fopen("pessoa.txt", "r+");
  63. if($arquivo)
  64. { while(true)
  65. { $linha = fgets($arquivo);
  66. if($linha==null)
  67. break;
  68. if(preg_match("/".$buscanome."/",$linha))
  69. {
  70. printf("\nDigite o novo nome:");
  71. utf8_encode($novonome=trim(fgets(STDIN)));
  72. printf("\nDigite a idade:");
  73. $novaidade=trim(fgets(STDIN));
  74. printf("\nDigite o sexo:");
  75. $novosexo=trim(fgets(STDIN));
  76. $string .= str_replace($linha,$novonome.";".$novaidade.";".$novosexo."\n",$linha);
  77. }
  78. else
  79. $string.= $linha;
  80. }
  81. }
  82. rewind($arquivo);
  83. ftruncate($arquivo,0);
  84. if(!fwrite($arquivo,$string))
  85. printf("\nNão foi possível atualizar o arquivo.");
  86. else
  87. printf("\nArquivo atualizado com sucesso!!!");
  88. fclose($arquivo);
  89. trim(fgetc(STDIN));
  90. }
  91.  
  92. function exclui()
  93. {
  94. printf("\n E X C L U S Ã O D E R E G I S T R O\n");
  95. printf("\nDigite o nome a ser excluído:");
  96. $buscanome=trim(fgets(STDIN));
  97. $string = "";
  98. $arquivo = @fopen("pessoa.txt", "r+");
  99. if($arquivo)
  100. { while(true)
  101. { $linha = fgets($arquivo);
  102. if($linha==null)
  103. break;
  104. if(preg_match("/".$buscanome."/",$linha))
  105. $string .= str_replace($linha,"",$linha);
  106. else
  107. $string.= $linha;
  108. }
  109. }
  110. rewind($arquivo);
  111. ftruncate($arquivo,0);
  112. fwrite($arquivo,$string);
  113. fclose($arquivo);
  114. trim(fgetc(STDIN));
  115. }
  116.  
  117. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement