Why Do You Need To Know About Data Files?Facts About Data FilesCreating And Using Flat Files In C
Creating And Using Flat Files in Visual BasicProblems
If you work with a computer you work with files. Sometimes those files contain information about something you are writing - in a word processor, for example. Sometimes those files contain other information like results of calculations. Sometimes those files contain results of measurements you took in a laboratory or out in the field.
In this lesson we are going to examine data files. There are numerous good reasons why you need to understand some basic ideas about data files.
In both cases you need to know something about file structure and some details of the characteristics of data files. That's what this lesson is about.The amount of data you can store in a data file on a disk is determined by the precision of the measurements you take and the number of data points you want to record.
You may want to write programs that take and store data, and you need to understand a little bit about file structure when you do that.
Let's look at the life history of a typical data file.
It's pretty clear that data files are useful. We've come a long way from the days when we wrote data into a lab notebook for storage or kept a pile of computer printouts. So what do you need today?
Given a need to store data in a file,
Be able to use data files in general application programs.
Be able to write C or Visual Basic programs that will write data within a program into a file.
Be able to explain the structure of a file you created
in C or Visual Basic.Be able to determine file size as a function of measurement
precision and number of data points.
In this section we are going to discuss what a flat file looks like. Later sections will show specific program functions that will let you open and close flat data files and write data to the files you create. First we will examine how files are built up. Then we will look at details of creating files and writing data to files in some popular programs.
In order to manipulate data as described earlier you will need to understand some basic facts about how files can be constructed. In particular, you'll need to know the following.
You can store a large amount of data even on a single floppy disk.
Now, there are higher density disks that hold 100 megabytes or 250 megabytes,
so consider these problems.
How many 267 page books will fit on a 100 megabye disk?
|
|
|
|
|
|
|
|
|
Open a simple text editor. (If you are in Windows, that will be Notepad. In Unix it might be emacs.) Then do the following, and DO NOT type any extra characters!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Every line on the list above has an ASCII character. (Click here to see a more complete list of ASCII characters.) That includes the tab, carriage return and linefeed characters. If you want to explore this further you might want to check out your favorite word processor. Many word processors have a feature that allows you to make the non-printing characters (like tabs, etc.) visible. Check that out and make sure that you can see how each of these characters shows up. (You might find the carriage return and line feed lumped into a single character shown with a paragraph mark.)
We need to examine the file contents of the simple file in more detail than we have to this point. Here is what is important.
In BASICC3$="TEM"+CHR$(&HD)+CHR$(&HA)In C/C++
CALL IBWRT(DAU%,C3$)long dau;
ibwrt(dau, "TEM\r\n", 5);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The letters also have a numerical code. That's the first column in the table above. Every character - all 256 possible ASCII characters - correspond to one of the numbers fomr 0 to 255. That numerical correspondence allows us to identify each character with a number.
At this point you have the knowledge you need to understand how data can be stored in files. This isn't the entire story. Storing data as characters is often done, but it is also possible to store data in numerical formats. That's another topic. However, IEEE-488 instruments often transmit data using a character format, and that's something you can learn about in another lesson.
C has numerous functions for creation and deletion of files, as well as functions that open your files, write data to them and close them.
In this section we'll give you some functions we have written that allow you to create your own data files in a flat file format. You can embed these functions in programs that read laboratory data, and you can use these functions to create files with lab data that you can open in spreadsheets and analysis programs. With that capability you can then plot and analyze your lab data.
The functions we will give you will be the simplest possible. You can then modify these functions to "personalize""them for your own purposes.
The sequence of operations in a typical program might be the following.
FILE *file; /* Declare a pointer to a FILE variable */NOTE: The "w+" opens the file to write data to.
/* Defined as a global to be available to */
/* other functions */
void OpenDataFile()
{
/* Subroutine to open a data file named "DATAFILE.TXT" in the same directory as the executable file.*/char file_name[20] = "DATAFILE.TXT";
file = fopen (file_name, "w+");
/* open data file "datafile.txt" */
}
Here's a function that will write data to the file you opened. Again, fprintf is a standard ANSI C function. WriteDataToFile writes one data point, a tab character, the next data point and a carriage return/line feed.
void WriteDataToFile(float x_data, float y_data)After you are finished writing all the data to the file, you need to close the file. Here's a function for that.
{
/* Subroutine to print values into the opened file. "present_time" is */
/* printed in one column, and "temperature" is printed in another. */
/* "present_time" and "temperature" must be sent down from function */
/* main. */fprintf(file, "%3f\t", x_data); /* write x_data to data file */
fprintf(file, "%.3f\n", y_data); /* write y_data to data file */
}
void CloseDataFile()That's all there is to it. These three functions - although somewhat bare bones - will suffice to get a data file for you. The precautions you need to take are:
{
/* Subroutine to close the data file named "DATAFILE.TXT" on the "A:" drive */fclose (file); /* close data file "a:datafile.txt" */
}
Visual Basic has numerous functions for creation and deletion of files, as well as functions that open your files, write data to them and close them.
In this section we'll give you some functions we have written that allow you to create your own data files in a flat file format. You can embed these functions in programs that read laboratory data, and you can use these functions to create files with lab data that you can open in spreadsheets and analysis programs. With that capability you can then plot and analyze your lab data.
The functions we will give you will be the simplest possible. You can then modify these functions to "personalize" them for your own purposes.
The sequence of operations in a typical program might be the following.
Open "DATAFILE.TXT For Output As #99The Open statement will open a file with the name specified immediately after the word "Open". Normally you will open the file for Output, and you can assign it a number - to be used later in Print and/or Write statements. (Here we used #99.) You can also specify complete path information if the file will be located in another directory other than the one your programming is running in.
After you've opened the file, write data to it using something like this.
Print #99, Xdata, YdataWhen you're done writing data to your file, close it. Here's the code.
Close #99There are better ways to specify a data file name. You can create A file I/O box using the standard FileListBox, DirListBox and DriveListBox tools. If you're good at Visual Basic, take a shot at that.