Skip to content Skip to sidebar Skip to footer

How to Read a Whole File in C Using Getline

The C++ getline() is a standard library part that is used to read a string or a line from an input stream. Information technology is a office of the <string> header. The getline() role extracts characters from the input stream and appends information technology to the cord object until the delimiting character is encountered. While doing then the previously stored value in the string object str volition be replaced by the input cord if whatsoever.
The getline() function tin can be represented in ii means:

Syntax:

istream& getline(istream& is,             string& str, char delim);

2. Parameters:

  • is: It is an object of istream class and tells the function well-nigh the stream from where to read the input from.
  • str: It is a string object, the input is stored in this object after being read from the stream.
  • delim: It is the delimitation character which tells the function to stop reading further input later reaching this character.

Instance: To demonstrate the use of delimiter in the getline() part.

C++

#include  <iostream>

#include  <$.25/stdc++.h>

using namespace std;

#ascertain MAX_NAME_LEN threescore  // Maximum len of your proper noun can't be more than 60

#define MAX_ADDRESS_LEN 120  // Maximum len of your accost can't be more than 120

#define MAX_ABOUT_LEN 250 // Maximum len of your profession can't be more 250

int principal () {

char y_name[MAX_NAME_LEN], y_address[MAX_ADDRESS_LEN], about_y[MAX_ABOUT_LEN];

cout << "Enter your name: " ;

cin.getline (y_name, MAX_NAME_LEN);

cout << "Enter your City: " ;

cin.getline (y_address, MAX_ADDRESS_LEN);

cout << "Enter your profession (press $ to complete): " ;

cin.getline (about_y, MAX_ABOUT_LEN, '$' );

cout << "\nEntered details are:\north" << '\northward' ;

cout << "Proper name: " << y_name << endl;

cout << "Address: " << y_address << endl;

cout << "Profession is: " << about_y << endl;

}

Output:

Output

Note: In the above example if the #ascertain MAX_NAME_LEN  6, Then in this case if you cantankerous the defined limit then, in this case, your programme will stop execution and exit it's applicative for every macro that you accept used with getline() function. And yous'll get the output equally below:

C++

#include  <iostream>

#include  <bits/stdc++.h>

using namespace std;

#define MAX_NAME_LEN threescore  // Maximum length of your name tin can't exist more than sixty

#ascertain MAX_ADDRESS_LEN 120  // Maximum length of your address tin't exist more than 120

#ascertain MAX_ABOUT_LEN 250 // Maximum length of your profession tin't exist more than 250

int main () {

char y_name[MAX_NAME_LEN], y_address[MAX_ADDRESS_LEN], about_y[MAX_ABOUT_LEN];

cout << "Enter your name: " ;

cin.getline (y_name, MAX_NAME_LEN);

cout << "Enter your Urban center: " ;

cin.getline (y_address, MAX_ADDRESS_LEN);

cout << "Enter your profession (press $ to consummate): " ;

cin.getline (about_y, MAX_ABOUT_LEN, '$' );

cout << "\north\nEntered details are:\n\n" ;

cout << "Proper name: " << y_name << endl;

cout << "Address: " << y_address << endl;

cout << "Profession is: " << about_y << endl;

}

Output:

Output_2nd

 Here, it is understandable that the length of the name field was more than the defined limit that'southward why the plan stop execution and exit.

1. Syntax:

istream& getline (istream& is, string& str);

2. The second annunciation is almost the same equally that of the outset one. The only difference is, the latter have an delimitation character which is by default newline(\n)character.
Parameters:

  • is: It is an object of istream class and tells the function almost the stream from where to read the input from.
  • str: Information technology is a cord object, the input is stored in this object after being read from the stream.

Beneath program demonstrates the working of the getline() role
Example 1:

CPP

#include <iostream>

#include <cord>

using namespace std;

int main()

{

string str;

cout << "Please enter your name: \n" ;

getline(cin, str);

cout << "Hullo, " << str

<< " welcome to GfG !\n" ;

return 0;

}

Input:

Harsh Agarwal

Output:

Hello, Harsh Agarwal welcome to GfG!

Example 2: We tin use getline() function to split a sentence on the footing of a character. Allow'southward look at an example to understand how it can exist done.

CPP

#include <bits/stdc++.h>

using namespace std;

int principal()

{

cord South, T;

getline(cin, S);

stringstream X(S);

while (getline(X, T, ' ' )) {

cout << T << endl;

}

render 0;

}

Input:

Hi, Faisal Al Mamun. Welcome to GfG!

Output:

Howdy, Faisal Al Mamun. Welcome to GfG!

Caution :This function considers a new line or ('\n') character as the delimitation character and new line character is valid input for this function.
Example of how new line can cause problem is given below:
Example:

CPP

#include <iostream>

#include <string>

using namespace std;

int primary()

{

cord name;

int id;

cout << "Delight enter your id: \n" ;

cin >> id;

cout << "Please enter your name: \due north" ;

getline(cin, name);

cout << "Your id : " << id << "\n" ;

cout << "Hello, " << name

<< " welcome to GfG !\northward" ;

getline(cin, name);

cout << "Hello, " << name

<< " welcome to GfG !\due north" ;

render 0;

}

Input:

7 MOHIT KUMAR

Output:

Your id : 7 How-do-you-do,  welcome to GfG ! Hi, MOHIT KUMAR welcome to GfG !

Related Articles:

  • How to utilize getline() in C++ when there are bare lines in input?
  • getline() function and character assortment

This article is contributed by Harsh Agarwal and improved by Faisal Al Mamun. If yous like GeeksforGeeks and would like to contribute, you tin likewise write an commodity using write.geeksforgeeks.org or mail service your article to review-team@geeksforgeeks.org. Encounter your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you lot find anything wrong, or you want to share more than information well-nigh the topic discussed in a higher place.

Want to acquire from the best curated videos and exercise problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To consummate your preparation from learning a language to DS Algo and many more,  delight refer Consummate Interview Preparation Form .


shifletsexpround.blogspot.com

Source: https://www.geeksforgeeks.org/getline-string-c/

Post a Comment for "How to Read a Whole File in C Using Getline"