Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. Lista 4
  2. Questão 2
  3.  
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include<stdlib.h>
  7.  
  8. #define T_S 200;
  9. #define T_V 50;
  10.  
  11. prod p[T_V]; //variaveis globais
  12. int num_produto;
  13.  
  14. struct material {
  15. char nome[T_S];
  16. int qnt;
  17. }
  18. typedef struct material mat;
  19.  
  20. struct produto {
  21. char nome[T_S];
  22. mat materiais[T_V];
  23. }
  24. typedef struct produto prod;
  25.  
  26. prod criaproduto() {
  27. prod tprod;
  28. int i;
  29. printf("Nome do produto: ");
  30. gets(tprod.nome);
  31. printf("Quantidade de materiais: ");
  32. scanf("%d", &tprod.qnt_material);
  33. for(i=0, i<qnt_material, i++) {
  34. printf("Nome do material: ");
  35. gets(tprod.materiais[i].nome);
  36. printf("Quantidade utilizada: ");
  37. scanf("%d", &tprod.materiais[i].qnt);
  38. }
  39. return tprod;
  40. }
  41.  
  42. void produto_imt(char nome_Arq[T_S], prod p) {
  43. FILE*arquivo
  44. arquivo = fopen("nome_Arq", "w");
  45. if(arquivo == NULL) {
  46. printf("Erro na abertura do arquivo!");
  47. exit(1);
  48. }
  49. fprintf(arquivo,"%c\n %c\n %d", p.nome,p.materiais[i].nome,qnt);
  50. fclose(arquivo);
  51. }
  52. fwrite(&p,sizeof(prod),1,arquivo);
  53.  
  54. void leiturat(char nome_Arq[T_S]) {
  55. int i,j;
  56. FILE*arq
  57. arq = fopen("nome_Arq","r");
  58. if(arq==NULL) {
  59. printf("Erro ao abrir o arquivo!!!");
  60. }
  61. fscanf(arq,"%d\n",&num_produtos);
  62. for(i=0, i<num_produtos, i++) {
  63. fgets(p[i].nome,T_S,arq);
  64. fscanf(arq,"%d\n",&p[i].qnt_material);
  65. for(j=0, j<p[i].qnt_material, j++) {
  66. fscanf(arq,"%s\n",&p[i].materiais[j]);
  67. }
  68. }
  69. fclose(arq);
  70. }
  71.  
  72. main() {
  73. prod pd;
  74. char nome_arq[T_S];
  75. pd = criaproduto();
  76. printf("Nome do arquivo: ");
  77. gets(nome_Arq);
  78. produto_imt(nome_Arq, pd);
  79. leiturat(nome_Arq);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement