C - ERROR not defined and storage unknown

505 Views Asked by At

I have this code for deleting directories. I have header1.h, header1.c, main.c. I get some errors, but the one is more difficult to me to understand is the errors: (1) storage size of ffblk isn't known. Also, i have the doubt of how to define the attributes of ffblk, which are ff_name and ff_attrib This example is from various code examples from the internet(even other from here), all of them do the same code, just in my case it does not work. Am i missing the definition of the struct? or maybe i have added code where it should not be any code? Could you please help me? I dont usually program in C. and i am using Dev-Cpp.

header1.h:

#ifndef HEADER1_H_INCLUDED
#define HEADER1_H_INCLUDED

typedef struct ffblk ffblk;

#endif

header.c:

#include <stdio.h>
#include <stdlib.h>
#include "header1.h"
struct ffblk
{
    char ff_attrib[20];  //this line i added just so dont show error of unknown
    char ff_name[20];  //this line i added just so dont show error of unknown
};

main.c:

#ifndef FA_RDONLY
  #define FA_RDONLY _A_RDONLY
 #endif

#ifndef FA_HIDDEN
  #define FA_HIDDEN _A_HIDDEN
 #endif

 #ifndef FA_SYSTEM
  #define FA_SYSTEM _A_SYSTEM
 #endif

 #ifndef FA_DIREC
  #define FA_DIREC _A_SUBDIR
 #endif

 #ifndef FA_ARCH
  #define FA_ARCH _A_ARCH
 #endif


# include <stdio.h>
# include <dos.h>
# include <dir.h>
# include <io.h>
# include <conio.h>
#include "header1.h"

    typedef struct ffblk ffblk;

int BorrarArchivo(char *nombarch)
{
   printf("Borrando Archivo %s \n",nombarch);

   remove(nombarch);

   return 0;
}

int EliminarAtributo(char *nombarch,int atributo)
{
   printf("Elimina Atributo %s   %d\n",nombarch,atributo);

   chmod(nombarch,atributo);

   return 0;
}

int BorrarArbol(void)
{  
   struct ffblk ffblk;
   int done,err;

   err=0;
   done=findfirst("*.*",&ffblk,FA_RDONLY|FA_HIDDEN|FA_DIREC|FA_ARCH|FA_SYSTEM);

   while (!done)
   {
     if (FA_HIDDEN & ffblk.ff_attrib)
         EliminarAtributo(ffblk.ff_name,FA_HIDDEN);

     if (FA_SYSTEM & ffblk.ff_attrib)
         EliminarAtributo(ffblk.ff_name,FA_SYSTEM);

     if (FA_RDONLY & ffblk.ff_attrib)
         EliminarAtributo(ffblk.ff_name,FA_RDONLY);

     if (FA_ARCH & ffblk.ff_attrib)
         err=BorrarArchivo(ffblk.ff_name);
     else if (FA_DIREC & ffblk.ff_attrib)
     {
        if (ffblk.ff_name[0]!='.')
        { 
            chdir(ffblk.ff_name);

            err=BorrarArbol();

            chdir("..");

            if (!err)  
                printf("Removiendo %s\n",ffblk.ff_name);

            rmdir(ffblk.ff_name);
        }
     }
     else
        err=BorrarArchivo(ffblk.ff_name);

     if (err)
     {  
        printf("Error en el borrado ... !"); return err;
     }

     done=findnext(&ffblk);
    }

   return 0;
}

int main (void)
{ int err=0;
  char c;
    printf("Esta seguro  [ Si -> S , No ->otra tecla ] =>");
    c=getchar();
    if (!(c=='S' || c=='s')) return 0;
    err=BorrarArbol();
    if (err) printf("Error en el borrado ... !");
    return err;
}

EDIT: I found a definition of struct and paste it in header.h

typedef struct ffblk {
      char lfn_magic[6];        /* LFN: the magic "LFN32" signature */
      short lfn_handle;     /* LFN: the handle used by findfirst/findnext */
      unsigned short lfn_ctime; /* LFN: file creation time */
      unsigned short lfn_cdate; /* LFN: file creation date */
      unsigned short lfn_atime; /* LFN: file last access time (usually 0) */
      unsigned short lfn_adate; /* LFN: file last access date */
      char ff_reserved[5];      /* used to hold the state of the search */
      unsigned char ff_attrib;  /* actual attributes of the file found */
      unsigned short ff_ftime;  /* hours:5, minutes:6, (seconds/2):5 */
      unsigned short ff_fdate;  /* (year-1980):7, month:4, day:5 */
      unsigned long ff_fsize;   /* size of file */
      char ff_name[260];        /* name of file as ASCIIZ string */
    }ffblk;

now it shows error:

C:\Users\1\AppData\Local\Temp\cc89P309.o:Untitled3.c:(.text+0x8e): undefined reference to `findfirst'
C:\Users\1\AppData\Local\Temp\cc89P309.o:Untitled3.c:(.text+0x1d8): undefined reference to `findnext'
1

There are 1 best solutions below

0
On BEST ANSWER

At the end, the problem was always the functions findfirst(a,b,c) and findnext(a,b,c). I had to change the functions and use their version with 2 parameters and not 3. I put all the code in one file with the includes, typedef struct and the code below.