Linux系统 C/C++获取当前文件夹路径和文件名

man getcwd

一、文件夹路径获取

1、学习参考网址:https://blog.csdn.net/Kevin_Xie86/article/details/100015485
2、Linux参考手册

1.1 头文件

#include <unistd.h> // Linux系统中
#include <direct.h> // windows系统中

1.2 函数原型

1.2.1 getcwd()

char* getcwd(char* buf, size_t size);
描述:该函数的功能是返回当前 工作目录的绝对路径到一个字符数组中(即参数buf)。该路径中不能包含符号链接组件。

返回值是char *的路径
参数

char* buf: 返回的目录地址的字符串数组
size_t size: buf数组的大小

注意:如果buf是一个空指针,那么该函数将会出不知名错误;如果给定的size超过了buf实际大小那么,getcwd()函数将会返回一个空指针。若成功获得路径getcwd()返回buf指针;
特别注意的是:当buff是空指针时,getcwd()函数将会使用malloc函数动态的为其分配size(size不为0)大小的内存。若size为0,则getcwd()函数将会为buf分配可能会用到的最大(注意:Linux中文件名字的长度限制是255个英文字符)的内存。使用完返回的路径字符串时,应当记得进行free(buf)操作

1.2.2 get_current_dir_name()

getcwd() 函数使用起来不怎么方便,因为他需要提前设定返回的数组大小,有时候路径长度不变,容易出错。而get_current_dir_name()函数就不需要提前考虑数组的大小。
函数原型:
char* get_current_dir_name(void)

参数说明:该函数直接返回当前工作目录的绝对路径

描述:get_current_dir_name()函数返回的路径也是通过malloc函数动态的分配的内存,因此使用完其返回值后也要记得对返回值进行free()操作。

1.2.3 getwd()

函数原型:
char* getwd(char* buf);
描述:该函数不会使用malloc函数动态的分配内存,但是返回值参数buf的大小必须是一个最少能容纳当前路径的最大可能的长度的字符串指针

1.3 函数使用实例参考

这里只介绍getcwd()和get_current_dir_name()的使用参考代码

1.3.1 getcwd()

#include<stdio.h>
#include<stdlib.h>
#include<unisted> // 函数所在头文件
#include<iostream>
using namespace std;
int main(){
	const int size = 255;
	char buf[size];
	getcwd(buf,size);
	cout<<"Working path is: "<<buf<<endl;
	
	// buf为空指针时
	char* buf1 = NULL;
	buf1 = getcwd(NULL, 0);
	puts(buf1); // 在屏幕输出该路径
	free(buf1); // 使用完路径释放buf1,因为是由getcwd()函数动态分配的内存(malloc)
	// 此时任然可以查看路径占用了多少字节
	cout << "size is: " << sizeof(buf1) << endl;
	return 0;
}

1.3.2 get_current_dir_name()

#include<iostream>
#include<unistd.h> // 函数所在头文件
using namespace std;
int main()	{
	char* buf;
	buf = get_current_dir_name();
	cout << "Current dir name is: " << buf;
	cout << "size is: " << sizeof(buf) <<endl;
	free(buf); // 一定记得free,因为是由get_current_dir_name()函数动态分配的内存
	
	return 0;
}

二、获取某一存在目录中所有文件名字(包括扩展名)

1、学习参考网站:https://blog.csdn.net/lsq2902101015/article/details/51373911
2、 Linux的man帮助命令

man opendir

2.1 实现步骤

在Linux系统中读取指定目录下所有文件名的实现步骤主要分三步:

  • 打开文件目录opendir()
  • 读取文件信息readdir()
  • 关闭文件目录closedir()

2.2 包含头文件

#include <sys/types.h>
#include <dirent.h>

2.3 打开文件目录opendir

相关函数原型:

DIR *opendir(const char *name);
DIR *fdopendir(int fd);

描述:

  • opendir() 函数根据目录路径打开一个目录流,并返回一个目录流(DIR)指针。这个指针指向目录的首地址。
  • fdopendir()函数和opendir()函数类似,只是它是根据文件操作流fd(即常用的文件读取),打开的目录,然后同样是返回目录流指针。
#include <iostream>
#include <sys/types.h>
#include <dirent.h>

using namespace std;
int main(){
	// 1、打开文件目录
	DIR* dirStream;
	const char* path = "/home/XX/code/";
	dirStream = opendir(path);
	// 2、接下来是读取文件信息,看2.4节
	return 0;
}

