Question

In: Computer Science

Write a program that creates a file called "data.dat" in the current directory. Prompt the user...

Write a program that creates a file called "data.dat" in the current directory. Prompt the user for five numbers, and write them, one at a time, on both the screen and into the file. Close the file, then open it again for reading only, and display the contents on the screen. Handle error conditions that may occur.

Please Need this to be done in PERL. Thanks

Solutions

Expert Solution

PERL code given below :-

open(DATA, ">data.dat") or die "Couldn't open file data.dat, $!"; # create file data.dat and write

for( $a = 0; $a < 5; $a = $a + 1 ) {
   print "Enter a number:\n";
   my $num = <STDIN>;
    chomp $num;   # get user input number
    push @arr, $num; # store number in array
   }
for( $a = 0; $a < 5; $a = $a + 1 ) {
   print "\n", $arr[$a];       # print number , one at a time
   print DATA $arr[$a],"\n";    # write number in data.dat file
   }
close DATA or "couldn't close";     # close the data.dat file
open(DATA, "<data.dat") or die "Couldn't open file data.dat, $!"; # open data.dat file for only Read
print "\n Read from file :\n";
while(<DATA>) {
   print "$_";           # print all the number from file
}
close DATA or "couldn't close"; # close the file

Sreeenshot of code and output :-


Related Solutions

Write a C++ program that creates a file called Readings.txt. Inside the file, your program must...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must create a list. The list is composed of integer double pairs. There is one pair per line. The integers are in sequence (0, 1, 2, 3, ...) beginning with zero and ending with some random value between 512 and 1024. The doubles should be random values between 50.000 and 90.000. The doubles only have 3 decimal places. The file should look like this (of...
Write a script that prompts the user for a pathname of a file or directory and...
Write a script that prompts the user for a pathname of a file or directory and then responds with a description of the item as a file along with what the user's permissions are with respect to it (read, write, execute).
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
In a file called NumbersToMonths.java, write a program that: Asks the user to specify a month...
In a file called NumbersToMonths.java, write a program that: Asks the user to specify a month as an integer between 1 and 12. It is OK for your program to crash when the user does not enter a valid integer. Prints out the name of the corresponding month. Prints out "This number does not correspond to a month." if the integer is less than 1 or greater than 12. For example: if the user enters 1, your program output should...
In a file called DivisibilityTests.java, write a program that: Asks the user to enter an integer....
In a file called DivisibilityTests.java, write a program that: Asks the user to enter an integer. It is OK for your program to crash when the user does not enter a valid integer. If the integer is less than 0, the program prints: "The number is negative." If the integer is divisible by 2 and by 3, the program prints: "The number is even and divisible by 3." If the integer is divisible by 2 but not by 3, the...
In a file called Conversions.java, write a program that: Asks the user to enter a double...
In a file called Conversions.java, write a program that: Asks the user to enter a double number. Stores that number into a variable called z. Casts variable z into an integer, and stores the result into a variable called z1. Creates a variable z2, and sets it equal to the integer closest to z. Creates a variable z3, and sets it equal to the floor of z. Creates a variable z4, and sets it equal to the ceiling of z....
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string....
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string. Prints out four different versions of that string: The original version of the string. The upper-case version of the string. The lower-case version of the string. A version where the character at position 0 capitalized, and all other characters in lower case. For example: if the user enters string "gaNDalF12 !! AB3w", your program output should look EXACTLY like this: Please enter a string:...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to enter two strings. First, the program prompts: Please enter a string. The program should read in the string, and prompts: Please enter another string. The program reads in two strings and it prints a menu to the user. The program asks for user to enter an option and performs one of the following: Here are the options on the menu: Option a: checks if...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT