Sams Teach Yourself C in 21 Days

(singke) #1
Using Disk Files 441

16


Windows (including NT, XP, and .NET), as well as most UNIX systems, permit file-
names up to 256 characters long.
Operating systems also differ in the characters that are permitted in filenames. In
Windows 95/98, for example, the following characters are not permitted:
/ \ : *? “ < > |

You must be aware of the filename rules of whichever operating system you’re writing
for.
A filename in a C program also can contain path information. The pathspecifies the
drive and/or directory (or folder) where the file is located. If you specify a filename with-
out a path, it will be assumed that the file is located at whatever location the operating
system currently designates as the default. It’s good programming practice to always
specify path information as part of your filenames.

If the path isn’t specified for a file, it is recommended that you assume the
file is in the same directory as the program being executed. You may want
to include programming logic to include the path of the current program.

Tip


On PCs, the backslash character is used to separate directory names in a path. For exam-
ple, in DOS and Microsoft Windows, the name
c:\data\list.txt
refers to a file named list.txt in the directory \DATA on drive C. Remember that the back-
slash character has a special meaning to C when it’s in a string. To represent the back-
slash character itself, you must precede it with another backslash. Thus, in a C program,
you would represent the filename as follows:
char *filename = “c:\\data\\list.txt”;
However, if when running a program, you enter a filename using the keyboard, enter
only a single backslash.
Not all systems use the backslash as the directory separator. For example, UNIX uses the
forward slash (/).

Opening a File ....................................................................................................


The process of creating a stream linked to a disk file is called openingthe file. When you
open a file, it becomes available for reading (meaning that data is input from the file to

26 448201x-CH16 8/13/02 11:13 AM Page 441

Free download pdf