260 likes | 308 Vues
Reading and Writing Text Files. MIS 3406 Department of MIS Fox School of Business Temple University. The Basics. Reading from a file Writing to a file Deleting a file. Reading from a file. Get set up to access the file system. Read the file, split it into records and store in an array.
E N D
Reading and Writing Text Files MIS 3406 Department of MIS Fox School of Business Temple University
The Basics • Reading from a file • Writing to a file • Deleting a file
Reading from a file Get set up to access the file system Read the file, split it into records and store in an array Step through the array to access the contents
Writing to a file Get set up to access the file system Write records to the file
Deleting a file Get set up to access the file system We can see the file exists Make sure the file exists before trying to delete it Delete the file Run the script The file is gone
Searching through a directory for files Get set up to access the file system Call fs.readdirSynch Step through the files in the array
GuessANumber.js (cont) Continue to work on your GuessANumber.js program. You should always be able to guess the correct number in seven guesses or less. Create a text file called insults.txt with a dozen insults. Have your program read this file in and store it in an array. Keep track of how many guesses the user makes and once the user makes more than seven incorrect guesses, display a randomly selected insult along with each additional incorrect guess the user makes.
FileDisplay.js Create a file containing a series of integers is named numbers.txt and write a program that displays all of the numbers in the file.
HeadFileDisplay.js Write a program that asks the user for the name of a file. The program should display only the first five lines of the file’s contents. If the file contains less than five lines, it should display the file’s entire contents.
TailFileDisplay.js Write a program that is passed the name of a file. The program should display only the last 10 lines of the file’s contents. If the file contains less than 10 lines, it should display the file’s entire contents.
LineNumbers.js Write a program that asks the user for the name of a file. The program should display the contents of the file with each line preceded with a line number followed by a colon. The line numbering should start at 1.
ItemCouner.js Create a file containing a series of names that is is named names.txt. Write a program that displays the number of names (records) that are stored in the file.
SumOfNumbers.js Write a program that reads all of the numbers stored in the numbers.txt file and calculates their total.
AverageOfNumbers.js Write a program that calculates the average of all the numbers stored in the file numbers.txt.
MyDir2.js Write a script that uses fs.readdirSync to find all of the files in a directory. Display the file name and file extension of each file separately as shown below: File Name: SampleTextFile File Extention: txt File Name: TextFileToWriteTo File Extension: txt
RandomNumberFileWriter.js Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file will hold.
RandomNumberFileReader.js Write another program that reads the random numbers from the file created in the previous challenge, displays the numbers, then displays the following data: The total of the numbers The number of random numbers read from the file
GolfScores.js • The Springfork Amateur Golf Club has a tournament every weekend. The club president has asked you to write two programs: • A program that will read each player’s name and golf score as keyboard input, then save these as records in a file named golf.txt. Each record will have a field for the player’s name and a field for the player’s score separated by a comma. • A program that reads the records from the golf.txt and will display: • The name of the golfer with the best score and their score. • The name of the golfer with the worst score and their score. • The average score.
AverageStepsTaken.js A Personal Fitness Tracker is a wearable device that tracks your physical activity, calories burned, heart rate, sleeping patterns, and so on. One common physical activity that most of these devices track is the number of steps you take each day. The steps.txt file contains the number of steps a person has taken each day for a year. There are 365 lines in the file, and each line contains the number of steps taken during a day. (The first line is the number of steps taken on January 1st, the second line is the number of steps taken on January 2nd, and so forth.) Write a program that reads the file, then displays the average number of steps taken for each month. (The data is from a year that was not a leap year, so February has 28 days.)
ChargeAccountValidation.js Write a program that reads the contents of the file charge_accounts.txt into an array. The program should then ask the user to enter a charge account number. The program should determine whether the number is valid by searching for it in the list. If the number is in the list, the program should display a message indicating the number is valid. If the number is not in the list, the program should display a message indicating the number is invalid.
DriversLicenseExam.js The local driver’s license office has asked you to create an application that grades the written portion of the driver’s license exam. The exam has 20 multiple-choice questions. The answer key is found in the file DriversExamAnswerKey.txt. Write a program that will read the student’s answers for each of the 20 questions from a text file and store the answers in another array. (Create your own text file to test the application.) After the student’s answers have been read from the file, the program should display a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It should then display the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions.
NameSearch.js GirlNames.txt contains a list of the 200 most popular names given to girls born in the United States from the year 2000 through 2009. BoyNames.txt contains a list of the 200 most popular names given to boys born in the United States from the year 2000 through 2009. Write a program that reads the contents of the two files into two separate arrays. The user should be able to enter a boy’s name, a girl’s name, or both, and the application will display messages indicating whether the names were among the most popular.
PopulationData.js USPopulation.txt contains the midyear population of the United States, in thousands, during the years 1950 through 1990. The first line in the file contains the population for 1950, the second line contains the population for 1951, and so forth. Write a program that reads the file’s contents into a list. The program should display the following data: The average annual change in population during the time period The year with the greatest increase in population during the time period The year with the smallest increase in population during the time period
WorldSeriesChampions.js WorldSeriesWinners.txt contains a chronological list of the World Series winning teams from 1903 through 2009. (The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2009. Note the World Series was not played in 1904 or 1994.) Write a program that lets the user enter the name of a team, then displays the number of times that team has won the World Series in the time period from 1903 through 2009.
Magic8Ball.js Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so”, “Yes, of course!”, “I’m not sure”, and so forth. The program should read the responses from the file into an array. It should prompt the user to ask a question, then display one of the responses, randomly selected from the list. The program should repeat until the user is ready to quit.