put() and get() Functions

Enter a reason for deleting this comment

put() and get() Functions

The function put() writes a single character to the associated stream. Similarly, the function get() reads a single character from the associated stream. The program requests for a string. On receiving the string, the program writes it, character by character, to the file using the put() function in a for loop, Note that the length of the string is used to terminate the for loop.

The program then displays the contents of the file on the screen. It uses the function get() to fetch a character from the file and continues to do so until the end=of-file condition is reached. The character read from the file is displayed on the screen using the operator <<.

#include <iostream.h>
#include <fstream.h>
#include <string.h>

int main()
char string[80];

cout << “Enter a string \n”;
cin >> string;

int len = strlen(string);
fstream file;                   // input and output stream“TEXT”, ios:: in | ios “” out);

for (int i=o; i<len; i++)
file.put (string [i]);          // put a character to file

file.seekg(0);                  // go to the start

char ch;
while (file)
        file.get(ch);           // get a character from file
        cout << ch;             // display it on screen
return 0;