From: Tom St Denis Newsgroups: comp.os.msdos.djgpp Subject: Re: findfirst/findnext woes Date: Tue, 02 Jan 2001 14:13:30 GMT Organization: Deja.com Lines: 108 Message-ID: <92snm5$skf$1@nnrp1.deja.com> References: NNTP-Posting-Host: 24.156.37.224 X-Article-Creation-Date: Tue Jan 02 14:13:30 2001 GMT X-Http-User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows 98) X-Http-Proxy: 1.1 x72.deja.com:80 (Squid/1.1.22) for client 24.156.37.224 X-MyDeja-Info: XMYDJUIDtomstdenis To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com In article , djgpp AT delorie DOT com wrote: > > On Tue, 2 Jan 2001, Tom St Denis wrote: > > > Anyways... My code only finds the first file then findnext() returns > > 1... ALWAYS! > > > > I don't want to flood the group, so if anyone has two mins to spare > > email me at tomstdenis AT yahoo DOT com and I will send you the source to look > > at. > > My advice would be to write the shortest program you can come up with > which uses findfirst and findnext, and still exhibits the problem, and > then post that code here as plain text. > > Experience shows that, while working on the short program, either > (1) you will find the problem in your code; or (2) the resulting > program will be simple enough for someone to spot the problem in > no time. Here is the entire program (minus the bzip lib of course) (one huge main file... the way programs of this caliber should be written... hehehehe) --bz.c-- #include #include #include #include #include #include #include "bzlib.h" int main(int argc, char **argv) { struct ffblk fb; unsigned long f, x, y; unsigned char buf[4096]; if (argc == 1) { printf("%s: [e|d] archive [files]\nE\tEncode files into archive\nD\tDecode files out of archive.\n", argv[0]); return 0; } if (toupper(argv[1][0]) == 'E') { FILE *input; BZFILE *output; /* encode */ output = BZ2_bzopen(argv[2], "wb9"); memset(&fb, 0, sizeof(fb)); f = findfirst(argv[3], &fb, FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_ARCH); while (!f) { /* write the file name and then it's size */ input = fopen(fb.ff_name, "rb"); if (!input) { printf("Can't open: %s\n", fb.ff_name); return -1; } printf("%s\n", fb.ff_name); fseek(input, 0, SEEK_END); x = ftell(input); fseek(input, 0, SEEK_SET); BZ2_bzwrite(output, fb.ff_name, sizeof(fb.ff_name)); BZ2_bzwrite(output, &x, sizeof(x)); do { y = fread(buf, 1, sizeof(buf), input); BZ2_bzwrite(output, buf, y); } while (y == sizeof(buf)); fclose(input); f = findnext(&fb); } BZ2_bzclose(output); } else { /* decode */ BZFILE *input; FILE *output; /* open archive */ input = BZ2_bzopen(argv[2], "rb"); while (BZ2_bzread(input, fb.ff_name, sizeof(fb.ff_name))) { output = fopen(fb.ff_name, "wb"); printf("%s\n", fb.ff_name); BZ2_bzread(input, &x, sizeof(x)); while (x) { /* read at most sizeof(buf) bytes */ y = (x>sizeof(buf))?sizeof(buf):x; x -= y; /* read/write */ BZ2_bzread(input, buf, y); fwrite(buf, 1, y, output); } fclose(output); } BZ2_bzclose(input); } return 0; } --bz.c-- > Sent via Deja.com http://www.deja.com/