A collection of data or information that are stored on a computer is known as a file. There is four classifications of files.
Text files.
Data files.
Program files.
Directory files.
Different types of files store different types of information. A file has a beginning and an end, we need a marker to mark the current position of the file from the beginning (in terms of bytes). While read and write an operation takes place on a file. Initially, the marker is at the beginning of the file. We can move the marker to any other position in the file. The new current position can be specified as an offset from the beginning the file.
File streams :
A stream refers to the flow of data ( in bytes) from one place to another ( from program to file or vice versa). There are two types of streams.
* Text stream
* Binary stream
Text stream :
* It consists of a sequence of characters.
* Each line of characters in the stream may be terminated by a newline character.
* Text streams are used for textual data, which has a consistent appearance from one environment to another (or) one machine to another machine.
Binary stream :
* It is a series of bytes.
* Binary streams are primarily used for non-textual data, which is required to keep the exact contents of the file.
program file:
usually, the program files contain the set of code. while we are writing the code in any editor IDE , after completed code we just save our program in a particular name with extension (extension which means, if you're writing a code in java, you should save your file as file.java , here, java perform as extension role) as your wish of the directory. this is called a program file.
Directory files :
A directory means the location where you are going to store your file. In LINUX or UNIX, you can store your file by using your terminal while we are writing the code in the terminal base. for this kind of case, first, you have to set your directory on your terminal afterward just start the writing editor and then, proceed with your code. don't forget while we are running our code in the windows operating system. It automatically show one window for saving the file , you have to select which directory place you are going to store your file and finally just terminate your save process.
Types of files:
* ASCII text files
* Binary files
ASCII text files :
A text file stores data in the form of alphabets,digits,and other special symbols by storing their ASCII values and are in a human readable format. For example, any file with a.txt , .c, etc. . .
Binary files :
A binary file is a sequence or a collection of bytes which are in a machine readable format. For example, files with .exe, .mp3, etc. . .
Naming a file:
A file is identified by its name, which is classified into two parts namely,
* File name - it consists of alphabets, digits and special characters also supported, but it only depends on the operating system we use.
* Extension - it describes the file type.
For For C file input you need to use a file pointer, which will let the program to keep track of the file being used of the file being accessed. A file pointer has the complete information about file being opened and processed such as, name of file, mode its opened in, starting buffer address, a character pointer that points into the character being read.
syntax : FILE *fptr
To perform any action (read, write, append) on a file, the file must be brought into the memory to storage device. The process of bringing the copy of the file to the memory to storage is called opening the file.
FILE ACCESSING BY PROGRAM:
In a file, the first operation is, reading the files, if any data part has been present on the file, it will print the whole content from the files.
|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define
size 500
int
main(){
//first have to give initializing the
file
FILE *fptr,*fptr1;
fptr=fopen("hii.txt","a");
if(fptr==NULL){
printf("error occured you cant
open\n");
exit(1);
}
// we are going to add the datas to the
file
char n[200];
int num,i;
printf("how may datas you want to
store :\n");
scanf("%d",&num);
printf("give the data for
storing\n");
for(i=1;i<num;i++){
fgets(n,size,stdin);
fputs(n,fptr);
}
fclose(fptr);
char c;
// just read the values
printf("now we are going to read the
files\n");
fptr1=fopen("hii.txt","r");
while((c=getc(fptr))!=EOF){
printf("%c",c);
}
fclose(fptr1);
}
|
here, I have given the code for storing the value add the data to the file. after you run this program on your compiler editor you can get the output screen then you have to give some data afterwards it will show the output on your screen .
file : hii.txt
ADVANTAGES && usage :
FILE system normally used for managing the user data. whenever you are more to any kind of work they will give the sample of the quest set in a text file or CSV file or any kind of file. you have to manipulate the code for opening the files. And you should some operation like appending the data and if needs mean we should write the data inside of the files. when you are moving to the advanced level of the coding your all the processes having related to the file system. In operating system labs, your more process has related to the file system. Some companies give coding contests related to the file system. they only give the input in the text file and your output should write in another text file. So file system is more important. be patient on these topics.
No comments:
Post a Comment