Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Comprehension of file operations FILEHANDLING: Input and output- concept of a file, text files and binary files, Formatted I/O,File I/O operations, example programs FILE: A

Comprehension of file operations FILEHANDLING: Input and output- concept of a file, text files and binary files, Formatted I/O,File I/O operations, example programs FILE: 
  • A File is a place on the disk where a group of related data is stored. Files are used for permanent storage of large amount of data. A file is a collection of related records.
  • C supports a number of functions that perform basic file operations. The basic operations on a file are
  1. Naming of a file
  2. Opening a file
  3. Reading a file
  4. Writing data to a file
  5. Closing a file
Naming a File:
  • File name is a name of the file specified for the operating system(OS). File name may contain two parts. That is it can have a primary name and an optional period followed by an extension name.
1. Primary name and 2. dot (.) with extension.
  • Example: example.c , sample.dat, duplicate.h
  • As we know primary name specifies name of the file and extension specifies the type of the file.
Opening a file:
  • To perform I/O operations on a file, the file must be opened. For opening a file, we must specify certain things about the file to the operating system. They are
  1. Filename
  2. Data Structure
  3. Purpose
  • The filename specifies which file is to be opened.
  • FILE is a data structure which is defined data type and available in stdio.h.
  • The purpose of the file is specified in terms of file opening modes. They are:
Operating Mode
Opening a file for reading purpose only
Opening a file for writing purpose only
Opening a file for adding data to the existing file
4.
read & write Opening a file for both reading andwriting purpose
write & read Opening a file for both writing and reading purpose
read & append Opening a file for both appendingand reading purpose
  • To declare and open a file:
  • Syntax:
FILE *fp; fp=fopen( filename,mode );
  • Example:
FILE *fptr; fptr = fopen( studinfo.dat, w); Closing a file :
  • After performing required operations on files, the already opened files must be closed. This can be done with fclose( ) library function.
  • Syntax: fclose (file-pointer);
  • Example: fclose(fptr);
Reading a file:
  • To read data from the file , several different functions are available. They are
S.No Function Purpose Syntax Example
1. fgetc() fgetc() function reads one character from the file through the pointer fgetc(file pointer) FILE *fptr; char ch; fptr=fopen(stud.dat, r); ch=fgetc(fptr);
2 fgets() fgets() function reads strings from the file through the pointer fgets(string name, length, filepointer) FILE *fptr; char name[30]; fptr=fopen(stud.dat, r); fgets(name, 30, fptr);
getw() getw() function reads integer values from the file through the pointer getw(file pointer) FILE *fptr; Int num; fptr=fopen(stud.dat, r); num=getw(fptr);
fscanf() fscanf() operation is similar to scanf() except that data is read from files through the file pointer fscanf(file pointer, format string, list of addresses); FILE *fptr; int rno; float marks; fptr=fopen(stud.dat, r); fscanf(fptr, %d%f, &rno, &marks);
Writing to a file:
  • To write data to the file , several different functions are available. They are
S.No Function Purpose Syntax Example
1. fputc() fputc() function writes one character to the file through the pointer fputc(ch, file pointer) FILE *fptr; char ch; fptr=fopen(stud.dat, w); fputc(ch, fptr);
2 fputs() fputs() function writes strings to the file through the pointer fputs(string name, filepointer) FILE *fptr; char name[30]; fptr=fopen(stud.dat, r); fputs(name, fptr);
putw() putw() function writes inter ger values to the file through the file pointer putw(integer, file pointer) FILE *fptr; Int num; fptr=fopen(stud.dat, r); putw(num,fptr);
fprintf() fprintf() operation is similar to printf() except that data is written to through the file pointer fprintf(file pointer, format string, list); FILE *fptr; int rno; float marks; fptr=fopen(stud.dat, r); fprintf(fptr, %d%f, rno, marks);
/* Program to demonstrate fputc() and fgetc() */ #include #include void main() { FILE *fptr; char text[100], ch; int i=0; clrscr(); printf(\"Enter a text:\"); gets(text); fptr = fopen(\"TEST\",\"w\") while(text[i]!=\'\\0\') fputc(text[i++],fptr); fclose(fptr); fptr = fopen(\"TEST\",\"r\") printf(\"The data from the file is as follows \"); while((ch=fgetc(fptr))!=EOF) printf(\"%c\",ch); getch(); } /* Program to demonstrate putw() and getw() */ #include #include void main() { FILE *fptr; int i=0, n, num; clrscr(); printf(\"Enter how many numbers are to be stored in a file:\"); scanf(\"%d\", &n); printf(\"Enter %d numbers one by one : \", n); fptr = fopen(\"TEST\",\"w\"); for(i=0;i{ scanf(\"%d\", &num); putw(num,fptr); } fclose(fptr); fptr = fopen(\"TEST\",\"r\"); printf(\"The data from the file is as follows \"); while((num=getw(fptr))!=EOF) printf(\"%d\\t\",num); getch(); } /* Program to demonstrate fprintf() and fscanf() */ #include #include void main() { FILE *fptr; char fname[100], ch; int i=0,rollno,n; int marks; clrscr(); printf(\"Enter how many students information are to be stored in a file:\"); scanf(\"%d\", &n); printf(\"Enter %d students rollno and marks one by one : \", n); fptr = fopen(\"TEST\",\"w\"); for(i=0;i{ scanf(\"%d%d\", &rollno,&marks); fprintf(fptr,\"%d%d\", rollno,marks); } fclose(fptr); fptr = fopen(\"TEST\",\"r\"); printf(\"The data from the file is as follows \"); printf(\"Roll Nimber \\t Marks \"); for(i=0;i{ fscanf(fptr,\"%d%d\", &rollno,&marks); printf(\"%d\\t%d \", rollno,marks); } getch(); } Types of file:-
  • Files are used for permanent storage of large amount of data on hard disk. Depending upon the format in which the data is stored files are primarily categorized into types they are
    1. Text File
    2. Binary File
Text File:-
  • A text file contains only text such as letter, numbers etc.,. Since data is to be stored in the binary format the textual information in the file is converted into binary format before storing it. But when a file is to be read by any text editor it is converted back to text format. When you open a text file with Notepad or some other, simple text editor then the information of the file can be read.
Binary File:-
  • A binary file stores information in the binary form. Since data is to be stored in the binary format there is no need of conversion to binary format before storing it. But when a file is to be read by any text editor it cant converted to text format. So a binary file cant be read through a text editor.
/* Program to demonstrate fwrite() and fread() */ #include #include struct s { int rollno; int marks; }; void main() { struct s a[5],b[5]; FILE *fptr; int i, n; printf(\"Enter how many students information are to be stored in a file:\"); scanf(\"%d\", &n); printf(\"Enter %d students rollno and marks one by one : \", n); fptr=fopen(\"file.txt\",\"wb\"); for(i=0;i{ scanf(\"%d%d\", &a[i].rollno,&a[i].marks); } fwrite(a,sizeof(a),1,fptr); fclose(fptr); fptr=fopen(\"file.txt\",\"rb\"); fread(b,sizeof(b),1,fptr); printf(\"The data from the file is as follows \"); printf(\"Roll Nimber \\t Marks \"); for(i=0;i{ printf(\"%d\\t%d \", b[i].rollno,b[i].marks); } fclose(fptr); getch(); } ERROR HANDLING DURING I/O OPERATIONS:
  • Errors may occur during I/O operations in a file. Commonly occurring errors are
  1. An attempt to read the file that doesnt exist.
  2. Trying to read the file beyond the end-of-file mark.
  3. Trying to use the file that is not opened.
  4. Trying to perform an operation on a file , when the file is opened for another purpose.
  5. Openning a file with an invalid filename.
/* Program to demonstrate ferror & feof */ #include #include void main() { FILE *fptr; int i=0, n, num; char fname[30]; clrscr(); fptr = fopen(\"test\",\"w\"); printf(\"Enter how many numbers are to be stored in a file:\"); scanf(\"%d\", &n); printf(\"Enter %d numbers one by one : \", n); for(i=0;i{ scanf(\"%d\", &num); putw(num,fptr); } fclose(fptr); printf(\"Enter the filename to be opened:\"); scanf(\"%s\", fname); fptr = fopen(fname,\"r\"); if(fptr==NULL) { printf(\"File cant be opened \"); getch(); exit(1); } printf(\"Enter how many numbers are to be printed from the file:\"); scanf(\"%d\", &n); printf(\"The data from the file is as follows \"); for(i=0; i{ num=getw(fptr); if(feof(fptr)) { printf(\" File creached to end \"); break; } else printf(\"%d\\t\",num); } getch(); } RANDOM ACCESS TO FILES:-
  • Random access to files can be achieved with the help of functions fseek, ftell, rewind available in the I/O library.
fseek():-
  • fseek( ) function sets the file pointer to the desired position.
  • Syntax: int fseek(FILE *fp, long int numbytes, int origin);
  • Here, fp is a file pointer, numbytes is the number of bytes from origin, which will become the new current position, and origin is one of the following macros:
Macro Name
Beginning of file SEEK_SET
Current position
End of file
  • Therefore, to seek numbytes from the start of the file, origin should be SEEK_SET. To seek from the current position, use SEEK_CUR, and to seek from the end of the file, use SEEK_END.
  • The fseek( ) function returns zero when successful and a nonzero value if an error occurs.
ftell():-
  • ftell() function returns the current location of the filepointer in a file.
  • Syntax: long int ftell(FILE *fp);
  • It returns the location of the current position of the file associated with fp. If a failure occurs, it returns 1.
rewind():-
  • rewind() function resets the file pointer to the start of the file.
  • Syntax: rewind(fp);
  • This function helps us to read a file more than once.
/* Program to demonstrate fseek(), ftell() and rewind() */ #include #include void main() { FILE *fp; long n; int i, data; char ch; clrscr(); fp=fopen(\"RANDOM\", \"w\"); printf(\"How many characters are to be entered : \"); scanf(\"%ld\",&n); for(i=1;i { fflush(stdin); scanf(\"%c\",&ch); putc(ch,fp); } printf(\"No of characters entered = %ld \",ftell(fp)); fclose(fp); fp=fopen(\"RANDOM\", \"r\"); printf(\"From which position the file is to be printed: \"); scanf(\"%ld\",&n); fseek(fp, n, 0); if(feof(fp)!=0) printf( \"File has reached to the end \"); else { printf(\"Data from the file after %ld position is as follows \", n); while(feof(fp)==0) printf(\"%c\\t\",getc(fp)); printf(\" \"); } rewind(fp); printf(\"Entire Data from the file is as follows \", n); while(feof(fp)==0) printf(\"%c\\t\",getc(fp)); printf(\" \"); getch(); }

Attachments:


Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Recommended Textbook for

Auditing and Assurance services an integrated approach

Authors: Alvin a. arens, Randal j. elder, Mark s. Beasley

14th Edition

133081605, 132575957, 9780133081602, 978-0132575959

More Books

Students also viewed these Programming questions

Question

Use R programming to compute: 2 ^ 7 / ( 2 ^ 7 - 1 )

Answered: 1 week ago

Question

How does the concept of hegemony relate to culture?

Answered: 1 week ago