Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Lista 4
- Questão 2
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define T_S 200;
- #define T_V 50;
- prod p[T_V]; //variaveis globais
- int num_produto;
- struct material {
- char nome[T_S];
- int qnt;
- }
- typedef struct material mat;
- struct produto {
- char nome[T_S];
- mat materiais[T_V];
- }
- typedef struct produto prod;
- prod criaproduto() {
- prod tprod;
- int i;
- printf("Nome do produto: ");
- gets(tprod.nome);
- printf("Quantidade de materiais: ");
- scanf("%d", &tprod.qnt_material);
- for(i=0, i<qnt_material, i++) {
- printf("Nome do material: ");
- gets(tprod.materiais[i].nome);
- printf("Quantidade utilizada: ");
- scanf("%d", &tprod.materiais[i].qnt);
- }
- return tprod;
- }
- void produto_imt(char nome_Arq[T_S], prod p) {
- FILE*arquivo
- arquivo = fopen("nome_Arq", "w");
- if(arquivo == NULL) {
- printf("Erro na abertura do arquivo!");
- exit(1);
- }
- fprintf(arquivo,"%c\n %c\n %d", p.nome,p.materiais[i].nome,qnt);
- fclose(arquivo);
- }
- fwrite(&p,sizeof(prod),1,arquivo);
- void leiturat(char nome_Arq[T_S]) {
- int i,j;
- FILE*arq
- arq = fopen("nome_Arq","r");
- if(arq==NULL) {
- printf("Erro ao abrir o arquivo!!!");
- }
- fscanf(arq,"%d\n",&num_produtos);
- for(i=0, i<num_produtos, i++) {
- fgets(p[i].nome,T_S,arq);
- fscanf(arq,"%d\n",&p[i].qnt_material);
- for(j=0, j<p[i].qnt_material, j++) {
- fscanf(arq,"%s\n",&p[i].materiais[j]);
- }
- }
- fclose(arq);
- }
- main() {
- prod pd;
- char nome_arq[T_S];
- pd = criaproduto();
- printf("Nome do arquivo: ");
- gets(nome_Arq);
- produto_imt(nome_Arq, pd);
- leiturat(nome_Arq);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement