I am working on a Dictionary project based on AVL tree.
The Dictionary can add words from the input.txt. Here is the input example:

0 3
sheep,con cuu
mother, me
water, nuoc

0 : the code to show that you are about to add words to the Dictionary
3 : the numbers of words you gonna add

The problem I have is that i cant read the numbers from input.txt.
I called A.Read();

cout<<"\n\n";
	DICTIONARY A;
	A.Read();
	
	cout<<"\n";

and the content of Read() is:

fstream file;
	int nCode;
	int nQuantity;
	
	file.open("input.txt",ios::in);
	file>>nCode;
	cout<<"\n"<<nCode<<"\n";
        file.close();

but in the console screen it gives me a rubbish value(attached image)
I also attach my code so it's easy for you to help me out. thnx in advance and sorry for my english :D

Recommended Answers

All 5 Replies

I'm going to make a suggestion. Don't worry about telling us about making a dictionary. If you drag too much stuff out on the floor, its harder for others to figure out what you are having trouble with.

Make a small program (not pieces out of some grand thing you are working on long-term) that demonstrates what you are trying to do.

Tell what the input to the program is (you did that well) and then say what output you got, and what you expected.

It will be almost a day before I can come back and look, but if you do as I suggest I expect someone will help you almost immediately.

Don't be discouraged.

Hey! Right beside your post on the right hand side, there is a heading "Similar Threads". One of the links is a tutorial on fstream. A few of the comments are misleading, but overall its very good, and no waiting required.

Happy reading!

Thnx for replying me, i want to read the '0' number from the file input.txt into 'nCode' but it always gives me the rubbish value.
Anyway, i tried to change the name 'input' to 'in' and it worked. So weird :(:(:(

Oh. I understand much better. I expect things to get even weirder, then.

The character "0" in a (ms dos or windows) text file is actually the number 48. Read as a binary file, the byte representing "0" is 48 decimal. What's different about a "text" file is that the numbers that are in it are interpreted as characters... google "ascii table" and you'll see what I mean. A zero (read as a number) is actually a null, often shown as "". Like the number 48 is translated in ascii to "0", a zero is translated as nothing. It's not even a blank space. The number 32 is a space.

So when a console program is showing a file of numbers AS TEXT, the number 48 prints on the screen as "0" (without the " marks) the number 32 prints an empty space, and a zero prints out as nothing, the screen cursor does not move.

Now, go google "ascii table" and then go to

http://www.datamystic.com/text-binary-files.html

In C, strings are terminated with a null. That may be how that zero got in there, I'm not sure. I am still at home so no compiler here, maybe tomorrow I can try your code during a break. Does your programming environment allow you to single-step through a program?

God bless you; there is a lot of work for you to do.
Keep plugging.

Ah Yeah, i have asked my teacher about this weird trouble and he told me not to save the file input.txt as UNICODE format but ANSI format. That is the point here.

The content of my original 'input.txt' is copied and pasted from my teacher's source(he tried to trap his students hahaha) and the 'in.txt' was typed by myself so it worked.

Anyway, thanks all for helping me out with this problem :D:D:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.