2.4 读取文件信息

相关函数原型:

#include <dirent.h> // 所在头文件
struct dirent *readdir(DIR *dirp);

函数描述:该函数根据打开的目录流指针dirp,返回一个dirent结构体指针。如果在目录流的结尾,那么该指针为空。否则就是表示由dirp指针指向的目录流的下一个目录条目

struct dirent
  {
    __ino_t d_ino;   /*inode number */ // 节点号索引
    __off_t d_off;   /*offset to this dirent */ // 在目录文件中的偏移
    unsigned short int d_reclen;   /*length of this d_name */ // 这个文件的名字长度
    unsigned char d_type;   /*the type of d_name */ // 文件类型
    char d_name[256];    /*file name(null-terminated)  */ // 这个是文件名
  };
————————————————

#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <string>
#include <vector>

using namespace std;
int main(){
	// 1、打开文件目录
	DIR* dirStream;
	const char* path = "/home/XX/code/";
	dirStream = opendir(path);
	// 2、接下来是读取文件信息
	struct dirent* dirInfo;
	vector<string> name;
	while((dirInfo = readdir(dirStream))!=0);
	{
		cout << "Value test: "<<dirInfo -> d_name << endl;
		name.push_back(dirInfo -> d_name);
	} // 注意此时dirStream 已经指向文件尾了
	// 3、最后关闭文件目录
	closedir(dirStream);
	return 0;
}

2.4 关闭文件目录

和opendir()成对出现:int closedir(DIR *)。略过

三、其他关于目录操作的函数

函数原型如下

#include <sys/types.h>
#include <dirent.h>

long telldir(DIR *dirp); //返回与目录流dirp相关联的当前位置(地址)

void seekdir(DIR *dirp, long loc);// 设置目录流dirp指向loc,其中参数loc必须是telldir()函数返回的地址。设置之后下次调用readdir函数将从该位置开始读

void rewinddir(DIR *dirp); // 该函数是将目录流指针dirp重置到开始的位置(地址)

  • 6
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于在Linux上配置C/C++环境并使用VS Code进行开发,你可以按照以下步骤进行操作: 步骤 1:安装GCC和G++编译器 首先,确保你的系统中已经安装了GCC和G++编译器。你可以通过在终端中运行以下命令来检查它们是否已安装: ``` gcc --version g++ --version ``` 如果命令返回了版本信息,则说明已经安装好了。如果没有安装,你可以通过运行以下命令来安装它们: ``` sudo apt-get update sudo apt-get install build-essential ``` 步骤 2:安装VS Code 访问VS Code的官方网站(https://code.visualstudio.com/)并下载适用于Linux的版本。选择合适的包(.deb或.rpm),然后按照安装向导进行安装。 步骤 3:安装C/C++扩展 在VS Code中,点击左侧的扩展图标(四个方块),搜索并选择 "C/C++" 扩展进行安装。 步骤 4:创建C/C++项目 在VS Code中,打开一个文件夹,然后点击菜单栏的 "文件" -> "新建文件夹",为项目创建一个新的文件夹。 步骤 5:配置编译器路径 打开VS Code的设置(快捷键:Ctrl + ,),在搜索框中输入 "C/C++",找到 "C/C++: Edit Configurations (UI)" 选项并点击。在弹出的窗口中,点击 "C++",然后再点击 "g++"。在 "compilerPath" 字段中,填入你的GCC编译器的路径。通常情况下,它应该是 "/usr/bin/g++"。 步骤 6:创建C/C++文件 在项目文件夹中,右键点击空白处,选择 "新建文件" -> "新建文件"。在文件名后面加上 ".cpp" 后缀,比如 "main.cpp"。然后,在文件中编写你的C/C++代码。 步骤 7:编译和运行 在VS Code中,按下快捷键 "Ctrl + ` " 打开集成终端。在终端中输入以下命令来编译你的代码: ``` g++ -o output_filename source_filename.cpp ``` 将 "output_filename" 替换为你想要的输出文件的名字,将 "source_filename.cpp" 替换为你的源代码文件名。 然后,在终端中运行以下命令来执行你的程序: ``` ./output_filename ``` 这样就可以在终端中看到你的程序的输出了。 希望这些步骤能够帮助你在Linux上配置C/C++环境并使用VS Code进行开发!如果有任何问题,请随时向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值