HELP!!!!!!!!!!!!!!!!!!!

BlitzMax Forums/BlitzMax Beginners Area/HELP!!!!!!!!!!!!!!!!!!!

Caton(Posted 2016) [#1]
file=ReadFile(filename$)
Length=ReadInt(file)
StringTest1$=ReadString(file,Length)
CloseFile(file)

I get a error ever time I use int to define Length of string


Brucey(Posted 2016) [#2]
Oh dear. See here and here for more information.


Derron(Posted 2016) [#3]
Could an moderator please moderate... (join the 3 threads ... or lock the "unused" ones).


bye
Ron


dw817(Posted 2016) [#4]
You've asked a few times now Caton. I guess you're not getting what you want. See if this helps:
Strict
Local fp:TStream,t$,f$="data.txt" ' f$ is name of file to read
fp=ReadStream(f$) ' you must assign a TSTREAM before reading a file
t$=LoadString(fp) ' read entire file into t$
Print Len(t$) ' print length of it
Print t$ ' print entire contents of it
CloseStream fp ' close your file stream when done
End ' exit



markcw(Posted 2016) [#5]
Firstly ReadInt returns 4 bytes from the stream as a Int value, what you probably want is StreamSize this gives you the filesize, then it works.

' caton.bmx

filename$="caton2.bmx"
file:TStream=ReadFile(filename$)
Length%=StreamSize(file)
StringTest1$=ReadString(file,Length)
DebugLog Length
DebugLog StringTest1
CloseFile(file)

Contents of text file.
hello world
this is a test



Brucey(Posted 2016) [#6]
dw817, if you actually read his code, you'd have seen this:
Length=ReadInt(file)

which implies that he has an Int followed by a string in the file. The int value signifies the length of the following text.
Presumably it is some kind of custom file format (see other of his previous posts also in the Blitz3D section).

ReadFile returns a TStream object.

Since he appears to ignore replies to the other posts, one expects the same outcome here.


dw817(Posted 2016) [#7]
Hi Brucey. I cut to what it appeared he wanted and skipped over the fact ReadInt was being used incorrectly. When I tutored in the past, I would do that with students sometimes and made greater headway than trying to explain what they did wrong.

If they brought it back up and there was no simple solution to use a different approach, then I would explain that command to them.

But yes, ReadInt will read 4-characters of the open file and convert it to a number, NOT the length of the file. It's essentially your CVS($) routine from BASIC, in this case read from a file and not a string variable.

http://www.antonis.de/qbebooks/gwbasman/cvi.html

And yes, it is hoped something can be learned from a working solution. :)


Brucey(Posted 2016) [#8]
You can't really just make sweeping assumptions as to what you *think* someone wants to do.

I refer you to Floyd's post (#2) here, in which he discusses Blitz3D's string storage, from which Caton appears to be trying to read from in BlitzMax...


At the end of the day, I'm pretty sure he's just having a laugh, as all these "help" posts appear to be subsequently ignored by the author.


dw817(Posted 2016) [#9]
Well you can't make sweeping assumptions about my sweeping assumptions either. :)



In fact, no-one is allowed that unique privilege, not unless they are from planet Corinia.



Knowledge is power ... it grows like a flooow-oowww-eerr !


Kryzon(Posted 2016) [#10]
I get a error ever time I use int to define Length of string

Why do you need to use an Int for defining length?
Use ReadLine and WriteLine, let BlitzMax handle string length itself.

By using those two functions you can read and write any string you want, regardless of size. The resulting file will be human-readable as well, which is a positive point.


Brucey(Posted 2016) [#11]
Why do you need to use an Int for defining length?

Probably because that's what Blitz3D does.

@dw817
I've never understood the point of posting images/videos as a reply to something, other than trying to be clever or cool, or in an attempt to distract - for whatever reason.


dw817(Posted 2016) [#12]
Brucey, sometimes you just gotta let it out. And yes, here is another.

YOUR ARGUMENT IS INVALID

What am I listening to ? MUSIC.

Don't worry, I'll post some new source soon. I'm still fighting the infernal gadgets from MAXGUI in my Carryall project. I think in the future I'll write my own GUI like S2 has.

Usually if I have a question about coding now, I Google it, and trace it back to BlitzBASIC.com. That's why I haven't asked any questions of late.


Floyd(Posted 2016) [#13]
I still get sucked into trying to answer these questions even though I know it is futile. There's never enough information.

This thread starts with a code fragment and "I get a error...". What error? What was expected that didn't happen?
What value did Length=ReadInt(file) produce? Does it look reasonable?

And we can't try the code ourselves. It is using a file we don't have. My guess about the current problem is that the first four bytes are supposed to contain the length of a string to be read but actually do not. Without access to the file this is speculation.


Kryzon(Posted 2016) [#14]
I'm still processing the fact that ReadFile returns a TStream object and he's putting that value in an Int.


Derron(Posted 2016) [#15]
That is because it does not use "strict" (or "superstrict")

save as nonstrict.bmx
Framework Brl.StandardIO
Import Brl.Stream
Import Brl.Reflection

local f = ReadStream("nonstrict.bmx")

local t:TTypeID = TTypeID.ForObject(f)
if t
	print t.name() 'prints "TCStream"
else
	print "unknown type"
endif


bye
Ron