Jump to content
Sign in to follow this  
gunky

Erro em estruturas...[Resolvido]

Recommended Posts

gunky

Boas tenho 1 array de estruturas e quero chamar 1 funçao que preenche essa estrutura, guarda num ficheiro e depois volte a ler do mesmo ficheiro....

ta-me a dar problemas a aceder aos campos....

alguem tem alguma ideia de onde esta o erro?!

cumps


typedef struct aluno{

char nome[150];
char cod[10];  //codigo do aluno e unico
int idade;
//Disciplina inscricao;  // campo que guarda as disciplinas o nivel e as notas do aluno
}ALUNO;


ALUNO array_a[200];	

void escreve(ALUNO *al[200])
{
FILE *fp;
int i=0;


fp=fopen("Alunos.dat","wb");
if(fp==NULL)
	printf("Erro abrir ficheiro");


for(;i<1;i++)
{
	printf("nome:\n");
	fflush(stdin);
	gets(al[i]->nome);
	printf("codigo:\n");
	fflush(stdin);
	gets(al[i]->cod);
	printf("idade:\n");	
	fflush(stdin);
	scanf(" %d", &(al[i]->idade));	 
	printf("------------->%d\n",al[i]->idade);

	fwrite(&al,sizeof(ALUNO),1,fp);

}
fclose(fp);

fp=fopen("Alunos.dat","rb");
if(fp==NULL)
	printf("Erro abrir ficheiro");



	for(i=0;i<1;i++)
	{
		while ( fread(&al, sizeof(ALUNO), 1, fp) == 1 )

		printf("------>valor de i:%d\n\n\n",i);
		printf("nome ------ %s\n", al[i]->nome);
		printf("------>valor de i:%d\n\n\n",i);
		printf("codigo ------ %s\n", al[i]->cod);
		printf("------>valor de i:%d\n\n\n",i);
		printf("idade ------ %d\n", al[i]->idade);
		printf("------>valor de i:%d\n\n\n",i);
		printf("\n");
		system("pause");
	}


	fclose(fp);

}

int main()
{
ALUNO n;

escreve(&n);

return 0;

}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.