BIL104E_w11 - BIL104E INTRODUCTION TO SCIENTIFIC AND...

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

View Full Document Right Arrow Icon
BIL104E INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTING (C) WEEK – 11
Background image of page 1

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

View Full DocumentRight Arrow Icon
Files and Streams 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 2
Creating Files First of all a pointer that points the file have to be created FILE *File_pointer; To open a file use fopen function that returns a FILE pointer (File_pointer) to file specified (filename) File_pointer = fopen (“filename.txt","w"); To close the file use function fclose that uses FILE pointer (File_pointer) as a parameter fclose(File_pointer);
Background image of page 3

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

View Full DocumentRight Arrow Icon
Modes of files Mode Description r Open an existing file for reading. w Create a file for writing. If the file already exists, discard the current contents. a Append; open or create a file for writing at the end of the file. r+ Open an existing file for update (reading and writing). w+ Create a file for update. If the file already exists, discard the current contents. a+ Append: open or create a file for update; writing is done at the end of the file.
Background image of page 4
Example1: opening and closing files -bash-3.2$ ls 1.c a.out -bash-3.2$ ./a.out file_ptr= 0 before opening file_ptr= 346529808 after opening file_ptr= 346529808 after closing -bash-3.2$ ls 1.c a.out example1.txt #include <stdio.h>
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 16

BIL104E_w11 - BIL104E INTRODUCTION TO SCIENTIFIC AND...

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

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