File handling


1. What is a console?
Ans:
Screen + keyboard

2. What is disk I/O?
Ans:
Reading data from a Disk file is called as Disk Input
Writing data to a Disk file is called as Disk Output

3. What is the benefit of file handling (Disk I/O)?
Ans:
i) To store results of a program permanently in a disk file.
ii) To store input for a program permanently in a disk file.
iii) Database applications ( dealing with records )

4. What is Binary mode file handling?
Ans:
Some data of ‘n’ bytes on RAM would be stored as exactly ‘n’ bytes on a Disk.
Eg: a student object of 46 bytes on RAM would be stored as exactly 46 bytes on a Disk.

The advantage is: as no format conversion is required, so execution would be faster.
fread();
fwrite();

5. What is random file accessing & what is the perquisite for it and why?
Ans:
The contents of a file can be accessed randomly by moving the file pointer to anywhere in a file.
Eg: fseek( fp, pos, SEEK_SET)

The prerequisite is it must be a binary mode file. (Otherwise behaves arbitrarily)

6. What happens when we open a file using fopen()?
Ans:
Step1: Searches for the file
Step2: If file not found then NULL is returned.
If found:
Step3: A buffer is reserved to it on RAM
Step4: A structure object is created (fields are set to some values, where one of  the fields points to the buffer)
Step5: Returns the base address of the FILE object to FILE pointer.

7. Why should we close a file?
Ans:
i)  To write the data in the buffer into the disk when programmer wants.
        ii) To reopen a file( particularly when each file operation is written as a separate function)
Eg : append(), read(), delete(), modify(), search() etc.

Even though we don’t close it, OS closes it at the end of the program.

8. What are the main areas where text and binary mode functions are different?
Ans:
i)   Storage of primitive types (int, floats) & secondary types (student, employee, book)
ii)  Representation of end of file (No end of file character for binary mode).
iii) Handling of a new line character (One byte in binary mode where as two bytes in text mode )

9. Explain the arguments of fwrite()
Ans:
fwrite( &s, sizeof(s), 1, fp );

Argument one: Base address of the object
Argument two: Size of the object
Argument three: Number of objects
Argument four: file pointer

10. What are the different file modes available?
Ans:
Text mode:
Mode: Operations that can be performed
r reading only
w writing only
a appending only
r+ reading / writing
w+ reading/writing
a+ reading/ appending( writing at the end)

Similarly for Binary mode

11. What is end-of-file character?
Ans:
A special character ( ASCII : 26 ) is inserted beyond the last character in a file, when it is created.

12. What are command line arguments?
Ans:
The arguments which are passed to a program( main() ) from the command prompt are called as command line arguments.

13. What does argc, argv stands for and mean in the line: 
Ans:
int main( int argc, char* argv[] )

Argument count                  Argument values
(contains the no. of arguments ( base addresses of each of the arguments(stings))
at the command prompt)

14.What does argv[0] contains?
Ans:
The program’s executable file name