reading/writting to file
Blitz3D Forums/Blitz3D Beginners Area/reading/writting to file
| ||
I am trying to write a Blitz app that read and writes to a file. I have played around with the example code that ships with Blitz3D. Blitz does not seem to write standard charracters to a .txt file and it does not seem to read characters from a .txt file as text. So for example if I have the chracter "2" in my tex file and I set my app up to read it into a varaiable then print that to screen it will be displayed as "50". If I put the number 1 in the text file it will be printed to screen via Blitz as 49. How do I get the number that is printed to my screen to be the same as the one the chracter in my text file. Here is the code I am using: ; Open the file to Read filein = ReadFile("myFile.txt") Read1 = ReadInt( filein ) Print "Integer Data Read From File - myFile.txt " Print Read1 WaitKey() Thank you. |
| ||
Do this:; Open the file to Read filein = ReadFile("myFile.txt") Read1 = ReadString( filein ) Print "Integer Data Read From File - myFile.txt " Print Read1 WaitKey() |
| ||
Use readline/writeline. |
| ||
That's because you are using ReadInt() which actually reads in *4* bytes (in other words, 4 characers) which is why you get odd results. An integer requires 4 bytes for storage because of it's size (4,294,967,296 or 2,147,483,648 signed). In order to read in 1 byte, use ReadByte(). 2 bytes is ReadShort(). Or do as the guys suggested above and use ReadLine(). Also have a look at this tutorial I wrote: http://www.blitzbasic.com/Community/posts.php?topic=27896 |
| ||
I appreciate the response from you guys. I have tried all the suggested functions ReadByte(), ReadShort() and ReadLine.ReadString and ReadLine() worked, however ReadByte, ReadShort() and ReadInt() are still not working. Here are a list of functions, the chracter I put in the text file and how Blitz printed it to the screen: ReadByte() textfile Blitz output 4 = 52 ReadShort() textfile Blitz output 12 = 12849 ReadInt textfile Blitz output 4321 = 855373492 Here is the entire app I am using which produces these results the diffrence being that I experimented with swapping the ReadInt()for ReadShort, ReadByte, etc. My txt file was created in notepad and has no spaces in front of the chracter. ; Open the file to Read filein = ReadFile("myFile.txt") Read1 = ReadInt( filein ) Print Read1 WaitKey() I am running a recently downloaded version of Blitz3D: IDE v1.64 Linker v1.64 Runtime v1.64 Oddly enough the tutorial program described in WolRons tutorial is exactly what I wanted to do however I can not get it to Run because of(I think) the above problem. |
| ||
You can't mix and match. Decide what data you're storing and store it. If you write a string and read an int, it's not going to match and vice versa. If you write a string "4" to a file, you're writing a character not a number, and in this case you're encoding an ascii string. Proof : Print Asc("4") If you want to write a number, writeint and readint. If you want to write a string, readline and writeline ( or readstring and writestring. ) |
| ||
A character in a typical .txt file is ALWAYS a string, so you cannot use ReadInt, ReadFloat, ReadByte, etc. The only usable command you can use is ReadLine (AFAIK). ReadInt, ReadFloat, ReadString, ... are commands to read from binairy files (these are very different from normal txt-files), which can be created/modified by a Hex-editor (or by Blitz itself). When you intend to read the character "1" from a file using ReadByte, then you read the ascii-value of that character, which is 49. |
| ||
Thanks for the info about the ascii. I want to set up my app to read data from a user editable text file that will be fed into a variable in Blitz. I suppose I would have to either convert the ascii to an decelmal/interger somehow. Thanks |
| ||
Just use the code in the tutorial I showed you! (or Dr. Av's code, which is linked to in my tutorial). You don't have to convert anything. |
| ||
WonRon do you mind if I email you a few small questions about your tutorial code? |
| ||
Nope. Go ahead. |