"bad file mode" error in qbasic - qbasic

I am just starting with file handling in qbasic. The program that I wrote always shows an error "bad file mode". Please help me!
OPEN "test.dat" FOR INPUT AS #1
CLS
INPUT "Enter username:"; a$
INPUT "Enter Password:"; b$
WRITE #1, a$, b$
CLOSE #1
END

If you want to WRITE output to a file then, logically, you might want to try this:
OPEN "test.dat" FOR OUTPUT AS #1
CLS
'etc

A more descriptive sample of file access in Qbasic:
' input data and write to file:
OPEN "test.dat" FOR OUTPUT AS #1
INPUT "Enter Username:"; a$
INPUT "Enter Password:"; b$
WRITE #1, a$, b$
CLOSE #1
' open file for input and display data:
OPEN "test.dat" FOR INPUT AS #1
INPUT #1, a$, b$
PRINT "Username: "; a$
PRINT "Password: "; b$
CLOSE #1
END

Related

error in system command to output

I want to run ext software (hmmer) commands through perl in a loop for input files (in linux).
I used this line
system "hmmbuild $outfile $files";
where $outfile is my output file and $files in my input files. hmmbuild is the command for the ext software.
When I run the program it gives me error code for the output file GLOB(0x1b94b220).
Can any one help me where I am wrong and how can it be corrected?
I tried exec command also with back tick and brackets.
This is the exact output message i got. How can I print my result to output file ($outfile)?
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `hmmbuild --amino GLOB(0x11eb3220) aproNOG00001'
script goes here..
#!/usr/bin/perl
use Bio::AlignIO;
use Bio::Align::AlignI;
my $allfiles= 'allfilenames_alpha_hmms.txt';
system "module load hmmer/3.1b1";
print "loaded hmmer\n";
open(FIH, $allfiles);
while ($min=<FIH>)
{ chomp($min); my #pats=split " ",$min;
foreach my $files(#pats) {
print $files; print "\n";
open(my $outfile, '>',"$prefix.hmm");
system "hmmbuild --amino $outfile $files";
print $outfile;
print "file saved\n";
# }
}
}
print "\n\n\n\t ###\tDONE\t### \n\n";
how can i print my result to output file ($outfile)
I take it hmmbuild expects a path to a file? Pass the path to the file rather than what's in $outfile.
system "hmmbuild --amino $prefix.hmm $files";

Why does perl remove the file extension when redirecting url via CMD?

I am at age 13 attempting to learn Perl. Here is one of my first scripts. The link brings the user to a webpage which downloads an image. As some observant people may notice the link has the ending of .png which specifies the image in the url. When I run this script it opens Chrome, opens the url BUT fails to include the extension .png . My questions are:
A) Why is this happening?
B) Can anyone suggest an alternative and or fix my script.
The script is found here below.
#!/usr/bin/perl
print "Type Username Here:
";
$username = <>;
print "Link: http://s3.amazonaws.com/MinecraftSkins/$username.png
";
exec "start www.s3.amazonaws.com/MinecraftSkins/$username.png";
Please help!!!
When you capture the data, you are also capturing the new line at the end of it.
This gives you:
exec "start www.s3.amazonaws.com/MinecraftSkins/EXAMPLE
.png";
Remove the whitespace from the end of the entered data to avoid this.
$username =~ s/\s+$//;
or
chomp($username);
$username will include the carriage-return you hit when you hit "enter" at the input prompt, so your exec call actually looks like two separate commands:
start www.s3.amazonaws.com/MinecraftSkins/$username
.png
Try chomp($username) to strip off that whitespace.
You have to whipe the newline at end of your input:
#!/usr/bin/perl
print "Type Username Here:
";
chomp ( $username = <> );
print "Link: http://s3.amazonaws.com/MinecraftSkins/$username.png
";
exec "start www.s3.amazonaws.com/MinecraftSkins/$username.png";
You could sanitize (ensure that user input contain only letters, numbers and/or the character underscore _, for sample) you variable by:
#!/usr/bin/perl
print "Type Username Here:
";
$username = $1 if <> =~ /^([a-z0-9_]*)$/i;
print "Link: http://s3.amazonaws.com/MinecraftSkins/$username.png
";
exec "start www.s3.amazonaws.com/MinecraftSkins/$username.png";
Better written (as #KeithThompson comment):
#!/usr/bin/perl
print "Type Username Here:
";
if ( <> =~ /^([a-z0-9_]*)$/i ) {
$username = $1
print "Link: http://s3.amazonaws.com/MinecraftSkins/$username.png\n";
exec "start www.s3.amazonaws.com/MinecraftSkins/$username.png";
} else {
print "Error input contain invalid characters\n";
};

Perl: How to "die" with no error message?

I run a simple file test in perl with the code below:
my $f1 = "$pre_file";
unless (-e $1) {
print "\n Pre_check file does not exists! \n";
die;
}
It prints the following output:
Pre_check file does not exists!
Died at ./huawei-postcheck line 81.
However I do not want the last line "Died at ./huawei-postcheck line 81.".
I want to to "die" with no error message.
Is it possible?
See the documentation for die.
If the last element of LIST does not end in a newline, the current
script line number and input line number (if any) are also printed,
and a newline is supplied.
So you can get die to work without printing anything by just using die "\n". But given that you have an error message, I can't see why you don't use that.
unless (-e $f1) {
die "\n Pre_check file does not exist!\n";
}
Of course, the difference is that the message will now go to STDERR rather than STDOUT. But that's probably the right place for it to go.
use exit instead of die.
You could just say
die "\n";
to suppress the message.
You probably want to exit 1 instead of dying then.
my $f1 = "$pre_file";
unless (-e $1) {
print "\n Pre_check file does not exists! \n";
exit 1;
}

PERL: reading Log file and cmd line input together

I have written a script which is reading some data from log file and transform the data to simpler form and writing it back to another file. the reading is done line by line with a delay of 5 seconds i.e. sleep(5).
Meanwhile, on the command line if a user enters 'suspend' (through STDIN) then the program went on to sleep unless 'resume' is not entered and then read the next line.
Since, with every iteration in the loop I am checking STDIN whether 'suspend' is entered or not by the user.
if not then read the next line from the file. but when my programs runs I have to at least hit a ENTER key, otherwise it does not picks the next line from the input log file albeit i put an if statement to check if STDIN is undefined or not.
I am not a perl expert and this the first time I am writing a code in PERL. infact i have never done this file parsing thing before :'-(
my code implementation is like this;
#!/usr/local/bin/perl
my $line_no = 0;my $cancel = 0; my $line = "";
my $curpos = 0; my $whence = 0;
my $log_file = "/var/log/tmp_nagios.log";
#open(LOGFILE, "+< $log_file")or die "Failed to open $log_file, $!";
my $inp_file = "/var/log/sec_input";
my $logbuffer="";
#open(LOGFILE, "+< $log_file")or die "Failed to open $log_file, $!";
my $in;
while(1){
print "in While (1) Pos: $curpos and Whence:$whence\n";
open(LOGFILE, "+< $log_file")or die "Failed to open $log_file, $!";
seek(LOGFILE, $curpos, $whence);
next if(eof(LOGFILE));
print "Beginning\n";
while(<LOGFILE>){
#chomp($in = <STDIN>);
#if(defined($in) && $in =~ /^suspend$/i){
### Problem here ###
if(defined(<STDIN>) && <STDIN> =~ /^suspend\n$/i){ ## checking if 'suspend' is entered
print "Suspend Mode";
do{
sleep(5);
}while(!(<STDIN> =~ /^resume\n$/i));
print "Resume now\n";
close(LOGFILE);
last;
}
else{
$line = $_;
if($line =~ m/^\[(\d+)\]\sCURRENT\sSERVICE\sSTATE:\s(\w+);(\w+|\_|\d+)+;(CRITICAL|OK);.+$/){
$logbuffer = "$1,$2-$3,$4\n";
print $logbuffer;
open(INPFILE, ">> $inp_file")or die "Failed! to open $inp_file, $!";
#print INPFILE $logbuffer;
close(INPUTFILE);
sleep(5);
$curpos = tell(LOGFILE);
$whence = 1;
}
}
}
print "\nRe openning the file from Pos=$curpos and Whence=$whence\n";
}
close(LOGFILE);
here is the sample log file (/var/log/tmp_nagios.log) data;
[1284336000] CURRENT SERVICE STATE: host1;event1;CRITICAL; s
[1284336000] CURRENT SERVICE STATE: host2;event1;CRITICAL; f
[1284336000] CURRENT SERVICE STATE: host3;event3;CRITICAL; g
[1284336000] CURRENT SERVICE STATE: host4;event4;CRITICAL; j
[1284336000] CURRENT SERVICE STATE: host5;event1;CRITICAL; s
[1284336000] CURRENT SERVICE STATE: host6;event1;CRITICAL; f
[1284336000] CURRENT SERVICE STATE: host7;event7;CRITICAL; s
Sorry guys! Typo mistake
In the beginning I said, 'my script is reading data from log file with a delay of 5 seconds i.e. sleep(5)'
but actually i forget to mention it in my code, therefore, uncomment this line: #sleep(3); and make 'sleep(5);'
thanks
If I have understood your question correctly: check out the Term::ReadKey CPAN Module.
You can use it to do non-blocking buffer reads. (If there is nothing in the buffer, your script does not pause for user input)
https://metacpan.org/pod/Term::ReadKey
You may also like to approach this problem slightly differently - using signals:
http://perldoc.perl.org/perlipc.html. You can have your program run normally, but capture interrupts (e.g. CTRL-C)
Alternatively, you could just use CTRL-Z and fg to make your script sleep and wake.
Another option is POE::Wheel::FollowTail for the log and POE::Wheel::ReadLine or Term::Visual for user input. Though this might be a little overkill

Here's an old school IF statement for you, but there is a problem

I have an IF statement in QBASIC... yes... QBASIC...
I have been teaching someone to program (I decided this would be nice and easy to see
how the syntax works).
...Anyway, I have this code:
CLS
start:
INPUT ">>", a$
PRINT a$
IF (INSTR(a$, "do you")) THEN
IF (INSTR(a$, "like")) THEN
IF (INSTR(a$, "cheese")) THEN PRINT "Yep, I like cheese":
IF (INSTR(a$, "music")) THEN PRINT "Depends, which genre?": GOTO musicGenre
ELSE IF (INSTR(a$, "hate")) THEN
IF (INSTR(a$, "cheese")) THEN PRINT "No, I like cheese"
END IF
END IF
END IF
musicGenre:
INPUT ">>", m$
SELECT CASE (m$)
CASE "pop"
PRINT "..pop! lol, baa baa"
CASE "rock"
PRINT "Rock is ok"
END SELECT
GOTO start
But when I type "do you like cheese?" it seems to only reply "Yep, I like cheese" every other time...
Could anyone shed some light on this?
note:
"do you like music?" works every time...
note 2:
Screenshot of the output:
Your code you provided appears correct.
Try one of the following:
If possible, send us a larger code sample. I'm guessing the error is outside the code you provided.
Output the input (a$) before the first IF to confirm your code will be working with the expected input.
In most languages, FALSE is zero and true is anything else. However, you may want to be more explicit with the following IF (INSTR(a$) > 0).
EDIT: You should put a goto start on any cheese result. Otherwise, it's going to the musicGenre code.
CLS
start:
INPUT ">>", a$
IF (INSTR(1, a$, "do you")) THEN
IF (INSTR(1, a$, "like")) THEN
IF (INSTR(1, a$, "cheese")) THEN PRINT "Yep, I like cheese"
IF (INSTR(1, a$, "music")) THEN PRINT "Depends, which genre?": GOSUB musicGenre
END IF
IF (INSTR(1, a$, "hate")) THEN
IF (INSTR(1, a$, "cheese")) THEN PRINT "No, I like cheese"
END IF
END IF
GOTO start
musicGenre:
INPUT ">>", m$
SELECT CASE (m$)
CASE "pop"
PRINT "..pop! lol, baa baa"
CASE "rock"
PRINT "Rock is ok"
END SELECT
RETURN
This program demonstrates parsing input and gosubs in Basic.
REM Cheese progran source:
CLS
DO
INPUT ">>", a$
a$ = LCASE$(a$)
PRINT a$
IF INSTR(a$, "do you") THEN
IF INSTR(a$, "like") THEN
IF INSTR(a$, "cheese") THEN
PRINT "Yep, I like cheese":
END IF
IF INSTR(a$, "music") THEN
PRINT "Depends, which genre?"
GOSUB MusicGenre
END IF
ELSE
IF INSTR(a$, "hate") THEN
IF INSTR(a$, "cheese") THEN
PRINT "No, I like cheese"
END IF
END IF
END IF
END IF
LOOP
END
MusicGenre:
INPUT ">>>", m$
a$ = LCASE$(a$)
SELECT CASE (m$)
CASE "pop"
PRINT "..pop! lol, baa baa"
CASE "rock"
PRINT "Rock is ok"
END SELECT
RETURN