从文件名中删除扩展名的简便方法?

时间:2022-11-04 14:55:02

I am trying to grab the raw filename without the extension from the filename passed in arguments:

我试图从参数传递的文件名中获取没有扩展名的原始文件名:

int main ( int argc, char *argv[] )
{
    // Check to make sure there is a single argument
    if ( argc != 2 )
    {
        cout<<"usage: "<< argv[0] <<" <filename>\n";
        return 1;
    }

    // Remove the extension if it was supplied from argv[1] -- pseudocode
    char* filename = removeExtension(argv[1]);

    cout << filename;

}

The filename should for example be "test" when I passed in "test.dat".

例如,当我传入“test.dat”时,文件名应为“test”。

9 个解决方案

#1


68  

size_t lastindex = fullname.find_last_of("."); 
string rawname = fullname.substr(0, lastindex); 

Beware of the case when there is no "." and it returns npos

当没有“。”时要小心。它返回npos

#2


29  

This works:

这有效:

std::string remove_extension(const std::string& filename) {
    size_t lastdot = filename.find_last_of(".");
    if (lastdot == std::string::npos) return filename;
    return filename.substr(0, lastdot); 
}

#3


13  

In my opinion it is easiest, and the most readable solution:

在我看来,它是最简单的,也是最易读的解决方案:

#include <boost/filesystem/convenience.hpp>

std::string removeFileExtension(const std::string& fileName)
{
    return boost::filesystem::change_extension(fileName, "").string();
}

#4


10  

The following works for a std::string:

以下适用于std :: string:

string s = filename;
s.erase(s.find_last_of("."), string::npos);

#5


7  

For those who like boost:

对于那些喜欢激励的人:

Use boost::filesystem::path::stem. It returns the filename without the last extension. So ./myFiles/foo.bar.foobar becomes foo.bar. So when you know you are dealing with only one extension you could do the follwing:

使用boost :: filesystem :: path :: stem。它返回没有最后一个扩展名的文件名。所以./myFiles/foo.bar.foobar变成了foo.bar。因此,当您知道您只处理一个扩展时,您可以执行以下操作:

boost::filesystem::path path("./myFiles/fileWithOneExt.myExt");
std::string fileNameWithoutExtension = path.stem().string();

When you have to deal with multiple extensions you might do the following:

当您必须处理多个扩展时,您可能会执行以下操作:

boost::filesystem::path path("./myFiles/fileWithMultiExt.myExt.my2ndExt.my3rdExt");
while(!path.extension().empty())
{
    path = path.stem();
}

std::string fileNameWithoutExtensions = path.stem().string();

(taken from here: http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/reference.html#path-decomposition found in the stem section)

(取自这里:http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/reference.html#path-decomposition in stem section)

BTW works with rooted paths, too.

BTW也适用于根路径。

#6


2  

You can do this easily :

您可以轻松完成此操作:

string fileName = argv[1];
string fileNameWithoutExtension = fileName.substr(0, fileName.rfind("."));

Note that this only work if there is a dot. You should test before if there is a dot, but you get the idea.

请注意,这仅在有点时才有效。你应该先测试一下是否有点,但你明白了。

#7


1  

In case someone just wants a simple solution for windows:

万一有人只想要一个简单的Windows解决方案:

Use PathCchRemoveExtension ->MSDN

使用PathCchRemoveExtension - > MSDN

... or PathRemoveExtension (deprecated!) ->MSDN

...或PathRemoveExtension(不建议使用!) - > MSDN

#8


0  

More complex, but with respect to special cases (for example: "foo.bar/baz", "c:foo.bar", works for Windows too)

更复杂,但就特殊情况而言(例如:“foo.bar/baz”,“c:foo.bar”,也适用于Windows)

std::string remove_extension(const std::string& path) {
    if (path == "." || path == "..")
        return path;

    size_t pos = path.find_last_of("\\/.");
    if (pos != std::string::npos && path[pos] == '.')
        return path.substr(0, pos);

    return path;
}

#9


-2  

Just loop through the list and replace the first (or last) occurrence of a '.' with a NULL terminator. That will end the string at that point.

只需遍历列表并替换'。'的第一个(或最后一个)事件。使用NULL终止符。那将在那时结束字符串。

Or make a copy of the string up until the '.', but only if you want to return a new copy. Which could get messy since a dynamically allocated string could be a source of memory leak.

或者将字符串的副本复制到'。',但仅当您要返回新副本时。由于动态分配的字符串可能是内存泄漏的来源,因此可能会变得混乱。

for(len=strlen(extension);len>= 0 && extension[len] != '.';len--)
     ;

char * str = malloc(len+1);

for(i=0;i<len;i++)
  str[i] = extension[i];

 str[i] = '\0'l

#1


68  

size_t lastindex = fullname.find_last_of("."); 
string rawname = fullname.substr(0, lastindex); 

Beware of the case when there is no "." and it returns npos

当没有“。”时要小心。它返回npos

#2


29  

This works:

这有效:

std::string remove_extension(const std::string& filename) {
    size_t lastdot = filename.find_last_of(".");
    if (lastdot == std::string::npos) return filename;
    return filename.substr(0, lastdot); 
}

#3


13  

In my opinion it is easiest, and the most readable solution:

在我看来,它是最简单的,也是最易读的解决方案:

#include <boost/filesystem/convenience.hpp>

std::string removeFileExtension(const std::string& fileName)
{
    return boost::filesystem::change_extension(fileName, "").string();
}

#4


10  

The following works for a std::string:

以下适用于std :: string:

string s = filename;
s.erase(s.find_last_of("."), string::npos);

#5


7  

For those who like boost:

对于那些喜欢激励的人:

Use boost::filesystem::path::stem. It returns the filename without the last extension. So ./myFiles/foo.bar.foobar becomes foo.bar. So when you know you are dealing with only one extension you could do the follwing:

使用boost :: filesystem :: path :: stem。它返回没有最后一个扩展名的文件名。所以./myFiles/foo.bar.foobar变成了foo.bar。因此,当您知道您只处理一个扩展时,您可以执行以下操作:

boost::filesystem::path path("./myFiles/fileWithOneExt.myExt");
std::string fileNameWithoutExtension = path.stem().string();

When you have to deal with multiple extensions you might do the following:

当您必须处理多个扩展时,您可能会执行以下操作:

boost::filesystem::path path("./myFiles/fileWithMultiExt.myExt.my2ndExt.my3rdExt");
while(!path.extension().empty())
{
    path = path.stem();
}

std::string fileNameWithoutExtensions = path.stem().string();

(taken from here: http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/reference.html#path-decomposition found in the stem section)

(取自这里:http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/reference.html#path-decomposition in stem section)

BTW works with rooted paths, too.

BTW也适用于根路径。

#6


2  

You can do this easily :

您可以轻松完成此操作:

string fileName = argv[1];
string fileNameWithoutExtension = fileName.substr(0, fileName.rfind("."));

Note that this only work if there is a dot. You should test before if there is a dot, but you get the idea.

请注意,这仅在有点时才有效。你应该先测试一下是否有点,但你明白了。

#7


1  

In case someone just wants a simple solution for windows:

万一有人只想要一个简单的Windows解决方案:

Use PathCchRemoveExtension ->MSDN

使用PathCchRemoveExtension - > MSDN

... or PathRemoveExtension (deprecated!) ->MSDN

...或PathRemoveExtension(不建议使用!) - > MSDN

#8


0  

More complex, but with respect to special cases (for example: "foo.bar/baz", "c:foo.bar", works for Windows too)

更复杂,但就特殊情况而言(例如:“foo.bar/baz”,“c:foo.bar”,也适用于Windows)

std::string remove_extension(const std::string& path) {
    if (path == "." || path == "..")
        return path;

    size_t pos = path.find_last_of("\\/.");
    if (pos != std::string::npos && path[pos] == '.')
        return path.substr(0, pos);

    return path;
}

#9


-2  

Just loop through the list and replace the first (or last) occurrence of a '.' with a NULL terminator. That will end the string at that point.

只需遍历列表并替换'。'的第一个(或最后一个)事件。使用NULL终止符。那将在那时结束字符串。

Or make a copy of the string up until the '.', but only if you want to return a new copy. Which could get messy since a dynamically allocated string could be a source of memory leak.

或者将字符串的副本复制到'。',但仅当您要返回新副本时。由于动态分配的字符串可能是内存泄漏的来源,因此可能会变得混乱。

for(len=strlen(extension);len>= 0 && extension[len] != '.';len--)
     ;

char * str = malloc(len+1);

for(i=0;i<len;i++)
  str[i] = extension[i];

 str[i] = '\0'l