Ⅰ c語言文件名提取
可以參考 DIR 命令選項 (/os /oe /od /on 等),知道其它排列方法。
例如:
system("dir *.* /os > m01.txt"); // m01.txt 存放內:按文件大小排列
system("dir *.* /oe > m02.txt"); //m02.txt 存放:按文件擴展名次序容排列
system("dir *.* /od > m03.txt"); //m03.txt 存放:按文件日期排列
Ⅱ 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/C++怎樣將獲取文件的擴展名和文件名
#include <string.h>
#include <stdio.h>
int main()
{
char filename[0]="text.txt" ;
char *ext=strrchr(filename,'.');
if (ext)
{
*ext='\0';
ext++;
}
printf("name=%s\n", filename);
printf("ext-name=%s\n", ext );
return 0;
}
Ⅳ C++中如何從路徑字元串中獲取文件名!
C風格:
char*p=strrchr(path.c_str(),'/')
p是path里最後一個'/'的地址。然後
strings(p+1);
,內s就是"world.shp"了。
C++風格:
intpos=path.find_last_of('/');
pos就是最後一個'/'的下標容。
然後
strings(path.substr(pos+1));
s就是"world.shp"了。
Ⅳ C# 遍歷文件夾下所有子文件夾中的文件,得到文件名
假設a文件夾在F盤下,代碼如下。將文件名輸出到一個ListBox中
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
DirectoryInfo theFolder = new DirectoryInfo(@"F:\a\");
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
//遍歷文件夾
foreach (DirectoryInfo NextFolder in dirInfo)
{
// this.listBox1.Items.Add(NextFolder.Name);
FileInfo[] fileInfo = NextFolder.GetFiles();
foreach (FileInfo NextFile in fileInfo) //遍歷文件
this.listBox2.Items.Add(NextFile.Name);
}
}
}
}