c++代码打印当前目录
时间: 2023-08-28 15:08:07 浏览: 80
你可以使用以下 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++打印当前工作目录
你可以使用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++ 循环打印当前进程pid
可以使用 `getpid()` 函数获取当前进程的 PID,并且使用一个循环来反复打印这个 PID,示例代码如下:
```c++
#include <iostream>
#include <unistd.h>
int main() {
while (true) {
std::cout << "Current PID: " << getpid() << std::endl;
sleep(1); // 暂停 1 秒钟
}
return 0;
}
```
这段代码将会无限循环打印当前进程的 PID,每隔一秒钟打印一次。你可以在终端运行这个程序并观察它的输出。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![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)