tags:

views:

153

answers:

6
 #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[],int a){
   FILE *fp;
   int i;
   for(i=0;i<100;i++){
      fp=fopen("trabalho.txt","r");

       if(fp==NULL){
           printf("Erro ao abrir o ficheiro\n");
       }
       while(!feof(fp)){
           fgets(alunos[i].cabecalho,60,fp);
           printf("%s\n",alunos[i].cabecalho);
       }
    }
    fclose(fp);
}

what is wrong here?

main:

int main(int argc, char *argv[]){    
   ALUNO alunos[100];  
   int aluno;  
   int b;

   cabclh(aluno,b);

   system("PAUSE"); 
   return 0
+1  A: 

It looks like you're opening the file 100 times, then using a while loop with [i] that's never changing. i = 100 because it's never changed inside your while loop.

John Boker
There also seems to be an extra closing brace.
Javier Badia
That seems to be more of an indent problem, guys. The whole thing is inside the for statement including, erroneously, the fopen.
paxdiablo
@paxdiablo it is now, it wasn't before.
John Boker
+1  A: 

I see at least one thing wrong:

char cabecalho[60];

// ... and later ...

fgets(alunos[i].cabecalho,100,fp);

Last I checked, 100 is bigger than 60, so you have a buffer overflow error.

James McNellis
steals the same
Pedro
+1  A: 

I don't understand why are you doing an fopen 100 times:

for(i=0;i<100;i++){
 fp=fopen("trabalho.txt","r");
 }

Just do:

 fp=fopen("trabalho.txt","r");
codaddict
+1  A: 

Besides all the other errors, you have an extra brace above the fclose(fp), which is likely causing a compilation issue due to unmatched braces.

Mark E
+2  A: 

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.

paxdiablo
all fixed, but appears in the console "<null>", and then crash
Pedro
Can `fgets` return a string of length zero? If so, you'd need to check that before using `strlen(...) - 1` as an index.
James McNellis
@James, not for a normal line, no. It will always have a newline. You may get a zero-length strings on an error condition but I prefer not to clog up educational code to the point where it's 80% error checking :-) The intent here is to teach how the code for normal cases can work, error checking (while vital to have in robust code) would only complicate that at this stage (IMNSHO).
paxdiablo
Try the new code, @Pedro, it's tested successfully.
paxdiablo
Tested!! Working fine xD... but why the cicle "for"? why it needs to run 100 times
Pedro
@Pedro, that's because it reads up to 100 lines from the file. The first loop initialises the strings to empty. The seconds reads lines up to 100 or to end of file, whichever comes first.
paxdiablo
+1  A: 
void cabclh(ALUNO alunos[],int a){
   FILE *fp;
   int i=0;

   fp=fopen("trabalho.txt","r");

   if(fp==NULL){
       printf("Erro ao abrir o ficheiro\n");
       return;     
   }
   while(!feof(fp) && i<100){
       fgets(alunos[i].cabecalho,60,fp);
       printf("%s\n",alunos[i++].cabecalho);
   }

fclose(fp);
}
Jujjuru
on the console appears "<null>"
Pedro