readint problem
BlitzMax Forums/BlitzMax Programming/readint problem
| ||
Hi I have some integers writen in a file. the file looks like this: "64 48 17 206 142 214" I want to read every integer using read int but I get some funky numbers instead of those. Local width:Int = ReadInt(inifile) Local height:Int = ReadInt(inifile) This is a bit of the code used, just to give you an ideea. |
| ||
ReadInt() is for Binary reads. You are using a Text based file (Ascii). You will need to read them in sequentially and then parse them out based upon the spaces. |
| ||
It looks the format of your file is TEXT, so ReadInt() and the like wont work. You need to do a ReadLine() feks, and then parse the string yourself. |
| ||
is there a way to write binary? |
| ||
Yes. Let me finish the sample for reading text for you first... Then I'll scratch out a sample for Binary. |
| ||
Sanctus: Here's the code to read text. I added a few things like streams, and arrays, but it should be very easy to follow. Function Read:String[](file:String) ' Reads a TEXT file then returns a string array Local i:TStream = ReadFile(file) Local rawdata:String Local data:String[] If Not i = Null Then If Not Eof(i) rawdata = ReadLine(i) data = Split(rawdata , " ") End If CloseFile(i) End If Return data End Function Function Split:String[](source:String, delimiter:String) ' Splits the data apart based on the delimiter specified Local i:Int Local array:String[1] Local value:String Local arrI:Int = -1 Local temp:String = source Repeat i = Instr(temp , delimiter) arrI :+ 1 array = array[..arrI + 1] If i > 0 Then ' In string array[arrI] = Mid(temp , 1 , i - 1) temp = Mid(temp, i + 1, Len(temp)) Else ' Equal string array[arrI] = temp End If Until i = 0 Return array End Function Function Main() Local intLoop:Int = 0 Local aryData:String[] Try Print "Reading from file..." aryData = Read("test.txt") Print "Data found, reporting..." For intLoop = 0 To aryData.Length - 1 Print "Position " + intLoop + ": " + aryData[intLoop] Next Catch ex:Object Print "Error occured: " + ex.ToString() End Try End Function Main() |
| ||
Wow man thx! |
| ||
Aight... I finally mustered up the time to squeak this one out for you. This one is a bit advanced, as it uses banks and streams with arrays, BUT... It does read and write binary.Function ReadBinary:String[](path:String) Local i:TStream = ReadStream(path) Local bank:TBank = New TBank Local bankstream:TBankStream = CreateBankStream(bank) Local aryData:String[] Local intI:Int = -1 CopyStream(i, bankstream, StreamSize(i)) CloseStream(i) SeekStream(bankstream, 0) While Not Eof(bankstream) ' Read in the binary intI :+ 1 aryData = aryData[..intI + 1] aryData[intI] = ReadInt(bankstream) Wend CloseStream(bankstream) Return aryData End Function Function WriteBinary(data:String[], path:String) Local o:TStream = WriteStream(path) Local intLoop:Int = 0 If Not o = Null Then ' Save the data 1 value per line For intLoop = 0 To data.Length - 1 WriteInt(o, Int(data[intLoop])) Next CloseStream(o) End If End Function Function Split:String[](source:String, delimiter:String) ' Splits the data apart based on the delimiter specified Local i:Int Local array:String[1] Local value:String Local arrI:Int = -1 Local temp:String = source Repeat i = Instr(temp , delimiter) arrI :+ 1 array = array[..arrI + 1] If i > 0 Then ' In string array[arrI] = Mid(temp , 1 , i - 1) temp = Mid(temp, i + 1, Len(temp)) Else ' Equal string array[arrI] = temp End If Until i = 0 Return array End Function Function Main() Local aryOut:String[] = Split("64 48 17 206 142 214"," ") Local aryIn:String[] Local strPath:String = "binarytest.dkm" Local intLoop:Int = 0 Try Print "Creating binary file..." WriteBinary(aryOut, strPath) Print "Reading binary file created..." aryIn = ReadBinary(strPath) Print "Displaying file contents..." For intLoop = 0 To aryIn.Length - 1 Print "Position " + intLoop + ": " + aryIn[intLoop] Next Catch ex:Object Print "Error occured: " + ex.ToString() End Try End Function Main() Keep in mind there are OTHER keywords you'll use writing and reading binary: Check out the STREAMS part in BlitzMax help. Hope this helps :) |