[comp.sys.ibm.pc] TC2: problem filling array of struct w/ findnext

burleigh@cica.cica.indiana.edu (Frank Burleigh) (10/04/89)

once again i need to draw on net.experience.

this code:

#include    <dir.h>
#include    <dos.h>

struct ffblk file_list[20];
int fnd = 0;

fnd = findfirst( "*.*", file_list+0, 0 );
for( i = 1; fnd != -1; i++ )
  fnd = findnext( file_list+i );

does not work right, and i have no doubt it is because i am not
properly referencing the addresses of the file_list array of
struct.  the intent is simply to have findfirst/findnext put a
list of file into the file_list[i].  instead, the first file goes
in correctly, but then we drop out of the for loop with fnd = -1,
meaning either the filespec is bad or we've exhausted the dir.  
this should not be happening.

if we un-array file_list and put a printf file_list.ff_name behind
the findnext, things work as expected.

this is a vastly stripped down version of the code.

i would very much appreciate any e-mail insights into my errors.
thank you muchly.

btw, this is tc2.0 and pc dos 4.01.

-- 
Frank Burleigh  burleigh@cica.cica.indiana.edu
USENET: ...rutgers!iuvax!cica!burleigh BITNET: BURLEIGH@IUBACS.BITNET
Department of Sociology, Indiana University, Bloomington, Indiana 47405

Ralf.Brown@B.GP.CS.CMU.EDU (10/05/89)

In article <154@cica.cica.indiana.edu>, burleigh@cica.cica.indiana.edu (Frank Burleigh) wrote:
 >struct ffblk file_list[20];
 >int fnd = 0;
 >
 >fnd = findfirst( "*.*", file_list+0, 0 );
 >for( i = 1; fnd != -1; i++ )
 >  fnd = findnext( file_list+i );
 >
 >does not work right, and i have no doubt it is because i am not
 >properly referencing the addresses of the file_list array of
 >struct.  the intent is simply to have findfirst/findnext put a
 >list of file into the file_list[i].  instead, the first file goes
 >in correctly, but then we drop out of the for loop with fnd = -1,
 >meaning either the filespec is bad or we've exhausted the dir.  
 >this should not be happening.

Yes, it should.  DOS initializes the reserved fields of the ffblk on
calling findfirst(), so that it knows where to continue searching when
you do a findnext().  To make a list, you need something like the
following:

struct ffblk ffb ;
char file_list[20][14] ;
int fnd ;
int count = 0 ;

fnd = findfirst( "*.*", &ffb, 0 ) ;
while (fnd != -1 && count < 20)
   {
   strcpy(file_list[count++],ffb.ff_name) ;
   fnd = findnext(&ffb) ;
   }

--
UUCP: {ucbvax,harvard}!cs.cmu.edu!ralf -=-=-=-=- Voice: (412) 268-3053 (school)
ARPA: ralf@cs.cmu.edu  BIT: ralf%cs.cmu.edu@CMUCCVMA  FIDO: Ralf Brown 1:129/46
FAX: available on request                      Disclaimer? I claimed something?
"Common sense is the collection of prejudices acquired by age eighteen"
	-- Albert Einstein

thorp@spudge.UUCP (Don Thorp) (10/06/89)

In article <154@cica.cica.indiana.edu> burleigh@cica.cica.indiana.edu (Frank Burleigh) writes:
>once again i need to draw on net.experience.
>
>this code:
>
>#include    <dir.h>
>#include    <dos.h>
>
>struct ffblk file_list[20];
>int fnd = 0;
>
>fnd = findfirst( "*.*", file_list+0, 0 );
>for( i = 1; fnd != -1; i++ )
>  fnd = findnext( file_list+i );
>
>does not work right, and i have no doubt it is because i am not
>thank you muchly.
>
>btw, this is tc2.0 and pc dos 4.01.
>
>-- 
>Frank Burleigh  burleigh@cica.cica.indiana.edu
>USENET: ...rutgers!iuvax!cica!burleigh BITNET: BURLEIGH@IUBACS.BITNET
>Department of Sociology, Indiana University, Bloomington, Indiana 47405

I haven't tested the following code, but I'm pretty sure that it will do
the job.

#include <dos.h>
#include <dir.h>

struct ffblk file_list[20];
struct ffblk tmpf;
int i;

if(findfirst("*.*",&tmpf,0) == 0) {
  file_list[0] = tmpf;  /* Save the directory structure */
  for(i=1;i < 20; i++) {
    if(findnext(&tmpf) == -1)
      break;
    file_list[i] = tmpf;   /* structure copy non-K&R */
  }
}

The reason the code didn't work, was that findnext expects the same structure
to be passed to it until no more files are found.  findfirst sets up the 
struct ffblk with information that findnext needs to find the next file.
You were passing an "uninitialized" struct ffblk* to findnext so it failed.

ps: you would have also run passed the end of the array if it had worked.

Good Luck

Don Thorp
USENET : ...!texbell!letni!rwsys!spudge!thorp