Share c++ get source file name

purplemouse372

New member
## C ++ Nhận tên tệp nguồn

### 1. Giới thiệu

Trong C ++, tên tệp nguồn có thể được lấy bằng macro `__file__`.Macro này mở rộng sang đường dẫn đầy đủ và tên tệp của tệp nguồn hiện đang được biên dịch.Ví dụ: nếu tệp nguồn được gọi là `foo.cpp`, macro` __file__` sẽ mở rộng thành `" /path/to/foo.cpp "`.

### 2. Nhận tên tệp nguồn

Để lấy tên tệp nguồn, chỉ cần sử dụng macro `__file__` trong mã của bạn.Ví dụ:

`` `C ++
#include <Istream>

int main () {
std :: cout << __file__ << std :: endl;
}
`` `

Mã này sẽ in toàn bộ đường dẫn và tên tệp của tệp nguồn hiện đang được biên dịch.

### 3. Ví dụ

Dưới đây là một số ví dụ về cách sử dụng macro `__file__`:

* Để nhận thư mục của tệp nguồn, bạn có thể sử dụng mã sau:

`` `C ++
std :: chuỗi get_source_file_directory () {
std :: chuỗi file_name = __file__;
size_t last_slash_index = file_name.find_last_of ("/\\\");
if (last_slash_index! = std :: string :: npos) {
return file_name.substr (0, last_slash_index);
} khác {
trở lại "";
}
}
`` `

* Để lấy tên cơ sở của tệp nguồn, bạn có thể sử dụng mã sau:

`` `C ++
std :: chuỗi get_source_file_base_name () {
std :: chuỗi file_name = __file__;
size_t last_dot_index = file_name.find_last_of (".");
if (last_dot_index! = std :: string :: npos) {
return file_name.substr (0, last_dot_index);
} khác {
trả về file_name;
}
}
`` `

### 4. Kết luận

Macro `__file__` là một công cụ hữu ích để lấy tên tệp nguồn trong C ++.Nó có thể được sử dụng để có được đường dẫn và tên tệp đầy đủ, thư mục hoặc tên cơ sở của tệp nguồn.

### 5. Hashtags

* C ++
* Tệp nguồn
* Macro
* Tên tệp
* Danh mục
=======================================
## C++ get source file name

### 1. Introduction

In C++, the source file name can be obtained using the `__FILE__` macro. This macro expands to the full path and filename of the source file that is currently being compiled. For example, if the source file is called `foo.cpp`, the `__FILE__` macro would expand to `"/path/to/foo.cpp"`.

### 2. Getting the source file name

To get the source file name, simply use the `__FILE__` macro in your code. For example:

```c++
#include <iostream>

int main() {
std::cout << __FILE__ << std::endl;
}
```

This code will print the full path and filename of the source file that is currently being compiled.

### 3. Examples

Here are some examples of how to use the `__FILE__` macro:

* To get the directory of the source file, you can use the following code:

```c++
std::string get_source_file_directory() {
std::string file_name = __FILE__;
size_t last_slash_index = file_name.find_last_of("/\\");
if (last_slash_index != std::string::npos) {
return file_name.substr(0, last_slash_index);
} else {
return "";
}
}
```

* To get the base name of the source file, you can use the following code:

```c++
std::string get_source_file_base_name() {
std::string file_name = __FILE__;
size_t last_dot_index = file_name.find_last_of(".");
if (last_dot_index != std::string::npos) {
return file_name.substr(0, last_dot_index);
} else {
return file_name;
}
}
```

### 4. Conclusion

The `__FILE__` macro is a useful tool for getting the source file name in C++. It can be used to get the full path and filename, the directory, or the base name of the source file.

### 5. Hashtags

* C++
* Source file
* Macro
* Filename
* Directory
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top