处理文件扩展名的方法? [英] Methods to handle filename extensions?

查看:70
本文介绍了处理文件扩展名的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,以便用我的应用程序处理文件名

扩展名。


虽然我可以提出几种方法所以在我自己的时候,我觉得

也许值得在这里问一下,看看有什么更有效的或者b / b
接受的做法会被提出来。


问题是这个。我的应用程序从命令行接受用于

a跨平台unix和windows环境的文件名。

因此应用程序可能会得到一个文件名''/ home / joe /myExample.ct'',

''C:\\\\ mymyExample.ct''或更通用的''myExample.ct''我的

申请表是将.ct文件中的数据转换成新的,然后将数据输入到本地目录中的文件中,应用程序

被执行名为''myExample。 out''。


他们是一个简单的方法来处理这个问题吗?


Tony

解决方案



gm ***** ***@yahoo.com 写道:

我需要帮助,以便用我的应用程序处理文件名
扩展名。

虽然我可以自己想出几种方法,但我感觉很好也许值得在这里询问看看会有什么更有效或可接受的做法。

问题是这个。我的应用程序从命令行接受用于跨平台unix和windows环境的文件名。
因此应用程序可能会得到一个文件名''/home/joe/myExample.ct'',
''C:\ Work\myExample.ct''或更通用的''myExample.ct''我的
应用程序是将.ct文件中的数据转换为新的,<然后将数据输入到本地目录中的文件中,应用程序执行名为''myExample.out''。

是他们处理这个问题的简化方法吗? / blockquote>


C语言和库没有多说文件名的

语法。它们被表示为字符串和

因此不能包含''\ 0''字符,有一些

值,如FILENAME_MAX和L_tmpnam,以及所有C都要说文件名,这就是

。特别是,C有
没有用于分开文件名的内置工具和

将它们重新组合在一起以生成新文件 -

好吧,有tmpnam(),但这几乎不能满足你的需求。


那就是说,如果你碰巧知道

上的文件名你关心的系统看起来就像上面那些,你可以使用直接的字符串抨击来完成工作。

这样的东西可能会起作用:


const char * old =" /home/joe/myExample.ct" ;;

const char * ext =" .out";

char * new;

char * p,* q;


/ *查找最右边的组件 * /

p = strrchr(old,''/'');

p =(p == NULL)? old:p + 1;

q = strrchr(p,''\\'');

p =(q == NULL)? p:q + 1;


/ *查找扩展的开头* /

q = strrchr(p,''。'');

p =(q == NULL)? p + strlen(p):q;


/ *汇总新名称* /

new = malloc(p - old + sizeof ext);

if(new == NULL)abort();

memcpy(new,old,p-old);

strcpy(new +(p - 旧),分机);


这将错误处理一些不寻常的文件名 - 例如,

它会破坏有效的POSIX文件名 ; myExample.\\",和

处理/home/joe/.cshrc"是怀疑。您可以通过一些额外的测试以及一些系统特定的#ifdef'来改善问题。
。在任何情况下,谢谢你的幸运

明星,你没有处理(开放)VMS!


DISK1: < ROOT。> [HOME.JOE] MYEXAMPLE.CT; 3

-
Er ********* @ sun.com


>他们是一个简单的方法来处理这个问题吗?


我以前使用过这些功能,没有问题。有什么建议吗?


#define MAXFNAME 2048 //预计文件名的最大长度

#define EXTLEN 10 //预计最大延伸长度


char * get_ext(char * fname)//从文件名返回扩展名

{

char * ext;

int i,j;

ext =(char *)malloc(sizeof(char)* EXTLEN);


for(i = strlen(fname) +1; i> 0; i--)

{

if(fname [i] ==''。'')

{

for(j = i + 1; j< strlen(fname)+1; j ++)

{

ext [ji -1] = fname [j];

}

i = 0;

}

}

返回分机;

}


char * get_basefilename(char * fname)//返回文件名减去

扩展名

