By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,349 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,349 IT Pros & Developers. It's quick & easy.

passing more than one file.

P: n/a
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
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;
}

Nov 9 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ni******@gmail.com 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
I don't think you should modify it to work on multiple files until
you have it working, which it clearly doesn't, since you've referred
to several undeclared identifiers and forgotten to #include <stdio.h>.

You should read a decent introductory C book, with special reference
to the arguments to `main` and your choice of `for` or `while` loops.
Start with a proglette which displays its command line arguments on
the standard output.

--
Chris ".enable proofreading" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Nov 9 '06 #2

P: n/a
oh this is just an extract from code given to me. its a homework
assignment and im stuck.

this is it all.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include "tree.h"

/************************************************** *****************************
* Global variable for storing all the text
* "last_word_end" marks the first free space
*/

#define MAXSTRING (100 * 1024 * 1024)
char info[MAXSTRING];
char *last_word_end = info;
/************************************************** *****************************
* Global variable for the word tree
*/

Tree word_tree = NULL;
/************************************************** *****************************
* Information kept on each word
* a pointer to the address in storage
* and the length of the word instance
*/

typedef
struct {
char *word;
int length;
} WordS, *Word;
/************************************************** *****************************
* Comparing word blocks
* this give case-independent alphabetical order
*/
int compareWords (Word alpha, Word beta) {
int min = alpha->length <= beta->length ? alpha->length :
beta->length;

int alpha_cmp = strncasecmp(alpha->word, beta->word, min);

if (0 != alpha_cmp) {
return alpha_cmp;
} else {
return alpha->length - beta->length;
}
}

/************************************************** *****************************
* adding a word item to the tree
*/

void addWord (char *word) {
Word w = malloc(sizeof(WordS));

w->length = strlen(word);
w->word = last_word_end;
strncpy(last_word_end, word, w->length);
last_word_end += w->length;
strncpy(last_word_end, " ", 1);
++last_word_end;

word_tree = insert((Comparator)compareWords, word_tree, (void *)w);
return;
}

/************************************************** *****************************
* reading in a file
*/
#define MAXLINE 256
// maximum size for lines read in

#define SEPARATORS " \t\n\r-:;,.!?()%\"'"
// possible non-word characters

void readFile(FILE *f) {
char buf[MAXLINE];

while (fgets(buf, MAXLINE, f)) {
// for current line
char *w = strtok(buf, SEPARATORS);
if (w) {
addWord(w);
}
while (w = strtok(NULL, SEPARATORS)) {
addWord(w);
}
}
}

/************************************************** *****************************
* showEntry
* show a line in the KWIC format
* assumes caller will free this memory
* shows some context either side of the target word
*/

#define SCREEN_WIDTH 80

char *showEntry(Word w) {
char *p = (char *)malloc(SCREEN_WIDTH);
char *q;
q = w->word - 7 * SCREEN_WIDTH / 16;
if (q < info) { q = info; } // question for you: why?
strncpy(p, q, SCREEN_WIDTH - 1);
p[SCREEN_WIDTH - 1] = '\0';
return p;
}
/************************************************** *****************************
* showEntry
* show a line in the KWIC format
* assumes caller will free this memory
* shows some context
*/
int main(int argc, char *argv[]) {
FILE *f;
if (1 == argc) {
f = stdin;
} else {
f = fopen(argv[i], "r");
if (! f) {
perror(argv[i]);
fprintf( stderr
, "Couldn't read file %s, dropping this file\n"
, argv[i]);
return errno;

}
}

readFile(f);
printInOrder((Displayer)showEntry,word_tree);
return 0;
}

Nov 9 '06 #3

P: n/a
On 2006-11-09, ni******@gmail.com <ni******@gmail.comwrote:
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.

Nov 9 '06 #4

P: n/a
On 2006-11-09, ni******@gmail.com <ni******@gmail.comwrote:
oh this is just an extract from code given to me. its a homework
assignment and im stuck.

this is it all.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include "tree.h"

/************************************************** *******
* Global variable for storing all the text
* "last_word_end" marks the first free space
*/

#define MAXSTRING (100 * 1024 * 1024)
char info[MAXSTRING];
char *last_word_end = info;
1. Don't use globals.
2. You can't statically allocate 100 megs. Use malloc(),
and use a reasonable number. If you insist on using a
fixed buffer, try 1024.
>
/************************************************** *******
* Global variable for the word tree
*/

Tree word_tree = NULL;
/************************************************** *******
* Information kept on each word
* a pointer to the address in storage
* and the length of the word instance
*/

typedef
struct {
char *word;
int length;
} WordS, *Word;
Don't typedef pointers. It's confusing as hell.
>
/************************************************** *******
* Comparing word blocks
* this give case-independent alphabetical order
*/
int compareWords (Word alpha, Word beta) {
int min = alpha->length <= beta->length ? alpha->length :
beta->length;

int alpha_cmp = strncasecmp(alpha->word, beta->word, min);
Why microoptimise with silly things like min here. Also,
strncasecmp() is a non-standard function, and shouldn't
be declared in any of the headers you included.
if (0 != alpha_cmp) {
return alpha_cmp;
} else {
return alpha->length - beta->length;
}
You used two variables in this function that you didn't need.
}

/************************************************** *******
* adding a word item to the tree
*/

void addWord (char *word) {
Word w = malloc(sizeof(WordS));
Uh... what? Oh, right, Word means a pointer. Hardly intuitive,
especially as you're malloc()ing the size of a different type!

[snip remaining unreadable code]
Nov 9 '06 #5

P: n/a
ni******@gmail.com 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

int main(int argc, char *argv[]) {
FILE *f;
int i;

if (1 == argc) {
f = stdin;
} else {

f = fopen(argv[1], "r");
[...]

I usually write a separate function which works on the filename
passed, and then loop through argv:

int main(int argc,char *argv[])
{
if ( argc == 1 )
DoIt(NULL);
else
{
while ( --argc )
DoIt(*++argv);
}
return(EXIT_SUCCESS);
}

void DoIt(char *name)
{
FILE *f;
if ( name == NULL )
f = stdin;
else
{
f = fopen(name,"r");
if ( f == NULL )
{
/* handle error here */
return;
}
}
/* handle the file here */
fclose(f);
}

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.