Data Files

1. If errno contains a nonzero number, is there an error?

 

The global variable errno is used by many standard C library functions to pass back to your program an error code that denotes specifically which error occurred. However, your program should not check the value of errnoto determine whether an error occurred.

Usually, the standard C library function you are calling returns with a return code which denotes that an error has occurred and that the value of errno has been set to a specific error number. If no error has occurred or if you are using a library function that does not reference errno, there is a good chance that errno will contain an erroneous value. For performance enhancement, the errno variable is sometimes not cleared by the functions that use it.

You should never rely on the value of errno alone; always check the return code from the function you are calling to see whether errno should be referenced. Refer to your compiler’s library documentation for references to functions that utilize the errno global variable and for a list of valid values for errno.

 

2. What is a stream?

 

A stream is a continuous series of bytes that flow into or out of your program. Input and output from devices such as the mouse, keyboard, disk, screen, modem, and printer are all handled with streams. In C, all streams appear as files – not physical disk files necessarily, but rather logical files that refer to an input/output source. The C language provides five “standard” streams that are always available to your program. These streams do not have to be opened or closed. These are the five standard streams:

 

Name Description Example
stdin Standard Input Keyboard
stdout Standard Output Screen
stderr Standard Error Screen
stdprn Standard Printer LPT1: port
stdaux Standard Auxiliary COM1: port

 

Note that the stdprn and stdaux streams are not always defined. This is because LPT1: and COM1: have no meaning under certain operating systems. However, stdin, stdout, and stderr are always defined. Also, note that the stdin stream does not have to come from the keyboard; it can come from a disk file or some other device through what is called redirection. In the same manner, the stdout stream does not have to appear on-screen; it too can be redirected to a disk file or some other device. See the next FAQ for an explanation of redirection.

3. How do you redirect a standard stream?

 

Most operating systems, including DOS, provide a means to redirect program input and output to and from different devices. This means that rather than your program output (stdout) going to the screen, it can be redirected to a file or printer port. Similarly, your program’s input (stdin) can come from a file rather than the keyboard. In DOS, this task is accomplished using the redirection characters, < and >. For example, if you wanted a program named PRINTIT.EXE to receive its input (stdin) from a file named STRINGS.TXT, you would enter the following command at the DOS prompt:

C:>PRINTIT < STRINGS.TXT

Notice that the name of the executable file always comes first. The less-than sign (<) tells DOS to take the strings contained in STRINGS.TXT and use them as input for the PRINTIT program.

Redirection of standard streams does not always have to occur at the operating system. You can redirect a standard stream from within your program by using the standard C library function named freopen(). For example, if you wanted to redirect the stdout standard stream within your program to a file namedOUTPUT.TXT, you would implement the freopen() function as shown here:

 

 

Now, every output statement (printf()puts()putch(), and so on) in your program will appear in the fileOUTPUT.TXT.

 

4. How can you restore a redirected standard stream?

 

The preceding example showed how you can redirect a standard stream from within your program. But what if later in your program you wanted to restore the standard stream to its original state? By using the standard C library functions named dup() and fdopen(), you can restore a standard stream such as stdout to its original state.

The dup() function duplicates a file handle. You can use the dup() function to save the file handle corresponding to the stdout standard stream. The fdopen() function opens a stream that has been duplicated with the dup() function. Thus, as shown in the following example, you can redirect standard streams and restore them:

 

5. Can stdout be forced to print somewhere other than the screen?

 

Although the stdout standard stream defaults to the screen, you can force it to print to another device using something called redirection. For instance, consider the following program:

 

 

At the DOS prompt, instead of entering just the executable name, follow it with the redirection character >, and thus redirect what normally would appear on-screen to some other device. The following example would redirect the program’s output to the prn device, usually the printer attached on LPT1:

C:>REDIR > PRN

Alternatively, you might want to redirect the program’s output to a file, as the following example shows:

C:>REDIR > REDIR.OUT

In this example, all output that would have normally appeared on-screen will be written to the file REDIR.OUT.

 

6. What is the difference between text and binary modes?

 

Streams can be classified into two types: text streams and binary streams. Text streams are interpreted, with a maximum length of 255 characters. With text streams, carriage return/line feed combinations are translated to the newline \n character and vice versa. Binary streams are uninterpreted and are treated one byte at a time with no translation of characters. Typically, a text stream would be used for reading and writing standard text files, printing output to the screen or printer, or receiving input from the keyboard.

A binary text stream would typically be used for reading and writing binary files such as graphics or word processing documents, reading mouse input, or reading and writing to the modem.

7. How do you determine whether to use a stream function or a low-level function?

 

Stream functions such as fread() and fwrite() are buffered and are more efficient when reading and writing text or binary data to files. You generally gain better performance by using stream functions rather than their unbuffered low-level counterparts such as read() and write().

In multiuser environments, however, when files are typically shared and portions of files are continuously being locked, read from, written to, and unlocked, the stream functions do not perform as well as the low- level functions. This is because it is hard to buffer a shared file whose contents are constantly changing.

Generally, you should always use buffered stream functions when accessing nonshared files, and you should always use the low-level functions when accessing shared files.

 