{

char * ext;

int i,j;

ext =(char * )malloc(sizeof(char)* MAXFNAME);


for(i = strlen(fname)+1; i> 0; i--)

{

if(fname [ i] ==''。'')

{

for(j = 0; j<一世; j ++)

{

分机[j] = fname [j];

}

分机[i] =''\''';

i = 0;

}

}

返回分机;

}


I need assistance coming up with a clean way to handle filename
extensions with my application.

While I can come up with several ways of doing so on my own, I felt
perhaps it would be worth asking here to see what more effective or
accepted methods for doing would be presented.

The issue is this. My application accepts files names, intended for
a cross platform unix and windows environment, from the command line.
So the application may get a file name ''/home/joe/myExample.ct'',
''C:\Work\myExample.ct'' or more generically ''myExample.ct'' My
application is to convert the data in the .ct file, into something new,
then enter the data into a file in the local directory the application
was executed named ''myExample.out''.

Is their a simplified way to handle this problem?

Tony

解决方案



gm********@yahoo.com wrote:

I need assistance coming up with a clean way to handle filename
extensions with my application.

While I can come up with several ways of doing so on my own, I felt
perhaps it would be worth asking here to see what more effective or
accepted methods for doing would be presented.

The issue is this. My application accepts files names, intended for
a cross platform unix and windows environment, from the command line.
So the application may get a file name ''/home/joe/myExample.ct'',
''C:\Work\myExample.ct'' or more generically ''myExample.ct'' My
application is to convert the data in the .ct file, into something new,
then enter the data into a file in the local directory the application
was executed named ''myExample.out''.

Is their a simplified way to handle this problem?



The C language and library don''t say much about the
syntax of file names. They''re represented as strings and
therefore can''t contain ''\0'' characters, there are a few
values like FILENAME_MAX and L_tmpnam, and that''s about
all C has to say about filenames. In particular, C has
no built-in facilities for picking apart filenames and
putting them back together again to generate new ones --
well, there''s tmpnam(), but that hardly meets your need.

That said, if you happen to know that file names on
the systems you care about look like those above, you can
use straightforward string-bashing to get the job done.
Something like this might work:

const char *old = "/home/joe/myExample.ct";
const char *ext = ".out";
char *new;
char *p, *q;

/* Find rightmost "component" */
p = strrchr(old, ''/'');
p = (p == NULL) ? old : p + 1;
q = strrchr(p, ''\\'');
p = (q == NULL) ? p : q + 1;

/* Find start of "extension" */
q = strrchr(p, ''.'');
p = (q == NULL) ? p + strlen(p) : q;

/* Assemble new name */
new = malloc(p - old + sizeof ext);
if (new == NULL) abort();
memcpy (new, old, p - old);
strcpy (new + (p - old), ext);

This will mis-handle some unusual file names -- for example,
it would botch the valid POSIX file name "myExample.\\", and
its treatment of "/home/joe/.cshrc" is suspect. You might
be able to improve matters with some extra tests and a few
system-specific #ifdef''s. In any case, thank your lucky
stars that you aren''t dealing with (Open)VMS!


DISK1:<ROOT.>[HOME.JOE]MYEXAMPLE.CT;3

--
Er*********@sun.com


> Is their a simplified way to handle this problem?

I''ve used these functions before, without problems. Any suggestions?

#define MAXFNAME 2048 // max length of filenames expected
#define EXTLEN 10 // max extension length expected

char * get_ext (char * fname) // returns the extension from a filename
{
char * ext;
int i, j;
ext = (char *)malloc(sizeof(char) * EXTLEN);

for ( i = strlen(fname)+1; i > 0; i--)
{
if (fname[i] == ''.'')
{
for (j = i+1; j < strlen(fname)+1; j++)
{
ext[j-i-1] = fname[j];
}
i = 0;
}
}
return ext;
}

char * get_basefilename (char * fname) // returns the filename minus
the extension
{
char * ext;
int i, j;
ext = (char *)malloc(sizeof(char) * MAXFNAME);

for ( i = strlen(fname)+1; i > 0; i--)
{
if (fname[i] == ''.'')
{
for (j = 0; j < i; j++)
{
ext[j] = fname[j];
}
ext[i] = ''\0'';
i = 0;
}
}
return ext;
}


这篇关于处理文件扩展名的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