How to Create a Flat File
Creating a flat text file is a good way to manage a small database with basic information, such as client phone numbers. However, if you plan to house more than just a few fields in your file, it is best to use MySQL or Oracle. This article shows you how to create a basic, flat-file text database that you can use on your Windows shell. It includes the most important steps in creating the input file only (you can create the output file another day). In an effort to keep it simple, these instructions only create a database that saves names and phone numbers. After practicing, you can create more fields if you like.
Things You'll Need
- Text editor
- Text file
Create the input program. Open a text file and save it as input.pl. This is where you will type your program. Reopen the program in your text editor.
Open the saved db.txt file for appending information by typing "open(DB,">>db.txt");" in the input.pl file. The ">>" symbols mean that you will append information to that file.
Get the input and send it to the text file. Typeprint "Client name: ";$name = <>;chomp($name);print "Client telephone: ;$phone = <>;chomp($phone);This captures the two bits of information that you want to send to the db.txt and saves them in two variables: name and phone, respectively.
Print to the db.txt file in an indicated format. You want the info to print out like the following in the txt file.Name: Mike Davis, Number: 555-5523Name:Jane Brooks, Number: 555-7878So type:print DB "Name:$name\,Number:$phone\n";
Open the shell and get into the c:\directory by typing "cd c:\" at the prompt.
Go to the directory where your script is saved, such as the "db" directory within the "bills" directory, "cd bills\db".
Input the information the program asks for. Since you are putting in two files, run the program twice. There is a way to keep it constantly running by using a while loop, but it gets a little more complex.
Check to see if you've successfully created a flat text database by opening the db.txt file. It should look just like the one in the image.
Add as many names as you like to your flat text database--even millions if your computer has enough memory. Try to add more fields to you database, such as address or ID numbers.
Tips & Warnings
- Make sure to put the ";" marks in the right place. The program will not run if they are not there.
- If you do make a while loop, use "Ctrl c" to stop the program if it starts to run infinitely.