Writing Data to File Requires Two Attempts

General FreeBASIC programming questions.
mark bower
Posts: 389
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Writing Data to File Requires Two Attempts

Postby mark bower » Dec 18, 2019 2:58

The code below works. However, to get it to open and write to the file as a result of: 1)key press for early termination, or 2) reaching the duration time (DT), I have to write to the file twice. If one attempt to write to the file is eliminated, no file is opened or written to the desktop. What do I not alter/need to add in the code?

Code: Select all

do
   If Inkey <> "" Then Exit Do      'press any key to terminate program early
   GET_DATA3(Samples)
   PLOT1
   DELAY_INTERVAL   
   if elapsedtime >= DT then exit do   
'loop until MultiKey(FB.SC_ESCAPE)   'didn't get the hang of how to use these two lines
'While Inkey <> "": Wend
Loop

Open  "/home/mark/Desktop/batterydischarg.txt" For Output As #1
   for j = 1 to 15
   Write #1,E1(j),A0(j),A1(j),A2(j)
   next
Close #1

Open  "/home/mark/Desktop/batterydischarg.txt" For Output As #1
   for j = 1 to 15
   Write #1,E1(j),A0(j),A1(j),A2(j)
   next
Close #1

locate 25,9:print "DONE"
locate 26,1: print "Press Any Key to Continue"
sleep
Last edited by mark bower on Dec 18, 2019 3:49, edited 2 times in total.
MystikShadows
Posts: 612
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Re: Writing Data to File Requires Two Attempts

Postby MystikShadows » Dec 18, 2019 3:11

i bet you it work if you on have your second open statement. like:

Code: Select all

do
   If Inkey <> "" Then Exit Do      'press any key to terminate program early
   GET_DATA3(Samples)
   PLOT1
   DELAY_INTERVAL   
   if elapsedtime >= DT then exit do   
'loop until MultiKey(FB.SC_ESCAPE)   'didn't get the hang of how to use these two lines
'While Inkey <> "": Wend
Loop

Open  "/home/mark/Desktop/batterydischarg.txt" For Output As #1
   for j = 1 to 15
   Write #1,E1(j),A0(j),A1(j),A2(j)
   next
Close #1

locate 25,9:print "DONE"
locate 26,1: print "Press Any Key to Continue"
sleep
mark bower
Posts: 389
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Re: Writing Data to File Requires Two Attempts

Postby mark bower » Dec 18, 2019 3:26

My apologies, I corrected the code in my first post to reflect two identical code attempts to write to the file. One attempt only will not open or write to the file.
fxm
Posts: 9820
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Writing Data to File Requires Two Attempts

Postby fxm » Dec 18, 2019 9:09

Looking at the block [Open ... Close], one single should be enough to properly write in the file, but I don't know the context when exiting the previous code (perhaps a conflict with another file input/output not closed and also using the same file number #1?).

In any case it is always better to use the 'FreeFile' keyword to attribute an available file number for each [Open ... Close] block:

Code: Select all

dim as long f
'.....

f = freefile
Open  "/home/mark/Desktop/batterydischarg.txt" For Output As #f
   for j = 1 to 15
   Write #f,E1(j),A0(j),A1(j),A2(j)
   next
Close #f
mark bower
Posts: 389
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Re: Writing Data to File Requires Two Attempts

Postby mark bower » Dec 18, 2019 16:05

@ fxm
Thank you. I directly incorporated the code (and remmed my two Write to file sections) you suggest and use of Freefile to create and write to the file; it works with a single Write attempt. I need to go to FB documentation and learn more about Freefile as I have never before used it. The discharg.battery.txt file is the only file in use, and it does not exist when creating it and writing to it.

Given time, I will tinker some more with the "upstream" code in my program. I initiate a HP meter in RS-232 mode to gather the data. Perhaps that needs to be closed out in some fashion before writing to file?

Anyway, appreciate your getting back to my request for help so quickly.
Last edited by mark bower on Dec 19, 2019 0:51, edited 1 time in total.
fxm
Posts: 9820
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Writing Data to File Requires Two Attempts

Postby fxm » Dec 18, 2019 16:25

If you assign file numbers yourself, you must not yet assign a number that has not yet been closed.
The simplest and safest way is to call FREEFILE just before each opening since it will always return the smallest available (unused) number of file if there is any, otherwise zero (0).
mark bower
Posts: 389
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Re: Writing Data to File Requires Two Attempts

Postby mark bower » Dec 19, 2019 1:01

o.k., I copied your guidance into notes in my program.

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest