今天给各位分享c语言输入从文件路径的知识,其中也会对c++输入路径进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、c语言如何获取用户通过键盘输入的文件目录中的文件名和文件路径,ballball大佬帮帮我🙏求代码?2、c语言读取文件的路径怎么设定3、C语言文件路径设置
c语言如何获取用户通过键盘输入的文件目录中的文件名和文件路径,ballball大佬帮帮我🙏求代码?
int main()
{
string s = “c:\\abc\\def\\text.txt”;
int xie_index = s.find_last_of(‘\\’);// 路径中最后一个\的位置
string file_dirname = s.substr(0, xie_index + 1);
string file_basename = s.substr(xie_index + 1, s.size());
cout file_dirname endl file_basename endl;
}
c语言读取文件的路径怎么设定
//获取指定目录下的所有文件列表 author:wangchangshaui jlu
char** getFileNameArray(const char *path, int* fileCount)
{
int count = 0;
char **fileNameList = NULL;
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
struct stat statbuf;
//打开目录
if ((pDir = opendir(path)) == NULL)
{
myLog(“Cannot open directory:%s\n”, path);
return NULL;
}
//读取目录
while ((ent = readdir(pDir)) != NULL)
{ //统计当前文件夹下有多少文件(不包括文件夹)
//得到读取文件的绝对路径名
snprintf(dir, 512, “%s/%s”, path, ent-d_name);
//得到文件信息
lstat(dir, statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
{
count++;
}
} //while
//关闭目录
closedir(pDir);
// myLog(“共%d个文件\n”, count);
//开辟字符指针数组,用于下一步的开辟容纳文件名字符串的空间
if ((fileNameList = (char**) myMalloc(sizeof(char*) * count)) == NULL)
{
myLog(“Malloc heap failed!\n”);
return NULL;
}
//打开目录
if ((pDir = opendir(path)) == NULL)
{
myLog(“Cannot open directory:%s\n”, path);
return NULL;
}
//读取目录
int i;
for (i = 0; (ent = readdir(pDir)) != NULL i count;)
{
if (strlen(ent-d_name) = 0)
{
continue;
}
//得到读取文件的绝对路径名
snprintf(dir, 512, “%s/%s”, path, ent-d_name);
//得到文件信息
lstat(dir, statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
{
if ((fileNameList[i] = (char*) myMalloc(strlen(ent-d_name) + 1))
== NULL)
{
myLog(“Malloc heap failed!\n”);
return NULL;
}
memset(fileNameList[i], 0, strlen(ent-d_name) + 1);
strcpy(fileNameList[i], ent-d_name);
myLog(“第%d个文件:%s\n”, i, ent-d_name);
i++;
}
} //for
//关闭目录
closedir(pDir);
*fileCount = count;
return fileNameList
C语言文件路径设置
C语言中指定文件路径的三种方法:
方法一:当前工程下的文件
fopen(“demo.txt”,”rt”);
方法二:当前工程下的 test文件夹 中文件
fopen(“.\\test\\demo.txt”,”rt”);
方法三:绝对路径,D盘下project文件夹中的文件
fopen(“d:\\project\\demo.txt”,”rt”);
关于c语言输入从文件路径和c++输入路径的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。