如何截取文件名,而去掉后缀??

kurt_hu 2000-08-17 12:15:00
请问:如何截取文件名,而去掉后缀?如:file1.txt,我只要file1即可。
...全文
3424 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
rusan 2001-09-12
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <dir.h>
#include <stdlib.h>

int fnsplit(const char* path,const char* drive,const char* directory,const char* filename,const char* ext)

上面是一函数, 其中filename就是你想要的没有扩展名的文件名
try it?
fhaibo 2001-09-12
  • 打赏
  • 举报
回复
我在项目中作过的
将一个全路径名分为:路径,文件名,后缀名
输入:istRoute是全路径名
输出:ostPath路径名
ostFile文件名
ostExtend后缀名
#include <strstrea.h>
void RouteTab(istrstream istRoute,ostrstream &ostPath,ostrstream &ostFile,ostrstream &ostExtend)
{
int tab=0;
istRoute.seekg(0,ios::end);
int end=istRoute.tellg()+1;
istRoute.seekg(0,ios::beg);
int a;
for(int i=1;i<end;i++)
{
istRoute.seekg(-i,ios::end);
a=istRoute.get();
if(a=='\\')
{
tab=istRoute.tellg();
break;
}
}
if(!tab)
istRoute.seekg(-1,ios::cur);
ostrstream ostname;
ostname<<istRoute.rdbuf();
istRoute.seekg(0,ios::beg);
while(istRoute.tellg()<tab)
ostPath<<(char)istRoute.get();
istrstream istname(ostname.str());
tab=ostname.pcount();
while(istname.tellg()<ostname.pcount())
{
if(istname.get()=='.')
{

istname.seekg(-1,ios::cur);
tab=istname.tellg();
break;
}
}
if(tab!=ostname.pcount())
ostExtend<<istname.rdbuf();
istname.seekg(0,ios::beg);
while(istname.tellg()<tab)
ostFile<<(char)istname.get();
}
phyllis 2001-09-12
  • 打赏
  • 举报
回复
CString FileTittle;
CFile Data_openDialog;

FileTittle=Data_openDialog.GetTittleName();

sztanj 2000-09-08
  • 打赏
  • 举报
回复
vc真麻烦,c++builder对这个功能有专门的函数。
claider 2000-08-20
  • 打赏
  • 举报
回复
只要用splitpath()
就行了
元明 2000-08-19
  • 打赏
  • 举报
回复
不错
borz 2000-08-18
  • 打赏
  • 举报
回复
#include <string.h>
#include <stdio.h>
char* getFile(char *fullname)
{
int from,to,i;
char *newstr,*temp;
if(fullname!=NULL)
{
if((temp=strchr(fullname,'.'))==NULL)//if not find dot
newstr = fullname;
else
{
from = strlen(fullname) - 1;
to = (temp-fullname); //the first dot's index;
for(i=from;i--;i<=to)
if(fullname[i]=='.')break;//find the last dot
newstr = (char*)malloc(i+1);
strncpy(newstr,fullname,i);
*(newstr+i)=0;
}
}
return newstr;
}

int main(int argc,char *argv[])
{
char *file1 = getFile(argv[1]);
printf("%s",file1);
exit(0);
}
Kevin_qing 2000-08-17
  • 打赏
  • 举报
回复
char * fn="file.txt";
int i=0;
i=strlen(fn);
char* fn1=fn+i;
while(fn1>fn){
if(*fn1=='.')
{
*fn1=0;
break;}
fn1--;
}
这样fn就变为"file "了
qiangsheng 2000-08-17
  • 打赏
  • 举报
回复
前面两位给了程序,俺就说说思路吧。
在MS DOS系列里(包括16位WIN,WIN3.0~PWIN3.2),是把文件名中“.”后面的部分作为扩展名的,一个文件名不允许出现两个“.”;到了WIN95以后,开始使用长文件名,并允许文件名中出现“.”,那样,WINDOWS就把最后一个“.”以后的部分作为扩展名。上两个算法都是查找最后一个“.”并截取它前面的部分作为文件名。
看在俺也写了这么多的份上,给点分吧。 ^_*
U皮特U 2000-08-17
  • 打赏
  • 举报
回复
CString strFile = "file1.txt";
int pos = strFile.ReverseFind('.'));
if ( pos > 0 )
CString strRet = strFile.Left(pos);

// Now, strRet == "file1"

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