8. How do you list files in a directory?

 

Unfortunately, there is no built-in function provided in the C language such as dir_list() that would easily provide you with a list of all files in a particular directory. By utilizing some of C’s built-in directory functions, however, you can write your own dir_list() function.

First of all, the include file dos.h defines a structure named find_t, which represents the structure of the DOS file entry block. This structure holds the name, time, date, size, and attributes of a file. Second, your C compiler library contains the functions _dos_findfirst() and _dos_findnext(), which can be used to find the first or next file in a directory.

The _dos_findfirst() function requires three arguments. The first argument is the file mask for the directory list. A mask of *.* would be used to list all files in the directory. The second argument is an attribute mask, defining which file attributes to search for. For instance, you might want to list only files with the Hidden or Directory attributes. The last argument of the _dos_findfirst() function is a pointer to the variable that is to hold the directory information (the find_t structure variable).

The second function you will use is the _dos_findnext() function. Its only argument is a pointer to the find_tstructure variable that you used in the _dos_findfirst() function. Using these two functions and the find_tstructure, you can iterate through the directory on a disk and list each file in the directory. Here is the code to perform this task:

 

9. How do you list a file’s date and time?

 

A file’s date and time are stored in the find_t structure returned from the _dos_findfirst() and_dos_findnext() functions.

The date and time stamp of the file is stored in the find_t.wr_date and find_t.wr_time structure members. The file date is stored in a two-byte unsigned integer as shown here:

 

Element Offset Range
Seconds 5 bits 0-9 (multiply by 2 to get the seconds value)
Minutes 6 bits 0-59
Hours 5 bits 0-23

 

Similarly, the file time is stored in a two-byte unsigned integer, as shown here:

 

Element Offset Range
Day 5 bits 1-31
Month 4 bits 1-12
Year 7 bits 0-127 (add the value “1980” to get the year value)

 

Because DOS stores a file’s seconds in two-second intervals, only the values 0 to 29 are needed. You simply multiply the value by 2 to get the file’s true seconds value. Also, because DOS came into existence in 1980, no files can have a time stamp prior to that year. Therefore, you must add the value “1980” to get the file’s true year value.

The following example program shows how you can get a directory listing along with each file’s date and time stamp:

 

 

Notice that a lot of bit-shifting and bit-manipulating had to be done to get the elements of the time variable and the elements of the date variable. If you happen to suffer from bitshiftophobia (fear of shifting bits), you can optionally code the preceding example by forming a union between the find_t structure and your own user-defined structure, such as this:

 

 

Using the preceding technique, instead of using bit-shifting and bit-manipulating, you can now extract date and time elements like this:

 

 

 

10. How do you sort filenames in a directory?

 

When you are sorting the filenames in a directory, the one-at-a-time approach does not work. You need some way to store the filenames and then sort them when all filenames have been obtained. This task can be accomplished by creating an array of pointers to find_t structures for each filename that is found. As each filename is found in the directory, memory is allocated to hold the find_t entry for that file. When all filenames have been found, the qsort() function is used to sort the array of find_t structures by filename.

The qsort() function can be found in your compiler’s library. This function takes four parameters: a pointer to the array you are sorting, the number of elements to sort, the size of each element, and a pointer to a function that compares two elements of the array you are sorting. The comparison function is a user-defined function that you supply. It returns a value less than zero if the first element is less than the second element, greater than zero if the first element is greater than the second element, or zero if the two elements are equal. Consider the following example:

 

 

This example uses the user-defined function named sort_files() to compare two filenames and return the appropriate value based on the return value from the standard C library function strcmp(). Using this same technique, you can easily modify the program to sort by date, time, extension, or size by changing the element on which the sort_files() function operates.

11. How do you determine a file’s attributes?

 

The file attributes are stored in the find_t.attrib structure member. This structure member is a single character, and each file attribute is represented by a single bit. Here is a list of the valid DOS file attributes:

 

Value Description Constant
0x00 Normal (none)
0x01 Read Only FA_RDONLY
0x02 Hidden File FA_HIDDEN
0x04 System File FA_SYSTEM
0x08 Volume Label FA_LABEL
0x10 Subdirectory FA_DIREC
0x20 Archive File FA_ARCHIVE

 

To determine the file’s attributes, you check which bits are turned on and map them corresponding to the preceding table. For example, a read-only hidden system file will have the first, second, and third bits turned on. A “normal” file will have none of the bits turned on. To determine whether a particular bit is turned on, you do a bit-wise AND with the bit’s constant representation.

The following program uses this technique to print a file’s attributes:

 

 

 

12. How do you view the PATH?

 

Your C compiler library contains a function called getenv() that can retrieve any specified environment variable. It has one argument, which is a pointer to a string containing the environment variable you want to retrieve. It returns a pointer to the desired environment string on successful completion. If the function cannot find your environment variable, it returns NULL.

The following example program shows how to obtain the PATH environment variable and print it on-screen:

 

13. How can I open a file so that other programs can update it at the same time?

 

Your C compiler library contains a low-level file function called sopen() that can be used to open a file in shared mode. Beginning with DOS 3.0, files could be opened in shared mode by loading a special program namedSHARE.EXE. Shared mode, as the name implies, allows a file to be shared with other programs as well as your own. Using this function, you can allow other programs that are running to update the same file you are updating.

The sopen() function takes four parameters: a pointer to the filename you want to open, the operational mode you want to open the file in, the file sharing mode to use, and, if you are creating a file, the mode to create the file in. The second parameter of the sopen() function, usually referred to as the “operation flag” parameter, can have the following values assigned to it:

 

Constant Description
O_APPEND Appends all writes to the end of the file
O_BINARY Opens the file in binary (untranslated) mode
O_CREAT If the file does not exist, it is created
O_EXCL If the O_CREAT flag is used and the file exists, returns an error
O_RDONLY Opens the file in read-only mode
O_RDWR Opens the file for reading and writing
O_TEXT Opens the file in text (translated) mode
O_TRUNC Opens an existing file and writes over its contents
O_WRONLY Opens the file in write-only mode

 

The third parameter of the sopen() function, usually referred to as the “sharing flag,” can have the following values assigned to it:

 

Constant Description
SH_COMPAT No other program can access the file
SH_DENYRW No other program can read from or write to the file
SH_DENYWR No other program can write to the file
SH_DENYRD No other program can read from the file
SH_DENYNO Any program can read from or write to the file

 

If the sopen() function is successful, it returns a non-negative number that is the file’s handle. If an error occurs, -1 is returned, and the global variable errno is set to one of the following values:

 

Constant Description
ENOENT File or path not found
EMFILE No more file handles are available
EACCES Permission denied to access file
EINVACC Invalid access code

 

The following example shows how to open a file in shared mode:

 

 

Whenever you are sharing a file’s contents with other programs, you should be sure to use the standard C library function named locking() to lock a portion of your file when you are updating it.

 

14. How can I make sure that my program is the only one accessing a file?

 

By using the sopen() function, you can open a file in shared mode and explicitly deny reading and writing permissions to any other program but yours. This task is accomplished by using the SH_DENYWR shared flag to denote that your program is going to deny any writing or reading attempts by other programs. For example, the following snippet of code shows a file being opened in shared mode, denying access to all other files:

 

 

By issuing this statement, all other programs are denied access to the SETUP.DAT file. If another program were to try to open SETUP.DAT for reading or writing, it would receive an EACCES error code, denoting that access is denied to the file.

 

15. How can I prevent another program from modifying part of a file that I am modifying?

 

If your C compiler library comes with a function named locking() that can be used to lock and unlock portions of shared files.

The locking function takes three arguments: a handle to the shared file you are going to lock or unlock, the operation you want to perform on the file, and the number of bytes you want to lock. The file lock is placed relative to the current position of the file pointer, so if you are going to lock bytes located anywhere but at the beginning of the file, you need to reposition the file pointer by using the lseek() function.

The following example shows how a binary index file named SONGS.DAT can be locked and unlocked:

 

 

Notice that before the record is locked, the record pointer is positioned to the 10th record (450th byte) by using the lseek() function. Also notice that to write the record to the file, the record pointer has to be repositioned to the beginning of the record before unlocking the record.

 

16. How can I avoid the Abort, Retry, Fail messages?

 

When DOS encounters a critical error, it issues a call to interrupt 24, the critical error handler. Your C compiler library contains a function named harderr() that takes over the handling of calls to interrupt 24. Theharderr() function takes one argument, a pointer to a function that is called if there is a hardware error.

Your user-defined hardware error-handling function is passed information regarding the specifics of the hardware error that occurred. In your function, you can display a user-defined message to avoid the ugly Abort, Retry, Fail message. This way, your program can elegantly handle such simple user errors as your not inserting the disk when prompted to do so.

When a hardware error is encountered and your function is called, you can either call the C library functionhardretn() to return control to your application or call the C library function hardresume() to return control to DOS. Typically, disk errors can be trapped and your program can continue by using the hardresume() function. Other device errors, such as a bat FAT (file allocation table) error, are somewhat fatal, and your application should handle them by using the hardretn() function. Consider the following example, which uses theharderr() function to trap for critical errors and notifies the user when such an error occurs:

 

 

In this example, a custom hardware error handler is installed named error_handler(). When the program attempts to access the A: drive and no disk is found there, the error_handler() function is called. Theerror_handler() function first checks to ensure that the problem is a disk error. If the problem is not a disk error, it returns 100 by using the hardretn() function. Next, the program pauses for one second and issues ahardresume() call to retry accessing the A: drive.

17. How can I read and write comma-delimited text?

 

Many of today’s popular programs use comma-delimited text as a means of transferring data from one program to another, such as the exported data from a spreadsheet program that is to be imported by a database program. Comma-delimited means that all data (with the exception of numeric data) is surrounded by double quotation marks (“”) followed by a comma. Numeric data appears as-is, with no surrounding double quotation marks. At the end of each line of text, the comma is omitted and a newline is used.

To read and write the text to a file, you would use the fprintf() and fscanf() standard C library functions. The following example shows how a program can write out comma-delimited text and then read it back in.

 

Leave a Reply

Your email address will not be published.