Re: passing more than one file.
- From: apoelstra@xxxxxxxxxxxxxxxxxxxxx
- Date: Thu, 09 Nov 2006 15:12:44 GMT
On 2006-11-09, nickm687@xxxxxxxxx <nickm687@xxxxxxxxx> wrote:
Hi i have this code below and i need to modify it so i can pass more
than one text file to it. any ideas? i am new to this. thanks in
advance.
Nick
Well, start by doing some design. What do you want to happen with
multiple files? (What I assume is that you want each one to be
printed off sequentially.) What happens when the filelist ends?
(What you have is that if the filelist before it starts, use
stdin.)
So, start with what you've got. Make the first filename a special
case because its absence is handled differently.
Then, loop through argv[] until you hit NULL. (A useful thing
that might not have been mentioned in class is that the last
element of argv[] is guaranteed to be NULL.) With each file,
try to open and do what you want with it.
Also, when you can't open a file, don't bomb. Print a warning
message to stderr and keep going. If no files are capable of
being opened, then you can return an error value. The only
portable value is EXIT_FAILURE, defined in <stdlib.h>. Anything
else may work on your system, but could cause problems on
others.
int main(int argc, char *argv[]) {
FILE *f;
int i;
if (1 == argc) {
f = stdin;
} else {
f = fopen(argv[1], "r");
if (! f) {
perror(argv[1]);
fprintf( stderr
, "Couldn't read file %s, dropping this file\n"
, argv[1]);
return errno;
}
}
readFile(f);
printInOrder((Displayer)showEntry,word_tree);
return 0;
}
You posted your complete code elsethread, so I'll comment over there
on such things as, say, that cast that statistically should not be
present.
.
- References:
- passing more than one file.
- From: nickm687
- passing more than one file.
- Prev by Date: Re: 2D array of structures
- Next by Date: Re: FILE argument to a function
- Previous by thread: Re: passing more than one file.
- Next by thread: Re: passing more than one file.
- Index(es):
Relevant Pages
|
|