IonicWind Software

IWBasic => General Questions => Topic started by: ckoehn on February 13, 2010, 01:31:35 PM

Title: File to String
Post by: ckoehn on February 13, 2010, 01:31:35 PM
Is it possible to load from a file to an ISTRING, reading all the newline characters and everything till the eof?  I've tried all kind of things and nothing seems to work.

When I read a line at a time and append it, it takes forever to read a long file.

Thanks,
Clint
Title: Re: File to String
Post by: sapero on February 13, 2010, 03:09:18 PM
Hello ckoehn, this is base code for reading a file into an string:
$define BUFF_SIZE 256 /*will read up to 255 characters*/
istring buffer[BUFF_SIZE]
uint  cch

if (LoadFileToString("C:\\WINDOWS\\setupapi.log", buffer, BUFF_SIZE, &cch))
if (cch >= BUFF_SIZE) then messagebox 0, using("the file is larger: # bytes", cch), "read file demo", 0
messagebox 0, buffer, "read file demo", 0
else
messagebox 0, "failed to open or read from file", "read file demo", 0
endif
end

sub LoadFileToString(string path, string buffer, uint cchBuffer, pointer pFileSize),int
int BytesRead, success = false
bfile f

if (cchBuffer and !openfile(f, path, "r"))
' declare required imports
declare import,GetBFileSize alias GetFileSize(bfile hFile,pointer lpFileSizeHigh),uint
declare import,ReadBFile alias ReadFile(bfile hFile,pointer lpBuffer,_
int nNumberOfBytesToRead,pointer lpNumberOfBytesRead,pointer lpOverlapped),int

uint filesizeHigh
uint filesizeLow = GetBFileSize(f, &filesizeHigh)
if (filesizeHigh)
'TODO: file size is >=4GB, *pFileSize should be uint64
filesizeLow = 0xFFFFFFFF
endif
*<uint>pFileSize = filesizeLow

uint BytesToRead = cchBuffer-1 ' make room for terminating NULL
if (ReadBFile(f, buffer, BytesToRead, &BytesRead, 0))
buffer[BytesRead] = 0
success = true
endif
closefile f
endif
return success
endsub
Title: Re: File to String
Post by: ckoehn on February 13, 2010, 06:18:00 PM
sapero,  I really do appreciate you helping me out.  Will try it out.

Thanks,
Clint
Title: Re: File to String
Post by: ckoehn on February 13, 2010, 07:00:09 PM
sapero,

I ran your test code and it worked fine, but when I tried my filename and buffer it didn't work.  The max file size I'm trying to load will be close to 64K.  Is that the problem?

Later,
Clint
Title: Re: File to String
Post by: ckoehn on February 13, 2010, 07:16:15 PM
sapero,

I hard coded the filename I wanted into your code and it loaded it.  I used a string for the filename in your code and it loaded it.  Is it something to do with the stack size or something?

Thanks,
Clint
Title: Re: File to String
Post by: ckoehn on February 13, 2010, 07:38:58 PM
Apparently my path name is too long.  It is under the default install directory of EBASIC.  When I pointed to a different directory it worked.

Thanks for your time sapero,
Clint
Title: Re: File to String
Post by: LarryMc on February 13, 2010, 08:01:37 PM
your full path name should be stored in an ISTRING that is 260 chrs

ISTRING myfilename[260]
myfilename="c:\\whatever\\whatever\\whatever\\myfile.txt"


maybe that's the problem

LarryMc
Title: Re: File to String
Post by: ckoehn on February 14, 2010, 03:52:49 PM
Thanks, Larry.  After my post that crossed my mind.  I appreciate the help that this forum gives.

Later,
Clint
Title: Re: File to String
Post by: ckoehn on February 14, 2010, 04:19:17 PM
Larry,

I checked into that file length and that wasn't it.  Below is the file path.

C:\Documents and Settings\All Users\Documents\Emergence BASIC\projects\MyProjects\MOT\MT195301.e

Why wouldn't that work?  When I change it to: C:\MOT\MOT\MT195301.e  then it works.

Later,
Clint
Title: Re: File to String
Post by: LarryMc on February 14, 2010, 05:05:51 PM
I think the spaces are causing your problem.

istring fname[260]="C:\\Documents and Settings\\All Users\\Documents\\EmergenceBASIC\\projects\\MyProjects\\MOT\\MT195301.e"
if (LoadFileToString(fname, buffer, BUFF_SIZE, &cch))


LarryMc


Title: Re: File to String
Post by: ckoehn on February 14, 2010, 07:57:49 PM
Larry,

What are the options?  How do you address that problem?

Later,
Clint
Title: Re: File to String
Post by: LarryMc on February 14, 2010, 07:59:56 PM
Did you try it just as I demonstrated above?

LarryMc
Title: Re: File to String
Post by: ckoehn on February 15, 2010, 05:58:00 AM
Yes I did Larry.   It doesn't work.


The way I need it is like this:

filename="MT195301.e"   'this is loaded from a list box
fname=GETSTARTPATH+"MOT\\"+filename

That doesn't work either.  The thing that makes it strange is that I use the above method with FILEOPEN in a different part of my program and it works.  The only difference is that I'm (sapero code) is sending the filename to a SUB, where as in the other part of my program I'm not.

Thanks for you time,
Clint
Title: Re: File to String
Post by: ckoehn on February 15, 2010, 07:47:38 AM
Just an update.  I modified sapero's code to where I just pass the filename and not the whole path.  That worked.  The pathname included spaces and periods.  I don't know if that had something to do with it or not.

sub LoadFileToString(string path,string buffer, uint cchBuffer, pointer pFileSize),int
int BytesRead, success = false
bfile f

if (cchBuffer and !openfile(f,GETSTARTPATH+"MOT\\"+path,"r"))
' declare required imports
'declare import,GetBFileSize alias GetFileSize(bfile hFile,pointer lpFileSizeHigh),uint
'declare import,ReadBFile alias ReadFile(bfile hFile,pointer lpBuffer,_
' int nNumberOfBytesToRead,pointer lpNumberOfBytesRead,pointer lpOverlapped),int

uint filesizeHigh
uint filesizeLow = _GetFileSize(f, &filesizeHigh)
if (filesizeHigh)
'TODO: file size is >=4GB, *pFileSize should be uint64
filesizeLow = 0xFFFFFFFF
endif
*<uint>pFileSize = filesizeLow

uint BytesToRead = cchBuffer-1 ' make room for terminating NULL
if (_ReadFile(f, buffer, BytesToRead, &BytesRead, 0))
buffer[BytesRead] = 0
success = true
endif
closefile f
endif

return success
endsub