Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get content of char

Status
Not open for further replies.

hecktic

Programmer
May 14, 2002
84
CA
Hi,

I have a program that must read from a file and insert each line into a binary search tree.

I've already written all needed code to read from file and insert into the tree. My problem is that when i get the data from the file and insert into the tree, the data is not inserted.. but it's the address of the char that is there.

Any thoughts about this please?

void readFile(const char *fileName, BST myBST)
{
ifstream inFile; //file object
char temp[10];

inFile.open(fileName, ios::in); //open input file

if (!inFile) {
cerr << &quot;Cannot open input file '&quot; << fileName;
exit(1);
}

while (!inFile.eof()) {
inFile >> temp;
cout << temp << endl;
BSTusers.insert(temp); //PROBLEM IS HERE
}
}

I'm not sure how to retrieve the contents of the char array.
Note that the &quot;cout << temp&quot; prints out the correct data and when i do BSTusers.insert(&quot;hello&quot;), the string 'hello' is inserted just fine.

Any help would be much appreciated.
Thanks!
 
myBST = BSTusers, typo sorry. Not the cause of my problem.
 
>BSTusers.insert(temp); //PROBLEM IS HERE

Good. You know where the problem is. Set a breakpoint and step down into the execution to see where it goes wrong.

/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
hecktic, your problems begins at 1st statement in main loop body: inFile >> temp. This input operator does not know about buffer (temp) size (10 bytes). Get 10-chars word (plus zero byte terminator) and your program stack will be crashed. The classic case of uncontrolled buffer overflow.
Use cin.get(temp,sizeof temp) instead or (better) rewrite the code with STL string class.

As PerfNurt said, we can't see your 2nd (main) problem. We don't know what BSTusers.insert() do with its C-string ptr arg. May suppose that you save (incorrect i.e. local) pointer to temp array into tree nodes. If so, you must rewrite node class (for example, use strdup for string copy and don't forget to free it in destructor).

There is the 3rd problem in this kind of work. Simple binary tree is prone to degenerate into an ordinary (and very long) list (suppose your data are alphabetically ordered). Use AVL trees or another (sub)balanced trees - but it's the other story...
 
Ok i'll try to look into it when i get home.

Rewriting node class is not an option.. i'd say that's my main problem :). This inserting in a bst thing is for an assignment... i HAVE to use the code given, cannot write my own code to implement the BST. And the bst is a class that inherits from another class... passing along all kinds of useless crap.

Anyways, enough ranting.

Can I use cin.get(...) for input from file?!

I'll try it out later.

Thanks for the suggestions though!
 
I wrote cin.get() for example only. any_file_stream.get() is common case.
It's a question how your node class inserts its string arg. If it make copy of inserted string - that's OK, no need to rewrite code, you must resolve 1st (buffer size) problem only. If it save pointer to arg only - you must build a dynamic (heap) copy of temp buffer. I hope the node class makes its own copy.
But how about possibly node class restriction on inserted string size?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top