21. C-RW One Line

C-Header Files: Previous                                                Next: C-Accessing Random File                                           

Reading and writing one line at a time (C-RW One Line): -A line can be read from a opened file. 

Two functions are used for reading a line:

1. fputs(): - this function is used to write a sequence of characters in given file. It takes two arguments: First, string is to be written. Second, file pointer.
Syntax: fputs(string, file pointer);
2. fgets(): - This function is used to read a sequence of characters until it reaches one of the following:

  • At the new line.
  • Maximum number of characters which are specified by the programmer.
  • At the end of the file(EOF).
It takes three arguments: First, string pointer where you want to receive string. Second, maximum number of characters to read. Third, file pointer.
Syntax: fgets(string pointer, characters, file pointer);

Eg:           #include
                void main( )
                                FILE *name;
                                char string1[50];
                                name = fopen(“first.txt”, “r”);
                                while(fgets(string1, 60, name)!= NULL)
                                                printf(“%s”, string1);
  1. Reading and writing one block at a time: - Data can be accessed in a block. Block is an array. If you want to read a block then number of elements are need to be specify.

Function for reading/writing a block is:
  1. fread()
  2. fwrite()
  1. fread(): -This function is used to read one or more structures and it takes four arguments: First, pointer location. Second, each structure has a size and size is need to be specified here, which must be in bytes. Third, number of item to be read. Fourth, file pointer variable.
Syntax: fread( pointer location, size, number of items, file pointer variable)
Eg:           #include
                void main( )
                                FILE *file;
                                struct student
                                                char name[20];
                                                int rollno;
                                struct student std;
                                file = fopen(“student.dat”, “r”);
                                if(file = = NULL)
                                                exit( );
                                                while(fread(&std, sizeof(std), 1, file) = = 1)
                                                                printf(“%s %d”, std.name, std.rollno);
2. fwrite(): -This function is same as fread() it takes the same number of argument and syntax, but instead of reading elements from an array it writes element to the array.
Syntax: fwrite(pointer location, size, number of item, pointer variable);
We can read one word i.e. an integer value from a file also.

Two functions are used for it:

1. getw(): - it is used to read integer value from specified file. It takes one argument i.e. Pointer to file.
Syntax: getw(file pointer);
2. putw(): - it is used to write integer value on specified file and takes two arguments.
Syntax: putw(integer value, file pointer);
First, integer value is to b written to file. Second, file pointer.
Now learn to Access Random files in C Programming.

C-Header Files: Previous                                                Next: C-Accessing Random File