C'Language - [File Management]

♠ Posted by Unknown in at 01:18

File Management Functions:

1. fopen():

The fopen() function is used to open a file.
Syntax:
          <filepointer> = fopen(<”filename”>, <”open mode”>);

“filepointer” is the name of the file pointer that has been defined earlier.

“filename” is the name of the file that is to be opened. If a constant value is to be supplied as a file name, it is enclosed with in double quote marks “ “. A file name can also be supplied as a string variable argument.

“open mode” is the mode in which the file is to be opened. Various modes in which a file can be opened are.
         
1. “w”         write mode
2. “r”           read mode
3. “a”           append mode
4. “w+”       write-read mode
5. “r+”        read-write mode
6. “a+”        append or create new

2. fclose():

The fclose() function is used to close a previously opened file.
Syntax:
                   fclose(<filepointer>);

“filepointer” contains the return value of the corresponding fopen() function when the specified file was opened.

3. putc():

To write data into a file character by character, we make use of the putc() standard function in ‘C’. putc() function is analogous to the putchar() function.
Syntax:
                   putc(<character variable>, <filepointer>);

“character variable” contains the character which one wants to write to the file, which has already been opened in a mode that supports writing to file. This function is used when this file must be opened in write or append mode. Another function which has been used to write data into a file character by character is the fputc() function. fputc() and putc() function work in exactly the same manner.
Syntax:
                   fputc(<character variable>, <filepointer>);

4. getc():

To read a file character by character, we make use of the getc() standard function in ‘C’. getc() function is analogous to the getchar() function.
Syntax:
                   <character variable> = getc(<file pointer>);

“character variable” is one which we want to read a character from the file that has already been opened in a mode that supports the reading operation. This function is used when file is opened in read mode or read-write mode. ‘C’ also provides another function fgetc() to read a file character by character.
Syntax:
                   <character variable> = fgetc(<file pointer>);

5. feof():

‘C’ provides the feof() function, which returns the value true (non zero) if the end of file has been reached or returns false (zero).
Syntax:
                   feof(<filepointer>);

6. fprintf():

The fprintf() function is similar to printf() function. the printf() function is used for formatted output on the standard output device. In a similar manner the fprintf() function is used for formatted output to files.
Syntax:
                   fprintf(<filepointer>, <format specifier>, <variables>);

7. fscanf():

The fscanf() function is similar to the scanf() function. the scanf() function is used for formatted input from the standard input device. In a similar manner the fscanf() function is used for formatted input from files.
Syntax:
                   fscanf(<filepointer>, <format specifier>, <variables>);

8. fputs():

fputs() function is similar to puts() function. it writes a string into the file and places the new line character after writing a line in the file.
Syntax:
                   fputs(<string>, <filepointer>);

“string” is the character array that contains the text that is to be written into the file, accessed by “filepointer”.

9. fgets():

fgets() function is similar to gets() function, and is used to read a sting of character from the file specified.
Syntax:
                   fgets(<string>, <max_no_of_characters>, <file pointer>);

“string” is the name of the character array into which the text will be read from the file.

“max_no_of_characters” specifies the maximum number of character that will be read from the file when the function is called. At any time max_no_of_characters-1 characters will be read from the file and stored in string.

0 comments:

Post a Comment