files - Introduction Data files When you use a file to...

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

View Full Document Right Arrow Icon
1 1 Input/Output and Files 2 Introduction Data files – When you use a file to store data for use by a program, that file usually consists of text (alphanumeric data) and is therefore called a text file. – Can be created, updated, and processed by C programs – Are used for permanent storage of large amounts of data • Storage of data in variables and arrays is only temporary 3 Files and Streams • C views each file as a sequence of bytes – File ends with the end-of-file marker • Stream created when a file is opened – Provide communication channel between files and programs – Opening a file returns a pointer to a FILE structure • Example file pointers: stdin - standard input (keyboard) stdout - standard output (screen) stderr - standard error (screen) 4 Files and Streams • Read/Write functions in standard library fgetc • Reads one character from a file • Takes a FILE pointer as an argument fgetc( stdin ) equivalent to getchar() fputc • Writes one character to a file • Takes a FILE pointer and a character to write as an argument fputc( 'a', stdout ) equivalent to putchar( 'a' ) fscanf / fprintf • File processing equivalents of scanf and printf
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 5 Creating a Sequential File • Creating a File FILE *myPtr; • Creates a FILE pointer called myPtr myPtr = fopen("myFile.dat", openmode ); • Function fopen returns a FILE pointer to the file specified • Takes two arguments – file to open and file open mode • If open fails, NULL returned fprintf • Used to print to a file • It is like printf, except first argument is a FILE pointer (pointer to the file you want to print in) 6 Creating a Sequential File • Typical file open modes: Mode Description
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 / 5

files - Introduction Data files When you use a file to...

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