How to read a txt file in BBx4 - business-basic

I have a >30 year old program in BBx what need to read something outside it's own database. Actually it must be something very simple like
txt$ = read (message.txt)
print txt$
However there isn't any documentation available. So my question is: How can i read a plain txt file in to BBx4

simple open the file and read it with READ RECORD
open (1,err=linenr) "message.txt"
read record (1,siz=1,end=linenr) txt$
opens on channel 1, linenr=line to go when there is an error
*siz=1 reads 1 character siz=100 reads 100 etc. end where to go when end of file is detected.

You can read an ASCII file line by line in a loop, as follows:
ch=unt
open (ch)file$
while 1
read (ch,end=*break)line$
if line$="" then
continue
rem if the line is empty, skip it
fi
print line$
wend
close (ch)
If you know that the content of the file will fit in the memory, you can read it in one:
ch=unt
open (ch)file$
read record (ch,siz=dec(fin(ch)(1,4)))content$
close (ch)
print content$
The fin(ch) is the file information string, bytes 1-4 are the actual file length in bytes (for an ASCII file).

Related

Cannot read lines from a file using bash read command [duplicate]

This question already has answers here:
Shell script read missing last line
(7 answers)
Closed 2 years ago.
Created a text file as hello_world.rtf with following two lines only:
Hello
World
and trying to read above file using below bash script from terminal:
while test= read -r line; do
> echo "The text read from file is: $line"
> done < hello_world.rtf
and it returns the following:
The text read from file is: {\rtf1\ansi\ansicpg1252\cocoartf1671\cocoasubrtf500
The text read from file is: {\fonttbl\f0\fswiss\fcharset0 Helvetica;}
The text read from file is: {\colortbl;\red255\green255\blue255;}
The text read from file is: {\*\expandedcolortbl;;}
The text read from file is: \paperw12240\paperh15840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
The text read from file is: \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
The text read from file is:
The text read from file is: \f0\fs24 \cf0 Hello\
Any suggestion what is wrong here and how can I get the clean result?
RTF means Rich Text Format. It is a language for text formatting, developed and used mostly by Microsoft and deprecated for a while.
The text inside the file looks as you can see in the output of your code. It contains the words "Hello" and "World" but also formatting instructions.
Save the file as plain text, not RTF and it will contain only the text you typed in it.
test= in front of read does not have any effect in this context. You can remove it.
Make sure the last line of the file ends with a new-line character. read returns an non-zero exit status (and this means false) when it reaches the end of file and your code exits the while loop and does not display the last value read by read. If the file ends with a new-line character, the last line (that is read but not listed by the code) is empty, therefore nothing is lost.
It is a recommended practice for text files to always end with a newline character.
Alternatively you can print the value of line again after the loop. It contains the last line of the file (from the last end-of-line character until the end of file).

How to read a file from command line using < operator and read user input afterwards?

I am writing a program in which I am taking in a csv file via the < operator on the command line. After I read in the file I would also like to ask the user questions and have them input their response via the command line. However, whenever I ask for user input, my program skips right over it.
When I searched stack overflow I found what seems to be the python version here, but it doesn't really help me since the methods are obviously different.
I read my file using $stdin.read. And I have tried to use regular gets, STDIN.gets, and $stdin.gets. However, the program always skips over them.
Sample input ruby ./bin/kata < items.csv
Current File
require 'csv'
n = $stdin.read
arr = CSV.parse(n)
input = ''
while true
puts "What is your choice: "
input = $stdin.gets.to_i
if input.zero?
break
end
end
My expected result is to have What is your choice: display in the command and wait for user input. However, I am getting that phrase displayed over and over in an infinite loop. Any help would be appreciated!
You can't read both file and user input from stdin. You must choose. But since you want both, how about this:
Instead of piping the file content to stdin, pass just the filename to your script. The script will then open and read the file. And stdin will be available for interaction with the user (through $stdin or STDIN).
Here is a minor modification of your script:
arr = CSV.parse(ARGF) # the important part.
input = ''
while true
puts "What is your choice: "
input = STDIN.gets.to_i
if input.zero?
break
end
end
And you can call it like this:
ruby ./bin/kata items.csv
You can read more about ARGF in the documentation: https://ruby-doc.org/core-2.6/ARGF.html
This has nothing to do with Ruby. It is a feature of the shell.
A file descriptor is connected to exactly one file at any one time. The file descriptor 0 (standard input) can be connected to a file or it can be connected to the terminal. It can't be connected to both.
So, therefore, what you want is simply not possible. And it is not just not possible in Ruby, it is fundamentally impossible by the very nature of how shell redirection works.
If you want to change this, there is nothing you can do in your program or in Ruby. You need to modify how your shell works.

Find specific column range and line using VBSCRIPT

I have a txt file that I only need to read the first line, but only want values from column 64-70 from line 1. How do I do this in vbscript? I have looked at several ways to do this but can't get exactly what I'm looking for. Please help.
For the reading characters from the line, use Mid(source_str, 64, 6). -- 6 is the length from character 64 to 70.
As for reading the first line from the text file, you will need to set up a loop to read each line until the end of the file, parse them into an array of strings, then process only the 1st.
Or, since you only need the first line, just run fsoStream.ReadLine() once.
So in your case:
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("filename.txt")
'This only reads the first line of the file.
'To read any others, we would need a loop.
line = file.ReadLine()
thisStr = Mid(line,64,6)

Why must I .read() a file I wrote before being able to actually output the content to the terminal?

I am learning Ruby and am messing with reading/writing files right now. When I create the file, 'filename', I can write to it with the .write() method. However, I cannot output the content to the terminal without reopening it after running .read() on it (see line 8: puts write_txt.read()). I have tried running line 8 multiple times, but all that does is output more blank lines. Without line 8, puts txt.read() simply outputs a blank line. The following code also works without the puts in line 8 (simply write_txt.read())
# Unpacks first argument to 'filename'
filename = ARGV.first
# Lets write try writing to a file
write_txt = File.new(filename, 'w+')
write_txt.write("OMG I wrote this file!\nHow cool is that?")
# This outputs a blank line THIS IS THE LINE IN QUESTION
puts write_txt.read()
txt = File.open(filename)
# This actually outputs the text that I wrote
puts txt.read()
Why is this necessary? Why is the file that has clearly been written to being read as blank until it is reopened after being read as blank at least once?
When you read or write to a file, there's an internal pointer called a "cursor" that keeps track of where in the file you currently are. When you write a file, the cursor is set to the point after the last byte you wrote, so that if you perform additional writes, they happen after your previous write (rather than on top of it). When you perform a read, you are reading from the current position to the end of the file, which contains...nothing!
You can open a file (cursor position 0), then write the string "Hello" (cursor position 6), and attempting to read from the cursor will cause Ruby to say "Oh hey, there's no more content in this file past cursor position 6", and will simply return a blank string.
You can rewind the file cursor with IO#rewind to reset the cursor to the beginning of the file. You may then read the file (which will read from the cursor to the end of the file) normally.
Note that if you perform any writes after rewinding, you will overwrite your previously-written content.
# Unpacks first argument to 'filename'
filename = ARGV.first
# Lets write try writing to a file
write_txt = File.new(filename, 'w+')
write_txt.write("OMG I wrote this file!\nHow cool is that?")
write_txt.rewind
puts write_txt.read()
Note, however, that it is generally considered bad practice to both read from and write to the same file handle. You would generally open one file handle for reading and one for writing, as mixing the two can have nasty consequenses (such as accidentally overwriting existing content by rewinding the cursor for a read, and then performing a write!)
The output is not necessarily written to the file immediately. Also, the pointer is at the end of the file, if you want to read while in read-write mode you have to reset it. You can simply close if you want to reopen it for reading. Try:
write_txt.write("OMG I wrote this file!\nHow cool is that?")
# This outputs a blank line THIS IS THE LINE IN QUESTION
write_txt.close
txt = File.open(filename)
puts txt.read()

Remove additional newlines from file output

I have a script that dumps data from a serial port to both a terminal and the harddrive. The output to the terminal looks fine, however the file write an ^M after each resulting in an extra newline for every other line.
The offending code:
# run and dump to file.
loop {
# output data to log file.
data = sp.read
data.delete!("\C-M") # Removes escape character.
if( data != "" )
puts data
File.open($log_file, 'a') { |f| f.write( data ) }
end
}
Example output:
On the terminal:
1
2
3
In the file
1
2
3
Edit: The solution is to run data.delete!("\C-M") after the read.
Try opening the data written to the file in ruby with read. I suspect the problem you have is with the carriage return characters that sometimes cause problems when transferring a file from windows to linux or when downloading files via some mail clients.
I don't know how your serial data looks like, but you can always do a chomp on data variable before writing. Try it and see how it goes.
Edit: If you want to remove the ^M, maybe you can try sp.read.tr("\r","")

Resources