Solved

Input/Output

Posted on 1998-05-29
5
184 Views
Last Modified: 2012-05-04
To Todd, if (available)

I'm trying to read a file into a tree which holds words only.  The tree places these words in dictionary order through out the tree.

When reading in a file I want to use all non letter characters as delimiters for the strtok function to use as such:

char delim[] = " \n\t`~1!2@3#4$5%6^7&8*9(0)-_=+[{]}\|;:'\",<.>/?";
char *token;

if((token = strtok(buff, delim))!=0)
strlwr(token);
insertWord(token);

My questions are:

1.  Am I declaring white space properly in delim[], it's the first char I define by simply pressing the space bar.

2.  Since I don't want any leading white space before words do I need a stripper routine to eliminate spaces, shouldn't the above routine do the trick for that?

3.  As it stands now, the reading of the file into the tree works ok, but I notice it is missing some words.  I feel it may be due to the way I've declared a carriage return in delim [].  Has it been declared correctly (\n) and do you notice any character I'm forgetting in delim[]?

Thanks!

0
Comment
Question by:John500
  • 3
5 Comments
 
LVL 4

Accepted Solution

by:
sganta earned 40 total points
ID: 1164933
Hai John !
? mark should be identified by using \?
\ mark should be identified by using \\
/mark should be identified by using \/
So delim should be
char delim[] = " \n\t`~1!2@3#4$5%6^7&8*9(0)-_=+[{]}|;:'\",<.>\/\\\?";

Try using this, I hope this will work for you.
JESUS LOVES YOU - sganta
0
 

Author Comment

by:John500
ID: 1164934
Hey sganta,

The new delim[] you gave me cleared up a lot of problems.  When I stepped through the file, line by line, to see if all the words are being added, I noticed that a few different words get into the tree but not in the order I expected.  One word in particular is "initialize."  I tried to insert a little code to see what the count is of that word but its not working.  Any suggestions on the routine below.  I am sure that the counter is working properly because I've printed all the counts out but I want a routine where I can choose certain words - less work on the eyes during the printout.

char initialize;
char *q = &initialize;
    if(strcmp(p->_word,q)==0)
      {cout<<p->_word<<" "<<p->_count<<"\n";
       return;}

>JESUS LOVES YOU - This is true
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164935
    char *q ="initialize";
         if(strcmp(p->_word,q)==0)
     {cout<<p->_word<<" "<<p->_count<<"\n";
     return;}

or

         if(strcmp(p->_word,"initialize")==0)
     {cout<<p->_word<<" "<<p->_count<<"\n";
     return;}
0
 
LVL 4

Expert Comment

by:sganta
ID: 1164936
Hai John !

I 've seen your comment, But one thing I did'nt understand what you are doing here
char initialize; /* What is the value you are assigning to initialize */
      char *q = &initialize; /* Here you are assigning the initialize, but where is the value
                                       of q which is not assigned */
          if(strcmp(p->_word,q)==0)
      {cout<<p->_word<<" "<<p->_count<<"\n";
      return;}

------- If you want to compare with "initialize" word then do the following code.
      char *q = "initialize";
          if(strcmp(p->_word,q)==0)
      {cout<<p->_word<<" "<<p->_count<<"\n";
      return;}

If not , Can you explain me what exactly you want ?, with the code example written by you.
If you don't want to hide your code to others, Then send it to my EMail
sganta@ch.oracle.com
I hope I will be able to help you. Thank you.
The Grace of Lord will always be with us.
0
 
LVL 4

Expert Comment

by:sganta
ID: 1164937
Hai John !
That's what I have mentioned in my earlier comment
char *q = "initialize";
  if(strcmp(p->_word,q)==0)
            {cout<<p->_word<<" "<<p->_count<<"\n";
            return;}
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question