C notes On Data file

Data Files 
Many applications require that information be written to or read from an auxiliary memory device. Such information is stored on the memory device in the form of data files. Thus data files allow us to store information permanently, to access and alter that information whenever necessary. In C, an extensive set of library functions is available for creating and processing data files. 
There are two different types of data files, called stream-oriented (or standard) data files, and system-oriented (or low-level) data files. Stream-oriented data files are generally easier to work with and are therefore more commonly used. 
Stream-oriented data files can be subdivided into two categories: text files and unformatted data files. The text file consists of consecutive characters. These characters can be interpreted as individual data items, or as components of strings or numbers. The manner in which these characters are interpreted is determined either by the particular library functions used to transfer the information, or by format specifications within the library function.
The unformatted data files organize data into blocks containing contiguous bytes of information. These blocks represent more complex data structures, such as arrays and structures. A separate set of library function is available for processing these data files. These library functions provide single instructions that can transfer entire arrays or structures to or from data files.
 System-oriented data files are more closely related to the computer’s operating system than stream-oriented data files. They are somewhat more complicated to work with though their use may be more efficient for certain kinds of applications. A separate set of procedures, with accompanying library functions, is required to process such data files. There is also another type of file called binary file. These files are useful to handle file containing machine language contents, for example .exe or .com files. The files which are stored in binary format differ significantly from data files. 

example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
int roll, marks;
char name[30], address[30];
};
int main()
{
int n,i;

    struct student a ;
    FILE *fp, *fpp;
printf("enter how many student:");
scanf("%d",&n);
fp= fopen("text.txt","w");

for(i=0;i<n;i++)
{
printf("enter %d roll number,name,address,marks:",i+1);
scanf("%d %s %s %d",&a.roll, &a.name, &a.address, &a.marks);
}
fclose(fp);

fp=fopen("text.txt","r");
printf("roll \tname\t\taddress\tmark\n");
for(i=0;i<n;i++)
printf("%d\t%s\t\t%s\t%d\n",a.roll,a.name,a.address,a.marks);

fclose(fp);
getch();

}

No comments

Powered by Blogger.