Quite a few issues here.
The first parameter passed to cabclh
is of the wrong type:
void cabclh(ALUNO alunos[],int a);
: :
int aluno;
cabclh(aluno,b);
You should probably exit the function (or some other error handling) if you can't open the file:
if (fp==NULL){
printf("Erro ao abrir o ficheiro\n");
return; // <- Added
}
There's no need to open the file a hundred times. If a regular file doesn't open the first time, it probably won't open at all (although there are cases where this may happen). This particular segment will result in wasted file handles:
for(i=0;i<100;i++){
fp=fopen("trabalho.txt","r");
}
In addition it will reset the file pointer to the start of the file each time.
If your intent is to read up to 100 items from that file for storage into your array, I would suggest you start with:
#include <stdio.h>
#include <stdlib.h>
typedef struct aluno{
char cabecalho[60];
char info[100];
int n_alunos;
char dados[100];
char curso[100];
int numero;
char nome[100];
char e_mail[100];
int n_disciplinas;
int nota;
} ALUNO;
void cabclh (ALUNO alunos[]) {
FILE *fp;
int i;
// Initialise all elements to indicate no data.
for (i = 0; i < 100; i++)
alunos[i].cabecalho[0] = '\0';
// Open the file, returning if not there.
fp = fopen ("trabalho.txt","r");
if (fp == NULL) {
printf("Erro ao abrir o ficheiro\n");
return;
}
// Only allow up to 100 elements.
for (i = 0; i < 100; i++) {
// Only read and load if more to go.
if (!feof(fp)) {
// Read the line and strip off newline character.
fgets (alunos[i].cabecalho,60,fp);
if (alunos[i].cabecalho[strlen(alunos[i].cabecalho)-1] == '\n')
alunos[i].cabecalho[strlen(alunos[i].cabecalho)-1] = '\0';
printf ("%s\n", alunos[i].cabecalho);
}
}
// Close the file.
fclose (fp);
}
int main (int argc, char *argv[]) {
ALUNO alunos[100];
cabclh(alunos);
system("PAUSE");
return 0;
}
It successfully reads a test file I created. Now it may be that your input file is more complicated that just 100 strings to be loaded into cabecelho
but the code above is a good start, showing the controlling logic. Ad different line format would only change the way each line is read, not the loop around it.
And, if you want to be able to handle arbitrary numbers of lines, I would move away from arrays to more expandable data structures. But, for a first attempt, you're making the right choice keeping it simple.