File and Streams
Streams, definition :
To keep data from the keyboard saved at secondary storage such as a data file. Streams is a sequence of character.File,
When a C program run, there are 3 standard stream activated.1. Standard Input Stream, controlling input stream from keyboard
2. Standard Output Stream, controlling output stream into the monitor
3. Standard Error Stream, controlling the error messaging
All of these streams associated with a file.
File Definition :
- File is a collection of record
- Record is a collection of field
- Field is a block of byte
- Byte is a collection of bit
File Operation, in C we perform 4 major operations on the file :
1. Creating a new file
Make a new text file to keep our data saved there
2. Opening an existing file
When opening the file that has our data in it, we use,
The syntax for opening a file in standard I/O is:
fptr = fopen("fileopen.txt","mode");
Mode is a feature that allow us to read, write, or add the data inside the file.
3. Closing a file
Don't forget when we open a file we must end it with a close
The Syntax for closing a file is :
fclose(fptr); //fptr is the file pointer associated with file to be closed.
4. Write or read on the existing file
There are the most used mode when opening a file :
- "r" "read" is used to read the data that is inside the file.
- "w" "write" is used to overwrite the data if there is a file, or to make a new file if the file doesn't exist.
- "a" "append" is used to add the data at the end if we want to add in a file.
Comments
Post a Comment