C++打印当前工作目录
时间: 2023-08-24 20:08:03 浏览: 56
你可以使用C语言中的getcwd函数来打印当前工作目录。以下是一个示例代码:
```
#include <stdio.h>
#include <unistd.h>
int main() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("当前工作目录为:%s\n", cwd);
} else {
printf("获取当前工作目录失败!\n");
}
return 0;
}
```
运行该程序将输出当前工作目录。
相关问题
c++代码打印当前目录
你可以使用以下 C 代码来打印当前目录:
```c
#include <stdio.h>
#include <unistd.h>
int main() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working directory: %s\n", cwd);
} else {
perror("getcwd() error");
}
return 0;
}
```
这个程序使用了 `getcwd()` 函数来获取当前工作目录,并使用 `printf()` 函数来打印出来。
c++ 打印当前时间
你可以使用 ctime 库中的函数来打印当前时间。下面是一个简单的示例代码:
```c++
#include <iostream>
#include <ctime>
int main() {
// 获取当前系统时间
time_t now = time(0);
// 转换为字符串形式
char* dt = ctime(&now);
// 输出当前时间
std::cout << "当前时间为: " << dt << std::endl;
return 0;
}
```
当你运行该程序时,输出将类似于以下内容:
```
当前时间为: Tue Aug 24 15:08:54 2021
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)