Liberty BASIC Community Forum
« Sequential Text File »

Welcome Guest. Please Login or Register.
Feb 18th, 2018, 3:41pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Sequential Text File  (Read 53 times)
Chester
New Member
Image


member is offline

Avatar




PM


Posts: 12
xx Sequential Text File
« Thread started on: Jan 31st, 2018, 5:08pm »

** TwainNovels.txt (program) ***


'****** OUTPUT Creates the text file ************************************************************

open "TwainNovels.txt" for output as #1
print #1, "Tom Sawyer"
print #1, "Huckleberry Finn"
print #1, "The Prince and the Pauper"
print #1, "A Connecticut Yankee in King Arthur's Court"
print #1, "A Tramp Abroad"
close #1
end

'****** INPUT Read the text file ****************************************************************

open "TwainNovels.txt" for input as #1
input #1, n1$
input #1, n2$
input #1, n3$
input #1, n4$
input #1, n5$
close #1
print n1$
print n2$
print n3$
print n4$
print n5$
end

(1) Can somebody tell me where this TwainNovels.txt are located on my harddrive? and how to control where you want the file to go.

(2) I can open this program for input and the file appear from my harddrive from somewhere, I do not quite understand how this work.

(3) Also an example of How to use notepad?.

Thanks, for your HELP
User IP Logged

Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2301
xx Re: Sequential Text File
« Reply #1 on: Jan 31st, 2018, 5:31pm »

1 and 2) You tell the file where to go by specifying the path in the OPEN statement when accessing the file. There are two ways to specify a path: exact paths and relative paths.

An exact path is just that: the full, exact path to the file. For example, C:\test\file.txt. Or C:\Users\Chris\Desktop\myfile.txt.

Code:
open "C:\test\file.txt" for output as #file 


If you do not specify a full path starting with a drive letter, LB will treat your file name as a relative path: the file is created in relation to the current directory of LB.

Every program running on Windows has a "current directory". This is a location where file operations are assumed to operate from, unless specified otherwise. In LB, this directory usually either the directory containing the EXE used to start the program, or the directory containing the source code you're working with. You can see what this is with the DefaultDir$ variable.

Code:
print DefaultDir$ 


That should list to you the folder you're working with. You should find your files in there.

Both of the following are examples of relative paths:

Code:
open "myfile.txt" for output as #file 

Code:
open "folder\myfile.txt" for output as #file 


In both cases, this is the same as specifying a full path that is the same as the DefaultDir$ variable.

Code:
open DefaultDir$ + "\myfile.txt" for output as #file 

Code:
open DefautlDir$ + "\folder\myfile.txt" for output as #file 


Those two examples should provide identical results to the examples above.

Opening files for input follows the same rules for accessing the file's location based on the current directory.


3) Your previous question shows how to have your LB code automatically open the results file in Notepad. If you want to open Notepad manually, and need to locate the file, either specify a full path, or get the contents of the DefaultDir$ variable, and check there for the file.
User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls