Carregar apresentação
A apresentação está carregando. Por favor, espere
PublicouRafaela Sousa Beretta Alterado mais de 10 anos atrás
1
rangel@dsc.ufpb.br DSC/CCT/UFCG Profs.:Roberto Medeiros de Faria Ulrich Schiel Carga Horária:60 h
2
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 2 Tópicos 9.1Introdução 9.2Declaração de Estruturas 9.3Uso de Estruturas 9.4Definição de Arquivos 9.5Abertura e fechamento de arquivos 9.6Escrever e ler arquivos 9.7Um exemplo 9. Estruturas e Arquivos
3
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 3 9.2. Declaração de Estruturas Combinação heterogênea de variáveis Data = Ficha-Emp = End = Logradouro = ou
4
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 4 9.2. Declaração de Estruturas struct rótulo { decl1;... decln; } nome; struct data{ int dia; char mes[4]; int ano; }; struct data hoje,*atual, vencimentos[5];
5
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 5 9.2. Declaração de Estruturas struct data{ int dia; char mes[4]; int ano; }; struct cadastro { char nome[30]; struct data nascimento; } ficha; strcpy(ficha.nome,”Jose Maria”); ficha.nascimento.dia = 21; strcpy(ficha.nascimento.mes,”Jan”); ficha.nascimento.ano=1995;
6
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 6 9.3uso Operações válidas Atribuições a componentes strcpy(hoje.mes, “Ago”); hoje.dia = 31; hoje.ano = 2007; Atribuição de estruturas hoje = vencimento[1]; Acessando o endereço de uma estrutura &hoje Aplicando size-of sizeof(ficha)
7
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 7 9.3uso Atribuição integral hoje = {31, “Ago”, 2007} Atribuição parcial strcpy(vencimento[1].mes, “Nov”); vencimento[1].dia = 8; vencimento[1].ano = 2007;
8
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 8 9.3uso Entrada e saída scanf(“%d”, &hoje.dia); printf(“%d”, hoje.dia); struct data{ int dia; char mes[4]; int ano; } hoje; struct data *atual, vencimentos[5];
9
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 9 9.2. Estruturas como parâmetros struct data{ int dia; char mes[4]; int ano; }; struct data hoje,*atual, vencimentos[5]; mostradata(hoje); mostradata(struct data d){ }
10
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 10 9.3. Uso Uso do apontadores Operador aresta (->) com apontadores para variáveis struct struct data hoje,*atual; atual = &hoje; printf( "%s", atual->mes ); Ou printf(“%s”, ( *myCardPtr ).suit
11
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 11 9.4Arquivos Variáveis para Arquivos FILE *apontador; Exemplo FILE *perguntas; Funções fopen(“nome-do-arquivo”, “modo-de-acesso”); (modo pode ser “w”, “r” ou “a”) fwrite(p, bytes, número, stream); fread(p, bytes, número, stream); feof() fclose(stream)
12
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 12 9.4Arquivos Exemplo FILE *perguntas; char pergunta[] = “Qual e´ teu nome?”; Funções fopen(“enquete.txt”, “w”); fwrite(pergunta, size(pergunta), 1, perguntas); fread(pergunta, 20, 1, perguntas); feof(); (!=0 se for o fim do arquivo) fclose(perguntas);
13
rangel@dsc.ufpb.br rangel@lmrs-semarh.ufpb.br DSC/CCT/UFCG 13 9.4Arquivos #include int main() { FILE *per; char pergunta[40]; pergunta[0] = '\0'; strcpy(pergunta, "Primeira pergunta?"); per = fopen("questoes.txt", "w"); fwrite(pergunta, 40, 1, per); fwrite("Segunda pergunta?", 40, 1, per); fclose(per); /* reabertura do arquivo */ fopen("questoes.txt","r"); fread(pergunta, 40, 1, per); printf("\nA pergunta recuperada eh: %s", pergunta); fread(pergunta, 40, 1, per); printf("\nA segunda pergunta recuperada eh: %s\n", pergunta); fclose(per); system("pause"); return 0; }
14
rangel@dsc.ufpb.br DSC/CCT/UFCG Ulrich Schiel DEPARTAMENTO DE SISTEMAS E COMPUTAÇÃO UNIVERSIDADE FEDERAL DE CAMPINA GRANDE CENTRO DE CIÊNCIAS E TECNOLOGIA
Apresentações semelhantes
© 2025 SlidePlayer.com.br Inc.
All rights reserved.