May 01, 2024, 09:04:57 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


File to String

Started by ckoehn, February 13, 2010, 01:31:35 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ckoehn

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

sapero

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

ckoehn

sapero,  I really do appreciate you helping me out.  Will try it out.

Thanks,
Clint

ckoehn

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

ckoehn

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

ckoehn

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

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

ckoehn

Thanks, Larry.  After my post that crossed my mind.  I appreciate the help that this forum gives.

Later,
Clint

ckoehn

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

LarryMc

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


LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

ckoehn

Larry,

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

Later,
Clint

LarryMc

Did you try it just as I demonstrated above?

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

ckoehn

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

ckoehn

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