Author Topic: Using crt.bi for opening files  (Read 8639 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Using crt.bi for opening files
« on: April 04, 2007 »
just wondering if anyone can see where im going wrong here as ive not used the crt file funcs in freebasic before.

Code: [Select]
#Include Once "Crt.bi"

Dim Shared As File PTR Handle
Handle = Fopen("BadGuyTMap1.SGM", "r")
Dim Shared As ZString PTR FileIn
 
Dim Shared As Integer size
Size=Sizeof(Handle)

filein = malloc (size+1)
for x = 0 to size
          Filein[x]=fgetc(handle)
next
fclose(handle)

print FileIn

sleep
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Using crt.bi for opening files
« Reply #1 on: April 04, 2007 »
I've not used it either but if handle is a pointer, won't sizeof(handle) return 4, as a pointer is 4 bytes and your code will read the first 5 bytes from the file?

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Using crt.bi for opening files
« Reply #2 on: April 04, 2007 »
ahh yes your quite right so i take it the file size is the number of elements in my txt file*4.

does anyone have a decent filesize function lying around where i could go filesize = sizeoffile(Handle).
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Using crt.bi for opening files
« Reply #3 on: April 04, 2007 »
what about writing the size of the file or size of the map into the start of your file? then when you open it for reading you can read how much data it contains.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Using crt.bi for opening files
« Reply #4 on: April 04, 2007 »
yeah for sure that would work i just like clean code that can dynamically resize a variable to whatever is needed at that time.

theres still something not quite right with the abouve even with the size amended when i try to print the contents of filein i just get garbage im trying to print it like this printf("%s", Filein).
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Using crt.bi for opening files
« Reply #5 on: April 04, 2007 »
you can read the size from the file first then allocate however much memory you're going to need. unless there's some other way like LOF that's in the inbuilt FB file handling that'll return the length of a file.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Using crt.bi for opening files
« Reply #6 on: April 04, 2007 »
not sure but i think Lof works only when you open the file using fbs commands im thinking it might be possible to create a getfilesize function that records current position moves to the start of the file then loops through the file 4 bytes at a time till it hits Eof then puts the file position back to where ever it was when the function was called ill try that out but i still have the problem of why its not reading properly?
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Using crt.bi for opening files
« Reply #7 on: April 04, 2007 »
print *filein

maybe?

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Using crt.bi for opening files
« Reply #8 on: April 04, 2007 »
To get the length you can try...
Code: [Select]
fseek(handle, 0, SEEK_END);
length = ftell(handle);
fseek(handle, 0, SEEK_SET);

Then, instead of reading every byte one at a time,
Code: [Select]
fread(filein, 1, size, handle);

I think you want to be opening your file in "rb" not "r" mode with fopen too.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Using crt.bi for opening files
« Reply #9 on: April 05, 2007 »
works like a charm now cheers guys.

it works in both r and rb modes.

Code: [Select]
#Include Once "Crt.bi"

Dim Shared As File PTR Handle
Handle = Fopen("BadGuyTMap1.SGM", "rb")
Dim Shared As ZString PTR FileIn
 
Dim Shared As Integer size
Fseek(Handle, 0, SEEK_END)
Size = Ftell(Handle)
Fseek(Handle, 0, SEEK_SET)

Filein = Malloc(Size+1)
Fread(Filein, 1, Size, Handle)
Fclose(Handle)

Printf("%s", Filein)

sleep
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Using crt.bi for opening files
« Reply #10 on: April 05, 2007 »
If you use "r" mode for binary files, the CRT is allowed to do line-ending translations (usually between DOS and Unix (0x0d 0x0a -> 0x0a)) which will break your file.  Use "rb" for binary.  Like you say it probably works OK in "r" mode, and on most Unixes there's no difference between the two, but you should do it right :)

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Using crt.bi for opening files
« Reply #11 on: April 05, 2007 »
you learn something new every day ta jim.
Challenge Trophies Won:

Offline rdc

  • Pentium
  • *****
  • Posts: 1495
  • Karma: 140
  • Yes, it is me.
    • View Profile
    • Clark Productions
Re: Using crt.bi for opening files
« Reply #12 on: April 05, 2007 »
file.bi contains several functions that operate on files that are a bit easier to use:

Code: [Select]
#include "file.bi"

dim as integer fh, i, fsize
dim as byte ptr fl

fsize = filelen("filename.ext")
fl = callocate(fsize)
fh = freefile
open "filename.ext" for binary as #fh
get #fh,, *fl, fsize
close fh

for i = 0 to fsize - 1
    print fl[i]
next
sleep

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Using crt.bi for opening files
« Reply #13 on: April 05, 2007 »
yeah ive used the fb commands before but i dont like them i think the crt ones are more versitile and then if my stuff is ever getting ported to c its should be a dodle.
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Using crt.bi for opening files
« Reply #14 on: April 05, 2007 »
just wonderind what the best way of reading a line in using sscanf would be my line is a has 150 numbers and 150 , s i want to stip the commas out and dump the numbers in an array i know how todo this on small lines using sscanf but not big ones.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Using crt.bi for opening files
« Reply #15 on: April 05, 2007 »
untested
Code: [Select]
dim numbers (256) as integer
for x = 0 to 149
fscanf(handle, "%d,", @numbers(x))
next
Check it doesn't miss out the last number.
fgets/sscanf are hard to use here because you want to step over each number as you eat it up.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Using crt.bi for opening files
« Reply #16 on: April 05, 2007 »
cool yeah it picks up the last number because its a file function will it automatically move onto the next line when it hits '\0' so calling the same would result in it reading a new line?
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Using crt.bi for opening files
« Reply #17 on: April 05, 2007 »
just tested and it does move down lines also. i must say its amazing how simple it is to parse through files with crt it looks a bit daunting at first but its so much more flexable.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Using crt.bi for opening files
« Reply #18 on: April 05, 2007 »
Quote
because its a file function will it automatically move onto the next line when it hits '\0' so calling the same would result in it reading a new line?
No, but the fscanf will hit the line end and see it doesn't match the "%d," and so stop anyway.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Using crt.bi for opening files
« Reply #19 on: April 06, 2007 »
ahh right ive noticed something weird in fb using printf it doesnt do anything with \n it just prints it as part of the string.
Challenge Trophies Won: