I wrote a new type of program using a file handling.My idea was to write something to file "pokemon" using a simple program and trying to access it from another simpler program write.cpp TO WRITE ANYTHING TO A FILE "POKEMON"
The first program seems to be working properly but the read.cpp seems to be not.If I enter more than one word it seems to be reading only the first word and displaying it back.I tried to sort the problem but I cant.What am I DOING WRONG.Please help. note::i am using very old compiler turbo c++ v 2 or something so no comments on conio.h or compiler other helps would be appreciatable
Thanks for your time,
Cyber Dude
I did change a couple things, you might try this but I don't think that is the problem. I don't think your write file is working. you might check that.
How to check if my write file is working or not and here is a sample of pokemon.txt
alas èÿÜ nÓ€ Ö "òÿ#"" ,
super imposable Ü nÓ€ Ö "òÿ#"" ,
cool dude sable Ü nÓ€ Ö "òÿ#"" ,
cool awsome ble Ü nÓ€ Ö "òÿ#"" ,
alassius ÿ¥*èÿÜÓ€ ÖÓ€
, "òÿ#"" ,
as far as i remember I inputted 5 words alas,super imposable,cool dude sable cool asome and alassius ......so the writing option seems to be working :-|
When I've tried this in the past, I found it doesn't always work with ISO standard C++ code. Because of that, it may force you to write non-standard code in order to work around its idiosyncrasies. If at all possible, I would strongly recommend using an up to date compiler (though I understand some education courses are based around Turbo C++).
Having said that, the following code worked ok in Turbo C++ 3.0
But I can only repeat the advice to avoid Turbo C++, as you will have to unlearn lots of incorrect things at some stage in the future, so it creates more work and difficulties for you in the long term.
what does memset(ch, 0, 50); on line 13 means is it some kind of fleshing technique.Also my exe file seems to be unstable it works sometimes and sometimes it does not while outputting some of the words it seems to be missing the first letter or sometimes 2nd letter what do u think is that???
I used memset() to clear the contents of the array, sorry I should have explained that. It sets each character in the array to 0, before getting the user input. I did that as it makes the contents of the file cleaner (mainly for cosmetic reasons, it looks better) since the remnants of previous usage will remain in the array. If you miss this line out, the program will still work. http://www.cplusplus.com/reference/cstring/memset/
Personally i like to examine the contents of the file using a hex editor such as HxD or Hexplorer which is more suitable than an ordinary text editor for working with binary files as in this case. If you use memset to clear the contents the file should contain only the actual words which were entered, and the rest will be zeros http://mh-nexus.de/en/hxd/ http://sourceforge.net/projects/hexplorer/
it works sometimes and sometimes it does not
I don't know the answer to that. You'd need give more specific information, state exactly what words you entered, and exactly what output you get.
I don't know the answer to that. You'd need give more specific information, state exactly
what words you entered, and exactly what output you get.
well it sometimes only displays partial letters (that's the best I could say) for eg if I entered say,cool it will display only ool in the output file and if I entered pokemon it only displays back okemon + some ASCII LETTERS Buy if I entered alas it would display back alas. It all depends like my exe file has a mind of it's own.I dont know what to do.I was planning on creating a text based pokemon simulator during vacation and this is what happened.
Since there are two separate programs here, the first step in identifying the cause of the problem is to discover which of the two (or possibly both) is the source of the issue. If the file itself has incorrect contents, then the issue is in the program which writes to the file. On the other hand if the file appears correct, but is displayed wrongly, the problem is in the second program.
eg if I entered say,cool it will display only ool in the output file
Could I ask you to clarify this statement please. When you say "in the output file" do you mean precisely that, in other words the output file itself is incorrect? If so, may I ask for clarification of how you are viewing the contents of the file e.g. do you use an ordinary text editor such as notepad, or have you tried one of the hex editors which I recommended previously?
Or is file itself ok, but the text displayed when it is read back by the second program and displayed on the console incorrect?
Well the write.cpp seems to be working fine.I guess it is the problem with the 2nd program.I downloaded hex editor as you recommended and all the words that I typed in seems to be intact in the file soooo what do you think is the problem.The output file was actually a mis-concept that I typed in. I ment the output console screen AND SORRY PLS HELP IT REALLY MEANS A LOT.I spent 1 month developing the idea
Thanks for the response. So far I don't have any immediate or straightforward answer. It's possible that your compiler is part of the problem. I was able to test the code using Turbo C++ V3.0, with no errors. However it says copyright 1990 and 1992, which means it is easily over 20 years out of date. Seriously, if you intend to spend days developing a project, I'd have to recommend once again that you switch to a more modern compiler, there are lots of free C++ compilers available.
As to your specific problem, I'd recommend several things. First, start with a fresh or empty pokemon file. Enter sufficient data in order to re-create the error. Make a note of exactly what you typed, maybe write it down in a notebook or save it in a separate file where you log the exact steps taken. Then post on this forum a precise description of the exact input you entered, and an exact copy of the output which is displayed on the screen. Also, just in case you've made any changes, post the latest version of your code which gives the errors.
sorry for the delay I was having exams at school.As you adviced I deleted existing pokemon.txt and opened write.txt and entered the following
corrupted
c++
project
and
a pain in the head
so
and the I ran read.cpp and got the following ouputs
corrupted
++
oject
in the head
n the head
the I opened pokemon.txt to check whether everything was written .It seems to be intact
>>>a copy of pokemon.txt<<<
corrupted èÿŒ @ Vû
:òÿ(): J2 c++ upted èÿŒ @ Vû
:òÿ(): J2 project d èÿŒ @ Vû
:òÿ(): J2 and ect d èÿŒ @ Vû
:òÿ(): J2 a pain the head Œ @ Vû
:òÿ(): J2 so ain the head Œ @ Vû
:òÿ(): J2
I have not created any change to write.cpp or read.cpp
Thank you for the detailed reply. This is helpful. When I ran the code exactly as posted with no changes, (and using TC++ 3.0) my output file lookd ok, however I got only the first word "corrupted" as the displayed output from read.cpp.
The main suggestion I have is to make sure the file is opened in binary mode. This is my modified version of read.cpp, see line 21:
I also changed the while loop at line 24. Note that main should be declared with a return type of int, not void (line 16).
You should also add the ios::binary to write.cpp too.
I hope this helps, though I'm deeply uncomfortable in commenting on a turbo c++ project, as it is a quirky and non-standard dialect which can add its own problems and issues. Upgrading to a modern IDE and a modern standards-compliant compiler would still be a primary recommendation.