Prolem Loading Data From File
BlitzMax Forums/BlitzMax Beginners Area/Prolem Loading Data From File
| ||
File = OpenFile(path) If Not file Return line:String = ReadLine File Here is my code - i get a compiling error: "Compile Error:Unable to convert from 'String(TStream)' to 'String' [/BMAX LEARNING/Tile Game/Level Creator.bmx;174;2]" Line 174 is the last line I posted here. The two lines above I thought might be relevant. Thanks in advance for your help, Will. |
| ||
Global line:string Function LoadFile(path:string) Local file:Int file = ReadFile(path) While Not Eof(file) line:+ ReadLine(file) Wend CloseFile file End Function This will load a normal text file and fill the 'line' string variable with that file. Feel free to adapt it to your purpose. I think you only need to put brackets around you ReadLine statement to get it to work. Hope it helps |
| ||
ok, i'm not sure why but it seems like "ReadLine(path)" works and not "Readline file" How do I get an integer of float out of my file? xtiles:Int = ReadLine(File) 'WriteLine File, map.xtiles I get the error: "Compile Error:Unable to convert from 'String' to 'Int' [/BMAX LEARNING/Tile Game/Level Creator.bmx;187;2] Build Error: failed to compile /BMAX LEARNING/Tile Game/Level Creator.bmx" any ideas? |
| ||
ReadInt(file) ReadFloat(file) Look in the 'Command->Streams' section in the help for lots more. Most commands in Blitz that are going to recieve some data use the brackets around the parameter that is going to recieve that data. Blitzbasic and Blitz3d have done this for years. You'll get used to it and it makes sense after a while. There was a post about this quite some time ago but I can't find it. Sorry. Regards |
| ||
If you're returning a value from a function, you need brackets around its parameters. |
| ||
Thanks for the brackets heads up! Thats really important! Additionally, its still not working: I am using readInt(File) to return an integer, here, firstoff, is the beginning of my file: Tilemap: Tilemap X Tile Count 25 Tilemap Y Tile Count 25 Tilemap X Tile Size 24 Tilemap Y Tile Size 24 Tile Information Follows: Number of tile attributes: 10 This is the code I am using to read it: Print ReadLine(File) 'WriteLine File, "Level Creator File" Print ReadLine(File) 'WriteLine File, "Tilemap:" Print ReadLine(File) 'WriteLine File, "Tilemap X Tile Count:" Print Readint(file) 'WriteLine File, map.xtiles Print ReadLine(File) 'WriteLine File, "Tilemap Y Tile Count:" Print Readint(file) 'WriteLine File, map.ytiles Print ReadLine(File) 'WriteLine File, "Tilemap X Tile Size" Print Readint(file) 'WriteLine File, map.tilewidth Print ReadLine(File) 'WriteLine File, "Tilemap Y Tile Size" Print Readint(file) 'WriteLine File, map.tileheight Print ReadLine(File) 'WriteLine File, "Tile Information Follows:" Print ReadLine(File) 'WriteLine File, "Number of tile attributes:" Print Readint(file) 'WriteLine File, TileAttributeCount" And THIS is what it outputs: Tilemap: Tilemap X Tile Count 25 1416195173 map Y Tile Count 842337546 Tilemap X Tile Size 842272010 Tilemap Y Tile Size 842272010 Tile Information Follows: Number of tile attributes: 825232650 |
| ||
ReadInt is for reading binary files, you can cast the string you get from readline to an int by replacing each of your =ReadInt(file) lines with =int(ReadLine(file)) |
| ||
Or use WriteInt instead of WriteLine coz your are trying to 'WriteLine' to the file and 'ReadInt' back from the file!! It doesn't matter to mix and match text and binary in the same file, you just need to read it back in correctly. |
| ||
But if he mixes WriteLine and WriteInt when creating the file then it can no longer be viewed or changed with a text editor. A plain text file should have all data, including numbers, written with WriteLine. The same data is read back with ReadLine. This will produce string values, such as "25" for the number 25. As mentioned, such strings can be cast to integer with Int( YourStringValue$ ). Note that previous versions of Blitz would do this cast automatically when assigning a string value to an integer variable: myInteger = "25" This would set myInteger to the numeric value 25. But BlitzMax does not do this. You must explicitly use Int() to cast a string to an integer. |
| ||
You are 100% correct. That will work. BUT Why would you need a text editor when you are using a program to write and read the file?? only valid reason I could think of, I suppose, is in a 'learning to trust your code' phase. |
| ||
Because you may want to make a small change to the file, perhaps changing to 25 to 26. Or you may want to allow people to 'configure' the program with a text file. They will not need source code or BlitzMax. |