CSci 2101 Data Structures: Lab 13

For this lab use File Input/Output programs that we studied in class as samples and use the references to the APIs as needed.

Problem 1

Create a file data.txt that has integers, one per line. The first integer is the number of lines in the rest of the file.
Your task is to write a program that reads data from the file. The first integer is used to create an array to store all the remaining elements. The rest of the program uses a loop to read the remaining integers and store them in the array. Make sure to close the file after you have read all the data.
Print out the array at the end of the program.
For instance, if the file has data

3
-7
9
0
then the program will create the array of 3 elements and store -7, 9, and 0 in the array.

Problem 2

Write a program that reads data in a similar way as in Problem 1, but only the first line in the file is an integer, the rest of the lines have strings. For instance, your data may be

3
Apples are red...
Are oranges orange?
Bananas are sweet!
In your program you need to read the integer, create an array of strings, read the strings and store them in the array. Note that a string may have more than one word.
At the end of the program print out all the strings and the number of letters in each string.

Problem 3

Create a file that has some integers in it (one per line). Write a program to read all of these integers (until the end of the file) and to compute their sum. You don't need to store them in an array (and in fact you can't store them in an array since you don't know the length of the array until you have read all the data).

Problem 4

Modify the program that you wrote for problem 2: after you have read all the strings you need to open a file for writing and output 5 randomly chosen strings into the file. It's OK if the same string gets selected more than once.

Problem 5

Write a program that asks the user to input the file name (at the standard input), reads the file name, reads the data from that file, and prints it to the standard output. The first problem of Lab 2 has an example of reading data from the standard input. What happens if the file doesn't exist or is not readable? To make a file not readable, type chmod a-r file.txt, replacing file.txt with your file name (you have to type the command in the same directory as the file). To restore the reading permissions, type chmod a+r file.txt. Note that this sets the permission so that the file is visible to everyone. If you want it to be visible only to yourself, type chmod u+r file.txt.

Problem 6

Modify the program so that if the file doesn't exist or cannot be open for reading, the program asks the user to open another file name. The process continues until the user enters a valid name. When debugging this program, keep in mind that it has a loop that's potentially infinite. If you can't stop your program in any other way, you can stop it by typing Ctrl-C.

Problem 7

Experiment with reading files that are not in your current directory. Recall that UNIX notation for going one level up in the file tree is .., so if I want to read data from a file data.txt which is one level above the current directory, I can create a new file reader like this: new FileReader("../data.txt")
Please write in comments the cases that you tried and the results.
This is a lab from CSci 2101 course.