user3092390
user3092390

Reputation: 11

Loading File with char pointer, C

so I have this function that is supposed to read a file:

int lerCoordenadasFich( char bairroCoord[ ][ COLUNAS ][ 50 ], int linhas,
                        char *fichIn ) {

   int contaLinhas;
   int contaColunas;
   FILE *fichIn;

   if( ( fichIn = fopen( "bairro-coordenadas.txt", "r" ) ) == NULL ) {
      printf( "Nao foi possivel abrir o ficheiro.\n" );
      return 0;
   }
   else {
      for( contaLinhas=0; contaLinhas < linhas; contaLinhas++) {
         for( contaColunas=0; contaColunas < COLUNAS; contaColunas++) {
            fscanf( fichIn,"%s", bairroCoord[contaLinhas][contaColunas]);
         }
      }

      fclose(fichIn);
      return 1;
   }
}

But I'm getting the error:

'fichIn' redeclared as a different kind of symbol

I really need to have it declared as a char from main, though... How can I solve this?

Upvotes: 0

Views: 99

Answers (4)

Floris
Floris

Reputation: 46365

You say you need to pass the name of the file in - yet you have hard-wired the filename in your code. Addressing both points, you need to change the code as follows (I have introduced a new variable fp and updated the fopen function):

int lerCoordenadasFich( char bairroCoord[ ][ COLUNAS ][ 50 ], int linhas,
                    char *fichIn ) {

int contaLinhas;
int contaColunas;
FILE *fp;

if( ( fp = fopen( fichIn, "r" ) ) == NULL ) {
    printf( "Nao foi possivel abrir o ficheiro.\n" );
    return 0;
   }
   else {
      for( contaLinhas=0; contaLinhas < linhas; contaLinhas++) {
         for( contaColunas=0; contaColunas < COLUNAS; contaColunas++) {
            fscanf( fp, "%s", bairroCoord[contaLinhas][contaColunas]);
         }
      }

      fclose(fp);
      return 1;
   }
}

Upvotes: 0

Wei Shao
Wei Shao

Reputation: 51

Because your function has TWO variables named finchIn. One is located in your function parameters, other one is FILE *fichIn.

gcc -g -o q.o -c q.c
q.c: In function ‘lerCoordenadasFich’:
q.c:10:10: error: ‘fichIn’ redeclared as different kind of symbol
q.c:6:31: note: previous definition of ‘fichIn’ was here

Upvotes: 2

Ed Heal
Ed Heal

Reputation: 59997

You have

int lerCoordenadasFich( char bairroCoord[ ][ COLUNAS ][ 50 ], int linhas,
                        char *fichIn ) {

i.e. fichIn

Then

FILE *fichIn;

i.e. Another one

Rename one of them!

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308111

You do have it declared twice, once as char *fichIn in the argument list and then later as FILE *fichIn. Just rename one or the other.

Upvotes: 2

Related Questions