{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

1_Review_7-1 - CIS 15AG 1 Review Chapter 7 Text Files Data...

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

View Full Document Right Arrow Icon
CIS 15AG Review Chapter 7: Text Files Data File an external collection of related data treated as a unit (such as a file of temperatures, a file of books, etc.) Data can be created separately from a program, stored in some auxiliary storage devices, and accessed by programs when needed. Text Data File – consists of characters – data must be converted to internal formats – are organized around lines – are created and used by either text editors or programs Binary Data File – consists of internal computer format – no format conversion is necessary , therefore they are much faster to input/output – are organized around records – are created and used by programs only Declare a Data File FILE * fpData; // this is a file pointer: now it has no useful value // later it will store the address of a file Open a Data File – using fopen fpData = fopen( "DATA.TXT", "r"); The fopen function searches for a file and if found it prepares it for processing; the function has two arguments: the name of the data file, including the extension, and the mode: "r" - for reading; the data file must be created first; you always start reading a file from the beginning. It is an error to try to open a file that does not exist (for instance when you misspell its name). "w" - for writing; if there's no such file, it is created; if there is a file with this name, it is erased! "a" - for appending; if there's no such file, it is created; if there is a file with this name, new data will be added at the end. The fopen function returns the address of the file. If an error occurs when opening the file, the function returns NULL that represents an invalid address. This is the function that "connects" a variable in your program, the file pointer, with the actual data file. Once that the connection is established, you no longer need the name of the actual file: the other functions that need to deal with the file will use the file pointer and only the file pointer. Validation when opening a file: fpData = fopen( "DATA.TXT", "r"); if ( fpData == NULL ) { printf ("Error opening DATA.TXT for reading"); exit(101); // or any other number for now } 1
Background image of page 1

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

View Full Document Right Arrow Icon
CIS 15AG Review Chapter 7: Text Files Close a Data File – using fclose fclose(fpData); The fclose function terminates the "connection" established by fopen between your program and the actual data file. What happens if you forget to close a file you no longer need? Sometimes nothing happens (at the end of the program all files are closed automatically) and sometimes there might be some problems (for instance if you want to read data from a file and then append new data to it: you have to close it first, then open it again in append mode).
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.

{[ snackBarMessage ]}