Share getline c++,

vylantrandan

New member
#C ++, #Getline, #C ++ IO, #C ++ lập trình, #C ++ Hướng dẫn ## GetLine trong C ++

** GetLine là gì? **

GetLine là một hàm trong thư viện tiêu chuẩn C ++ đọc một dòng văn bản từ luồng đầu vào.Nó thường được sử dụng để đọc đầu vào từ bảng điều khiển hoặc từ một tệp.

** Cú pháp **

Cú pháp cho chức năng GetLine như sau:

`` `C ++
std :: getline (luồng, chuỗi);
`` `

Trong đó `stream` là luồng đầu vào và` String` là biến sẽ lưu trữ dòng văn bản được đọc.

**Thông số**

Tham số `stream` có thể là bất kỳ đối tượng nào thực hiện giao diện` std :: istream`.Điều này bao gồm các đối tượng như `std :: cin`,` std :: ifstream` và `std :: StringStream`.

Tham số `String` là biến sẽ lưu trữ dòng văn bản được đọc.Biến này phải là đối tượng `std :: String`.

** Giá trị trả lại **

Hàm GetLine trả về số lượng ký tự được đọc từ luồng đầu vào.Nếu kết thúc của tệp, hàm trả về `-1`.

**Ví dụ**

Mã sau đây cho thấy cách sử dụng chức năng GetLine để đọc một dòng văn bản từ bảng điều khiển:

`` `C ++
#include <Istream>

int main () {
// Tạo một chuỗi để lưu trữ đầu vào.
STD :: Đầu vào chuỗi;

// Nhận một dòng đầu vào từ bảng điều khiển.
std :: getline (std :: cin, input);

// In đầu vào vào bảng điều khiển.
std :: cout << "bạn đã nhập:" << input << std :: endl;

trả lại 0;
}
`` `

** Đầu ra **

Khi mã này được chạy, đầu ra sau được tạo ra:

`` `
Bạn đã bước vào: Xin chào Thế giới!
`` `

## Người giới thiệu

* [C ++ getline] (https://www.cplusplus.com/reference/istream/getline/)
* [Hướng dẫn C ++ IO] (28.1 — Input and output (I/O) streams – Learn C++)
* [Hướng dẫn lập trình C ++] (C++ Tutorial)
=======================================
#C++, #Getline, #C++IO, #C++Programming, #C++Tutorial ## Getline in C++

**What is Getline?**

Getline is a function in the C++ standard library that reads a line of text from an input stream. It is typically used to read input from the console or from a file.

**Syntax**

The syntax for the Getline function is as follows:

```c++
std::getline(stream, string);
```

where `stream` is the input stream and `string` is the variable that will store the line of text that is read.

**Parameters**

The `stream` parameter can be any object that implements the `std::istream` interface. This includes objects such as `std::cin`, `std::ifstream`, and `std::stringstream`.

The `string` parameter is the variable that will store the line of text that is read. This variable must be a `std::string` object.

**Return value**

The Getline function returns the number of characters that were read from the input stream. If the end of the file is reached, the function returns `-1`.

**Example**

The following code shows how to use the Getline function to read a line of text from the console:

```c++
#include <iostream>

int main() {
// Create a string to store the input.
std::string input;

// Get a line of input from the console.
std::getline(std::cin, input);

// Print the input to the console.
std::cout << "You entered: " << input << std::endl;

return 0;
}
```

**Output**

When this code is run, the following output is produced:

```
You entered: Hello world!
```

## References

* [C++ Getline](https://www.cplusplus.com/reference/istream/getline/)
* [C++ IO Tutorial](https://www.learncpp.com/cpp-tutorial/input-and-output/)
* [C++ Programming Tutorial](https://www.tutorialspoint.com/cplusplus/index.htm)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top