Write and execute a C program to find the average of numbers stored in sequential access file.
Answers
Explanation:
The simplest way that C programming information is stored in a file is sequential, one byte after the other. The file contains one long stream of data.
File access in C is simply another form of I/O. Rather than go to the display, the input or output goes into a file. A file is opened by using the open() function:
handle = open(filename,mode);
The fopen() function requires two arguments, both strings. The first is a filename; the second is a mode. The fopen() function returns a filehandle, which is a pointer used to reference the file. That pointer is a FILE type of variable.
After the file is open, you use the handle variable to reference the file as you read and write. The file I/O functions are similar to their standard I/O counterparts, but with an f prefix. To write to a file, you can use the fprintf(), puts(), pitcher(), and similar functions. Reading from a file uses the fscanf(), fgets(), and similar functions.
You close the file by using the close() function with the filehandle as its argument.
How to write text to a file
#include
#include
int main()
{
FILE *fh; fh=fopen("hello.txt","w"); if(fh==NULL)
{
puts("Can't open that file!"); exit(1);
}
fprintf(fh,"Look what I made!n"); fclose(fh); return(0);
}