Data Files |
Here are a few things you can do after you have
copied the data and gotten it onto the computer clipboard.
- Start a word processor on
your computer and paste the data into the word processor.
- Start a spreadsheet and paste
the data into the spreadsheet.
Putting data on a clipboard
and pasting it into an application is one way to get data into a program.
However, in some cases you may need to do something else to get the data into a
program. Analysis programs like Mathcad and Matlab can also load this data, but
each program has a special way of getting the data into the workspace so it can
be analyzed.
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.
- When you type you generate
characters. When you type a ""3"" you generate a character that is put into
the editor for later storage in the file.
- Even when you use the
""Enter"" key you are typing a character or two. You are entering a
carriage return and/or a new line character, and each has its' own ASCII
representation.
- When you store the data in a
file, you are storing characters - the characters that you typed.
Tabs, carriage returns and
line feeds and other characters are ASCII characters, and you can write those
characters into files from all popular programming languages. Also you can
write strings of characters in other contexts. Here are some programming
segments that write a string ""TEM"" followed by a carriage return and line
feed. In these examples, a function is used to write the string to an
instrument to cause it to measure a temperature.
In BASIC
C3$="TEM"+CHR$(&HD)+CHR$(&HA)
CALL
IBWRT(DAU%,C3$)
In C/C++
long dau;
ibwrt(dau,
"TEM\r\n", 5);
Some special
characters are listed below.
ASCII Character
Number |
Name
|
BASIC form
|
C form
|
9
|
Tab
|
CHR$(&H9)
|
\t
|
10
|
LF
|
CHR$(&HA)
|
\n
|
13
|
CR
|
CHR$(&HD)
|
\r
|
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.
Creating
And Using Flat Files in C
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.
- Open a data file so that you
can write data to it.
- Write data to the file you
have open.
- Close the data file.
Here's a function that
will open a file named DATAFILE.TXT on the A drive. If the file doesn't already
exist, fopen creates it automatically. The function,
fopen, used here is a
standard, ANSI C, function. You can select the text and use
Ctrl+C to copy the function
if you are in a Windows system.
FILE *file;
/* Declare a pointer to a FILE variable */
/* 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" */
}
NOTE: The "w+" opens the file to
write data to.
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)
{
/* 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 */
}
After you are finished
writing all the data to the file, you need to close the file. Here's a function
for that.
void CloseDataFile()
{
/* Subroutine to close the
data file named "DATAFILE.TXT" on the "A:" drive */
fclose
(file); /* close data file "a:datafile.txt" */
}
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:
- Remember to keep the FILE
pointer - *file - outside of all functions so that it is available to all
the functions.
- If you use this function
twice (if you run your program twice, for example) the file will be written
over, and you will lose your first data set. To get around this, rename the
first file immediately.
Creating
And Using Flat Files in Visual Basic
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 a data file so that you
can write data to it.
- Write data to the file you
have open.
- Close the data file.
First, you need to be able to
open a file. That's simple in Visual Basic. Here's the code.
Open "DATAFILE.TXT For
Output As #99
The 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, Ydata
When you're done writing data
to your file, close it. Here's the code.
Cl99ose #
There 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.
|