day8 - Files and Libraries in the C Programming Language...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Basic Text File Access A file is a data target or data source that resides on disk and is completely independent from your C program. A file on disk exists whether or not your program is active in memory. It has a name of its own and a location of its own. In order to connect your C program to a file, you need to: 1) declare a file pointer variable in your program 2) connect that file variable to the actual disk file with an fopen function call 3) remember to break the connection when you are finished with the file by an fclose function call. In between steps 2 and 3, all communication between your program and the disk file is accomplished by referencing the file pointer variable and not by using the file’s disk name. The only place the file’s disk name is used is in the fopen function call. The fopen function determines the mode of communication that your program will use in communicating with the file. The file can be opened for reading ( using “r” mode) or opened for writing ( using “w” mode). Let’s assume that a text file named data.txt resides in the default directory on your disk drive. You want to open this file for reading (as an input source for your program). First, declare a file pointer variable: FILE * infile; Then in your code section, make an appropriate call to the fopen function. infile = fopen(“data.txt”, “r”); If your function call fails for any reason (misspelled name etc) the infile pointer will have the NULL value. If it succeeds, the value will not be NULL. This means that the open call can be placed in an if statement that checks for failure and notifies the person running your program as follows: Files in C - 1 Files and Libraries in the C Programming Language
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The above will gracefully terminate your program if the input file is not opened correctly and continue running if it is opened successfully. Assuming it opened correctly, your program can now communicate with the file by referencing the file pointer infile . Example To read a string variable called name from the file, fscanf(infile, “%s”, name); should work. There are other C functions that can read from a file opened in read mode. Most are listed in <stdio.h> The use of fscanf is identical to that of scanf except for the new first argument. The first argument to fscanf must be a file pointer. After you finish processing the file, close it with an appropriate call to the fclose function. The fclose function takes the file pointer as its only argument. fclose (infile); Now, suppose you want to open a file named output.txt to receive data from your program. Once again, you need a file pointer variable in your program to associate with the file. FILE * outfile; and an appropriate call to the function fopen to make the connection: command is potentially destructive.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 17

day8 - Files and Libraries in the C Programming Language...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online