Page 132 - DCAP408_WEB_PROGRAMMING
P. 132
Windows Programming
Notes you can follow these steps to change the default file association so that double-clicking a file will
open the program you choose instead of the last program you installed.
1. In Windows Explorer, navigate to a file of the type you want to associate (GIF, JPEG, etc.).
2. Click on its icon once to select it.
3. While holding the shift key down, right click on the icon. (Note: Holding the Shift key
down is not necessary in Windows 2000 and Windows XP.)
4. In the pop-up menu, choose Open With... A box will open asking you to choose a program
to open that file type. If you have a lot of software installed it could take a few moments
for this box to appear.
5. Pick a program from the list.
6. If the program you need isn’t in the list, choose [other] to navigate to another EXE file on
your hard drive.
7. If you always want that program to open these types of files, put a checkmark in the box
that says: Always use this program to open files of this type.
Notes
1. Make sure the program to which you associate a file type is capable of opening that
type of file. Images must be opened in an image editor or viewer, text documents
must be opened in a text editor or word processor, and so on.
2. You can also change file associations in Windows Explorer by going to View >
Folder Options > File Types.
8.1.2 Reading, Writing, Closing: Disk Files
Writing and reading to disk files is accomplished in much the same way as reading and writing
to the screen. The approach taken in C is to associate a stream with the file using fopen, and then
use fprintf and fscanf for reading and writing. These behave exactly like printf and scanf except
that they allow you to specify which stream they operate on (printf always uses stdout, while
scanf uses stdin). When you have finished using the stream you should close it with fclose.
fopen
fopen opens a file and associates a stream with it. (Declared in stdio.h.)
FILE *fopen(char *path, char *mode);
path is a string specifying the name of the file, while mode is a string indicating how the file is
to be opened, typically either “r” to read from the file or “w” to write to it. If the file cannot be
opened for some reason, fopen returns NULL.
FILE *my_file;
/* .. */
my_file = fopen(“datafile.txt”, “r”); /* open datafile.txt for reading */
if (my_file == NULL) {
fprintf(stderr, “Can’t open datafile.txt for reading”);
126 LOVELY PROFESSIONAL UNIVERSITY