Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct al{
- int num;
- char local[50];
- char maq[50];
- int ano;
- char tipo[50];
- }VECstruct;
- VECstruct reg;
- FILE *fx;
- int pesquisa(int x){
- int p;
- fx=fopen("fotos.dat","r");
- fread(®,sizeof(reg),1,fx);
- while(x!=reg.num && !feof(fx)){
- fread(®,sizeof(reg),1,fx);
- }
- p=ftell(fx);
- fclose(fx);
- if(reg.num==x){
- return p;
- }
- else
- return 0;
- }
- void insere(){
- int n,c;
- do{
- printf("Numero do registo: ");
- scanf("%d",&n);
- if(pesquisa(n)==0){
- fx=fopen("fotos.dat","a+");
- reg.num=n;
- printf("Local: ");
- scanf("%s",reg.local);
- printf("Maquina: ");
- scanf("%s",reg.maq);
- printf("Ano: ");
- scanf("%d",®.ano);
- printf("Tipo: ");
- scanf("%s",reg.tipo);
- fwrite(®,sizeof(reg),1,fx);
- fclose(fx);
- }
- else printf("Esse registo ja existe\n");
- printf("Continuar?\n");
- scanf("%d",&c);
- }while(c==1);
- }
- void listar(){
- int i,lmi,lms,vano[3000];
- for(i=0;i<3000;i++){vano[i]=0;}
- printf("Limite inferior: ");
- scanf("%d",&lmi);
- printf("Limite superior: ");
- scanf("%d",&lms);
- fx=fopen("fotos.dat","r");
- fread(®,sizeof(reg),1,fx);
- while(!feof(fx)){
- if(reg.ano>=lmi && reg.ano<=lms){
- /* printf("\nNumero de registo: %d\n",reg.num);
- printf("Local: %s\n",reg.local);
- printf("Maquina: %s\n",reg.maq);
- printf("Ano: %d\n",reg.ano);
- printf("Tipo: %s\n",reg.tipo);*/
- vano[reg.ano]++;
- }
- fread(®,sizeof(reg),1,fx);
- }
- printf("Fotos por ano:\n");
- for(i=0;i<3000;i++){
- if(vano[i]>0){
- printf("%d-",i);
- printf("%d\n",vano[i]);
- }
- }
- fclose(fx);
- }
- void altera(){
- char lp[50],nl[50];
- printf("Local a pesquisar: ");
- scanf("%s",lp);
- printf("Local novo: ");
- scanf("%s",nl);
- fx=fopen("fotos.dat","r+");
- fread(®,sizeof(reg),1,fx);
- while(!feof(fx)){
- if(strcmp(reg.local,lp)==0){
- strcpy(reg.local,nl);
- fseek(fx,-sizeof(reg),1);
- fwrite(®,sizeof(reg),1,fx);
- fseek(fx,sizeof(reg),1);
- }
- fread(®,sizeof(reg),1,fx);
- }
- fclose(fx);/*
- fx=fopen("fotos.dat","r+");
- fread(®,sizeof(reg),1,fx);
- while(!feof(fx)){
- if(strcmp(reg.local,lp)==0){
- strcpy(reg.local,nl);
- fseek(fx,-sizeof(reg),1);
- fwrite(®,sizeof(reg),1,fx);
- fseek(fx,sizeof(reg),1);
- }
- fread(®,sizeof(reg),1,fx);
- }
- fclose(fx);*/
- }
- int main(){
- int x;
- if(fopen("fotos.dat","r")==NULL){
- fx=fopen("fotos.dat","w");
- fclose(fx);
- }
- printf("1-Inserir fotos\n");
- printf("2-Listar fotos entre 2 anos e total de fotos por ano \n");
- printf("3-Alterar fotos de um local\n");
- printf("4-Sair");
- do{
- printf("\n: ");
- scanf("%d",&x);
- printf("\n");
- switch(x){
- case 1:insere(); break;
- case 2:listar(); break;
- case 3:altera(); break;
- }
- }while(x!=4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement