Home » C and C++, Tech Corner

Data Files

14 October 2009 No Comments Posted By:Dileep
  • Data stored in program variables and arrays are temporary in nature. Such data is normally stored in the main memory(RAM) of the computer and it will be lost when the program. The best way to work with or process a large data is to store that data permanently in the computer’s memory.
  • To store data permanently we can use the secondary storage devices such as hard disks and magnetic tapes.
  • The data stored on the secondary storage devices is viewed as the data files.

File

  • A data file(or simply file) is a collection of logically related information.
  • Data files can be accessed for the purpose of reading data from them and writing data back into them.
  • The data read from the files are assigned to the variables in the program and similarly, the values of variables from the source program are written into the files.
  • There are two ways in which the data files are accessed for either reading data from or writing data into them. They are :
  1. Sequential Access(or linear) – from beginning to end of file.
  2. Random Access(or direct) – directly pointing to the specified location in a file.

File Declaration

  • Any file that is accessed for reading or writing purpose must be declared.
  • The syntax – FILE *pointer_to_file;

Opening a File

  • Before performing any operation, a data file must be opened.
  • The function fopen() is used for opening a file.
  • On Success, fopen() returns a file pointer. Otherwise, it returns NULL.


Access Modes

  • File access modes indicate the purpose of the file being opened.
  • A file can be opened for reading data from a file or for writing data into a file. Sometimes we may be adding data to the end of the already existing file.
Access Mode Status
w – Write-only mode The file is opened for writing. The file is created if it is not existing. If the file is already existing, then the earlier contents are overwritten.
r – Read-only mode The file is opened for reading. If the file does not exist then we can not open it for reading.
a – Append mode The file is opened for appending. That is, the file already exists, and it may have some content. When a file is opened in this mode, the file pointer points to the end of the file.
w+ – Read-Write mode The file is opened for both reading and writing. It destorys the previous data stored in the file.
r+ – Write-Read mode The file is opened for both reading and writing.
a+ – Append mode The file is opened for reading and writing. However, data is written at the end of the file.

Closing a File

  • The files that are opened for reading or writing purpose must be closed.
  • The fclose() is used for closing a file.
  • int fclose(pointer_to_file);
  • This closes the file pointed to by the file pointer.
  • Once it is closed we can reuse the file pointer for opening other files.
  • On success, fclose() returns 0. Otherwise, it returns EOF(End of File) value.

Writing Data to Files

  • fprintf() – to write the formatted data to the file.
  • fputc() – to print a single character to the file.
  • fputs() – to write a string(or array of characters) to the file.

Reading Data from Files

  • fscanf() – to read the formatted data from the file.
  • fgetc() – to read a single character from the file.
  • fgets() – to read a string from the file.

Random Access Files

  • C provides a set of functions for writing or reading data to or from the data file randomly.
  • fseek() – Repositions the file pointer of a stream.
  • ftell() – Returns the current file pointer.
  • rewind() – Takes the control back to the beginning of the file.
  • fseek() function – The function fseek() sets the file pointer associated with a stream to a new position.
  • int fseek(FILE *stream, long offset, int where);
  • On success, the fseek() function (if the pointer is successfully moved) returns 0. On failure, fseek() returns a non-zero value.
  • ftell() function – The ftell() function returns the current file pointer for stream.
  • long ftell(FILE *stream);
  • The value returned by ftell() can be used in a subsequent call to fseek()
  • On success, ftell() returns the current file pointer position. Otherwise, it returns -1L and sets errno(a variable indicating the type of error) to a positive value.

<<Previous Post

Related posts:

  1. Pointers
  2. Preprocessor Statements
  3. Strings(Character Arays)
  4. Functions
  5. C Language – Fundamentals

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>