Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fread returning 0 bytes

Status
Not open for further replies.

acorbally

Programmer
Jan 9, 2001
65
US
This file has 15559 bytes in but is not showing anything in the gettxt. It has a len of 0. Can you see what I am doing wrong to see what is in the file?
if file ('cvtoracl.txt')
file = fopen('cvtoracl.txt')
endif

store fopen('cvtoracl.txt') to ofile
store fseek(ofile,0) to bof &&beginning of file
store fseek(ofile,0,2) to eof &&end of file

gettxt = fread(ofile,eof)
 
You either need to remove this line:
store fseek(ofile,0,2) to eof &&end of file

or issue this line again:
store fseek(ofile,0) to bof &&beginning of file

Your current methodology positions the record pointer to EFO(), which will not allow you to read any more of the file.


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
I have changed it to the following: Almost exactly like it examples in help and still nothing. Interesting enough feof(ofile) returns .T.

store fopen('cvtoracl.txt') to ofile
store fseek(ofile,0,2) to eof && end of file
store fseek(ofile,0) to bof && beginning of file

gettxt = fread(ofile,eof)
 
Interesting enough feof(ofile) returns .T.
There are a couple instances where FEOF() will return .T.
If you are at end of file or because the file handle isn't open.
You probably still have the file open from your testing.
Let me elaborate a little more here.
Once you open the file, you then need to close it before you can open it again.
Also keep in mind, when you move the file pointer around, you manually need to reposition it if you expect to get the desired results.

store fopen('cvtoracl.txt') to ofile
No problem here, unless the file is already open. If that is the case, ofile will == -1, and the rest of these functions won't work.

Try issuing a close all or FCLOSE(ofile) after every attempt to open it.

-Dave Summers-
[cheers]
Even more Fox stuff at:
 
You don't seem to be testing if you've got a valid file handle. Here's and example which shows a number of concepts...


Code:
* Your file name: 'cvtoracl.txt'

set step on
private cFile1, cFile2, cFile3
private cTmpCurs, cTmpFile1, cTmpFile2, nWkArea, nTmpFlHndl

* Get temporary drive and directory
* Work around for Fox2X's sys(2023) command
* only returning the drive and not the path
cTmpCurs  = sys(2015)
cTmpFile1 = cTmpCurs  && Creates a file to test empty and open logic below
cTmpFile2 = cTmpFile1 + "A"

nWkArea = select()

create cursor (cTmpCurs) (fld c(1))

cTmpFile1 = left(dbf(),rat("\",dbf())) + cTmpFile1 + ".txt"
cTmpFile2 = left(dbf(),rat("\",dbf())) + cTmpFile2 + ".txt"

use in (cTmpCurs)
select (nWkArea)
*

cFile1 = cTmpFile2
cFile2 = ""
cFile3 = cTmpFile1

* Create test files...
nTmpFlHndl = fcreate(cTmpFile1)
=fclose(nTmpFlHndl)

nTmpFlHndl = fcreate(cTmpFile2)
=fwrite(nTmpFlHndl,"Hello")
=fclose(nTmpFlHndl)


* Test function...

private cBytes, i

for i = 1 to 3

  if i == 3
    * Cause open file failure...
    nTmpFlHndl = fopen(cTmpFile1)
  endif

  cBytes = ReadAFile(eval("cFile"+str(i,1)))

  do case

    case type("cBytes")=="N"
      wait "Can't open file!" window timeout 3

    case type("cBytes")=="L"
      wait "File doesn't exist!" window timeout 3

    case empty(cBytes)
      wait "File is empty!" window timeout 3

    otherwise
      * Process valid file bytes...
  endcase

next

=fclose(nTmpFlHndl)

delete file (cTmpFile1)
delete file (cTmpFile2)

function ReadAFile
  parameters cFileName

  private cFileName, nFlHndl, nSize, vFileBytes, bFileExists

  vFileBytes = ""

  bFileExists = file(cFileName)

  if bFileExists
    nFlHndl = fopen(cFileName)

    if nFlHndl <> -1

      nSize = fseek(nFlHndl,0,2) && Get file size

      =fseek(nFlHndl,0,0) && Reposition the file pointer

      vFileBytes = fread(nFlHndl,nSize) && Read the file

      =fclose(nFlHndl) && Close it

    endif

  endif

  return iif(!bFileExists, bFileExists, iif(nFlHndl<>-1, vFileBytes, nFlHndl) )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top