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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fwrite

Status
Not open for further replies.

thegame8311

Technical User
Jan 6, 2012
133
US
I have this problem where Fwrite is not writing the data

Code:
lnFileHandle3 = FCREATE('C:\Stats3.txt') && creating final file

PUBLIC newline3

*lnCount3 = OCCURS(',', z)*

LOCAL lnFileHandle7 && numeric file handle

lnFileHandle7 = FOPEN( "C:\Stats2.txt") && open previous file for reading

IF lnFileHandle7 = -1

   && error, could not open the file

   && do something to handle the error

   RETURN

ENDIF

LOCAL lcLine && define a variable to hold each line

DO WHILE NOT FEOF( lnFileHandle7) && loop through the file

   lcLine = FGETS( lnFileHandle7) && store each line in lcLine
   
   lp1.text3.value = GETWORDCOUNT(lcLine)
   
   FWRITE(lnFileHandle3, GETWORDCOUNT(lcLine))
   
ENDDO 

FCLOSE(lnFileHandle3)
FCLOSE(lnFileHandle7)

 
In an update I was told that fwrite cannot write out numbers

so is the solution to make it a string, then write out to the file?
 
As already said, FWRITE is parameterized FWRITE(nFilehandle,cExpression) and an optional third parameter.

The essential part is, you write out a character expression, the type of the second parameter must be C (char). GETWORDCOUNT() returns a number, not a character/string expression. To write out that number, use either TRANSFORM() or STR() to convert to a string.

Bye, Olaf.

 
You can have that whole stuff simpler wothout writing out files:

Code:
Text To lcSampleText NoShow
This is some text.
It has three lines.
It has fourteen words in total.
EndText

* could also do
* lcSampleText = filetostr("c:\some.txt")
? "total number of words, the short way:", GetWordCount(lcSampleText)

Create Cursor curWordcounts (iLineNo I Autoinc, iWordcount I)
ALines(laTextLines,lcSampleText)
For each lcLine In laTextLines
  Insert Into curWordcounts (iWordcount) values (GetWordCount(lcLine))
EndFor
Select Sum(iWordCount) As iTotal From curWordCounts Into Cursor curTotalWords

? "total number of words, found out the hard way:", curTotalWords.iTotal
Select curWordcounts 
Browse

But I think you're doing this for some leraning.

As a side note, see and
Bye, Olaf.
 
Well i write out to a file to make sure it's getting the right lines from the other file
 
It's clear you initially have a file to read in and count words in, you can keep that in fopen()/fgets()/fclose() manner, if that file is very large, or simply read into a memory variable via FileToStr() function, as long as the file is smaller than 16MB.

But you can alsways also output your counts into a dbf or cursor, especially the cursor is way faster than writing to disc, it stays in memory as long as it's not taking so much memory, vfp starts swapping. The upper limit of cursors and ram usage is about 2GB, lots of counts.

In general: You're using a language with database integration to use low level file functions? Then you'd also be faster with C++ for such tasks.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top