Object Oriented Programming: Chapter 6: IO Systems and File IO

Unformatted I/O

C++ Object Oriented Programming

There are various functions for unformatted I/O and these are, 1. get and put 2. getline 3. ignore 4. write. Let us discuss these functions one by one along with the suitable C++ code.

Unformatted I/O

•  There are various functions for unformatted I/O and these are,

1. get and put

2. getline

3. ignore

4. write

Let us discuss these functions one by one along with the suitable C++ code.

 

1. get and put functions

• The get function is used for reading the data through keyboard and put function is for displaying the data on console.

• Suppose we want to read a character and write the character on the console then the syntax of these functions will be

get (char *)

get (void)

put(char ch)

• Note that, the get function will be written along with cin and put function will be along with the cout statement. Here is the illustration.

C++ Program

#include<iostream>

using namespace std;

int main()

{

       char ch;

       cout<<"\n Enter some character: ";

       cin.get(ch);

       cout<<"\n You have entered ...\n";

       cout.put(ch);

       return 0;

}

Output

Enter some character: a

You have entered ...

a

•  Just similar to accepting a single character and displaying it we can also use get and put functions for handling the stream of characters i.e. string. Following program illustrates it ‒

C++ Program

#include<iostream>

using namespace std;

int main()

{

char s;

cout<<"\n Enter a string: ";

while(s!='\n')

{

     cin.get(s);

     cout.put(s);

}

return 0;

}

Output

Enter a string: Hello friends

Hello friends

 

2. getline function

• If we need to read few sentences from a stream, the generally preferred way is to use getline() function. It can read till it encounters newline or sees a delimiter provided by user.

Syntax

getline (string& str, char delim);

getline (string& str);

• We can use getline function along with the cin. Following program shows the use of getline function to accept a line of text

C++ Program

#include<iostream>

using namespace std;

int main()

{

     char data[20];

     cout<<"\n Enter some text";

     cin.getline(data,20);//reading a sentence

     cout<<data; //displaying the sentence

     return 0;

}

Output

Enter some text Hello How are you?

Hello How are you?

• We can also accept few sentences using the getline function. In that case we need to specify y the terminating character, which will indicate that the line is getting terminated. Following program demonstrates this concept.

C++ Program

#include<iostream>

using namespace std;

int main()

{

    char data[50];

    cout<<"\n Enter some sentences(type q) to terminate

    cin.getline(data,50,'q');//reading a sentence

    cout<<data; //displaying the sentence

    return 0;

}

Output

Enter some sentences(type q) to terminate Hello

How are you?

Enjoying

and happy

q

Hello

How are you?

Enjoying

and happy

Program explanation: In above program, we have used getline function for accepting the number of lines, but we have specified a third parameter to this function as a terminating character. When user types this character, the acceptance of the lines from the user is terminated. The above program will terminate the acceptance of line either on getting 50 characters or on reading the terminating character 'q'.

 

3. ignore function

• The ignore function extracts the character from the input sequence and discard them. Consider following program,

C++ Programes

#include<iostream>

using namespace std;

int main()

{

      int num;

      char s[10];

      cout<<"\n Enter some number: ";

      cin>>num;

      cout<<"\n Enter some string:

      cin.getline(s,10);

      cout<<"\n You have entered ...\n";

      cout<<num;

      cout<<"\n"<<<s;

      return 0;

}

Output

Enter some number: 10

Enter some string:

You have entered...

10

• Note that, the above program does not allow you enter the text, because the cin is used to accept a number then it automatically stores the enter pressed by you after inputting the number. Hence instead of accepting the text, the newline is accepted as next input and the program does not allow the user to enter the string. To overcome this problem, we need to flush out the new line character '\n' from earlier cin statement. In such a case, we use ignore function along with cin. This is illustrated by following C++ program.

C++ Program

#include<iostream>

using namespace std;

int main()

{

    int num;

    char s[10];

    cout<<"\n Enter some number:";

    cin>>num;

    cin.ignore();

    cout<<"\n Enter some string: ";

    cin.getline(s,10);

    cout<<"\n You have entered ...\n";

    cout<<num;

    cout<<"\n"<<s;

    return 0;

}

Output

Enter some number: 10

Enter some string: hello

You have entered ...

10

hello

Program explanation: Note that in above program we have used cin.ignore() function which flushes the input stream and then using the getline function the next input can be read.

 

4. Write

The write function is used to write the data.

Syntax

write((const char *buf, stream,size num);

where buf represents the data to be written and num represents the size of that data. Following C++ program illustrates the use of write function.

C++ Program

#include<iostream>

#include<cstring>//for using strlen function

using namespace std;

int main()

{

    char str[20];

    int i;

    cout<<"\n Enter some string: ";

    cin.getline(str,20);

    int n=strlen(str);//strlen function calculates length of

    //string

    cout<<"\n You have entered following string...\n";

    cout.write(str,n);

    return 0;

}

Output

Enter some string: Hello friends!!

You have entered following string....

Hello friends!!

 

Object Oriented Programming: Chapter 6: IO Systems and File IO : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Unformatted I/O


Object Oriented Programming: Chapter 6: IO Systems and File IO



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation