files - Input/Output and Files 1 Introduction Data files...

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

View Full Document Right Arrow Icon
1 Input/Output and Files
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 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
Background image of page 2
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)
Background image of page 3

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

View Full DocumentRight Arrow Icon
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 4
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)
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Creating a Sequential File Typical file open modes:
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

files - Input/Output and Files 1 Introduction Data files...

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

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