Share đọc và ghi file txt trong c++,

hongkhanh238

New member
#C ++, #TXT, #file, #IO, #Programming ## Đọc và ghi các tệp TXT trong C ++

Các tệp TXT là một cách đơn giản và phổ biến để lưu trữ dữ liệu văn bản.Chúng thường được sử dụng để lưu trữ dữ liệu cấu hình, tệp nhật ký và dữ liệu dựa trên văn bản khác.Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách đọc và ghi các tệp TXT trong C ++.

### Đọc tệp TXT

Để đọc tệp TXT trong C ++, bạn có thể sử dụng hàm `fopen ()` để mở tệp ở chế độ đọc.Khi tệp được mở, bạn có thể sử dụng hàm `fscanf ()` để đọc dữ liệu từ tệp.Hàm `fscanf ()` có ba đối số: con trỏ tệp, chuỗi định dạng và biến để lưu trữ dữ liệu.

Ví dụ: mã sau đọc dòng đầu tiên của tệp TXT và lưu trữ nó trong một biến chuỗi:

`` `C ++
#include <Istream>
#include <Fstream>

sử dụng không gian tên STD;

int main () {
// Mở tệp ở chế độ đọc.
tệp fstream ("file.txt", iOS :: in);

// Đọc dòng đầu tiên của tệp.
chuỗi chuỗi;
fscanf (tệp, "%s \ n", dòng);

// In dòng vào bảng điều khiển.
cout << line << endl;

trả lại 0;
}
`` `

### Viết tệp TXT

Để ghi tệp TXT trong C ++, bạn có thể sử dụng hàm `fopen ()` để mở tệp ở chế độ ghi.Khi tệp được mở, bạn có thể sử dụng hàm `fprintf ()` để ghi dữ liệu vào tệp.Hàm `fprintf ()` có ba đối số: con trỏ tệp, chuỗi định dạng và dữ liệu cần ghi.

Ví dụ: mã sau viết chuỗi "Hello World!"đến một tệp TXT:

`` `C ++
#include <Istream>
#include <Fstream>

sử dụng không gian tên STD;

int main () {
// Mở tệp ở chế độ ghi.
tệp fstream ("file.txt", iOS :: out);

// Viết chuỗi vào tệp.
fprintf (tập tin, "Xin chào thế giới! \ n");

// Đóng tệp.
file.close ();

trả lại 0;
}
`` `

### Phần kết luận

Trong hướng dẫn này, chúng tôi đã chỉ cho bạn cách đọc và ghi các tệp TXT trong C ++.Chúng tôi hy vọng bạn tìm thấy hướng dẫn này hữu ích.Để biết thêm thông tin về việc đọc và ghi các tệp trong C ++, vui lòng tham khảo [tài liệu C ++] (https://en.cppreference.com/w/cpp/io/basic_file_io).

## hashtags

* #C ++
* #TXT
* #tài liệu
* #Io
* #Programming
=======================================
#C++, #TXT, #file, #IO, #Programming ## Read and write TXT files in C++

TXT files are a simple and common way to store text data. They are often used for storing configuration data, log files, and other text-based data. In this tutorial, we will show you how to read and write TXT files in C++.

### Reading a TXT file

To read a TXT file in C++, you can use the `fopen()` function to open the file in read mode. Once the file is open, you can use the `fscanf()` function to read the data from the file. The `fscanf()` function takes three arguments: the file pointer, the format string, and the variable to store the data.

For example, the following code reads the first line of a TXT file and stores it in a string variable:

```c++
#include <iostream>
#include <fstream>

using namespace std;

int main() {
// Open the file in read mode.
fstream file("file.txt", ios::in);

// Read the first line of the file.
string line;
fscanf(file, "%s\n", line);

// Print the line to the console.
cout << line << endl;

return 0;
}
```

### Writing a TXT file

To write a TXT file in C++, you can use the `fopen()` function to open the file in write mode. Once the file is open, you can use the `fprintf()` function to write data to the file. The `fprintf()` function takes three arguments: the file pointer, the format string, and the data to write.

For example, the following code writes the string "Hello world!" to a TXT file:

```c++
#include <iostream>
#include <fstream>

using namespace std;

int main() {
// Open the file in write mode.
fstream file("file.txt", ios::eek:ut);

// Write the string to the file.
fprintf(file, "Hello world!\n");

// Close the file.
file.close();

return 0;
}
```

### Conclusion

In this tutorial, we showed you how to read and write TXT files in C++. We hope you found this tutorial helpful. For more information on reading and writing files in C++, please refer to the [C++ documentation](https://en.cppreference.com/w/cpp/io/basic_file_io).

## Hashtags

* #C++
* #TXT
* #file
* #IO
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top