获取文件路径c|C语言获取相对路径的文件名(不带路径) 我获取的是带相对路径的文件名这里只需要文件夹中的文件名称

获取文件路径c|C语言获取相对路径的文件名(不带路径) 我获取的是带相对路径的文件名这里只需要文件夹中的文件名称的第1张示图

❶ c语言如何获得文件当前路径

C语言里在main函数来的第二个参数里面,自保存着当前程序运行的目录也就是argv[0]main( int argc, char *argv[]){ printf("%s ", argv[0] );}就是文件当前所在位置不过需要注意的一点是这个路径里面保存了当前文件的文件名如果你只是需要路径的话还需要自己操作一下main(int a,char *c[]){ char s[100]; int i; //把路径保存到字符串s里 strcpy(s,c[0]); for(i=strlen(s); i>0 ; i–) if( s[i] == '\\') { s[i]='\0'; break; } //找到最后一个 \ 并删除之后的内容 //最后输出的s,就是当前文件的路径了 puts(s);}

❷ 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语言…如何获取进程的可执行文件路径…

hMole=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,pinfo->th32ProcessID); Mole32First(hMole, minfo); GetShortPathName(minfo->szExePath,shortpath,256);如果还需要详细的代码,请Q我吧。。我最近刚在做这块。。。新手,写的东西比较简单- –

❹ c如何获得FILE*的路径

关于通过 C 语言编程获取到某个文件的完整路径、以及文件名称的问题,那是毫无疑问可内以做容到的。但是具体的编程我由于已经好多年没有编写 C 语言程序了。故具体的程序调试过程需要你自己进行完成了。关于这部分的问题,我记得 C 语言库函数大全上面肯定是有的,你可以参考有关 C 语言库函数的教材。但是注意一点就是:如果是在 WINDOWS 系统下面的编程,那么路径名必须使用双斜线,即:\\ 才行。例如:若想表示打开驱动器 D 上的 MY_SUBDIR 子目录下面的 MYFILE.TXT 文件,则在 C 语言源程序中要写为如下代码:#include <stdio.h>void main( ){FILE * fpr ;fpr = fopen("D:\\MY_SUBDIR\\MYFILE.TXT", "r") ; /* 这样写才是正确的语句。*/……fclose(fpr) ;}

❺ 怎么用标准C/C++打开windows explorer,选择文件,获取文件或文件夹路径

char szDir[100] ={0};char szPath[255] = {0};BROWSEINFO bi ;bi.hwndOwner = m_hWnd ;bi.pidlRoot = NULL ;bi.lParam = NULL ;bi.lpfn = NULL;bi.lpszTitle = "选择";bi.iImage = NULL ;bi.ulFlags = BIF_BROWSEINCLUDEFILES ;bi.pszDisplayName = szDir ; //存放的文件名字专ITEMIDLIST *pidl = ::SHBrowseForFolder(&bi);SHGetPathFromIDList(pidl,szPath); // szPath返回的是完全属路径

❻ C语言获取相对路径的文件名(不带路径) 我获取的是带相对路径的文件名,这里只需要文件夹中的文件名称

ExtractFileName(文件抄完整路径 含文件名)例:procere TForm1.Button1Click(Sender: TObject);begin if OpenDialog1.Execute then begin showmessage(ExtractFileName(OpenDialog1.FileName)); end;end;以上例子为:当点击Button1时,弹出选择文件后,显示所选的文件名称(含扩展名)。

❼ html怎么通过file获取文件路径

html通过file获取文件路径方法:第一种: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); 结果: C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin 获取当前类的所在工程路径; 如果不加“/” File f = new File(this.getClass().getResource("").getPath()); System.out.println(f); 结果: C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test 获取当前类的绝对路径; 第二种: File directory = new File("");//参数为空 String courseFile = directory.getCanonicalPath() ; System.out.println(courseFile); 结果: C:\Documents and Settings\Administrator\workspace\projectName 获取当前类的所在工程路径; 第三种: URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt"); System.out.println(xmlpath); 结果: file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt 获取当前工程src目录下selected.txt文件的路径

❽ C语言试题 编写一个获取文件路径的函数

#include <stdio.h> main(){char str[80]="d:\\files\\dataFiles\\data\\avatar.bmp"; // 单斜杠处填双斜杠int L,i;L = strlen(str);for (i=L-1;i>=0;i–) if (str[i] == '\\') { str[i] ='\0'; break;}; // 去掉文件名就是路径printf("Path= %s",str);return 0;}———–写成函数和调用:#include <stdio.h>void fun(char *str){int L,i;L = strlen(str);for (i=L-1;i>=0;i–) if (str[i] == '\\') { str[i] ='\0'; break;};}main(){char str[80]="d:\\files\\dataFiles\\data\\avatar.bmp";fun(str);printf("Path= %s",str);return 0;}

❾ c语言读取txt文件时,文件路径怎么写,调试的时候都是找不到该文件

带空格的文件名,可以先放入char 数组,再使用。例如: FILE *fin;char namein[80]="D:\\kkk\\tmp\\test sp\\a.txt"; // 带空格int x;printf("%s\n",namein);fin=fopen(namein,"r"); // 这里用变量名if (!fin) printf("open err");fscanf(fin,"%d",&x);printf("%d",x);fclose(fin);}c++ 也一样。 还有一种老方法,带空格的路径 用 8字符 省略法(写6个字符加1个波浪号加1):namein[80]="C:\DOCUME~1\Administrator\abc.txt";

未经允许不得转载:山九号 » 获取文件路径c|C语言获取相对路径的文件名(不带路径) 我获取的是带相对路径的文件名这里只需要文件夹中的文件名称

赞 (0)