How to Create a Flat File

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.

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.

Advertisement

Step 1

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.

Video of the Day

Step 2

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.

Advertisement

Step 3

Get the input and send it to the text file. Type print "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.

Advertisement

Step 4

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-5523 Name:Jane Brooks, Number: 555-7878 So type: print DB "Name:$name,Number:$phone\n";

Step 5

Open the shell and get into the c:\directory by typing "cd c:" at the prompt.

Advertisement

Step 6

Command prompt to call input.pl program

Go to the directory where your script is saved, such as the "db" directory within the "bills" directory, "cd bills\db".

Advertisement

Advertisement

Step 7

Inputting names and numbers in program

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.

Advertisement

Step 8

Names in the database

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.

Advertisement

Step 9

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.

Video of the Day

Advertisement

Advertisement

references & resources