注册 登录
编程论坛 C语言论坛

求助,全注释掉,留个空函数,还是有错。。。

追梦人zmrghy 发布于 2022-05-23 20:51, 3344 次点击
求助,全注释掉,留个空函数,还是有错。。。

程序代码:
#include <iostream>
#include <filesystem>
#include <algorithm>
using namespace std;

bool ReduceFileName(const std::filesystem::path& path)
{
    //auto make_name_name = [](const std::wstring& filename)
   
//{
   
//    size_t a = filename.find(L':');
   
//    if (a != std::wstring::npos)
   
//    {
   
//        a = filename.find_first_not_of(L"\t\n\v\f\r ", a + 1);
   
//        size_t b = filename.find_first_of(L"((", a);
   
//        b = filename.find_last_not_of(L"\t\n\v\f\r ", b - 1);
   
//        return filename.substr(a, b + 1 - a);
   
//    }
   
//    return filename;
   
//};

   
//using namespace std::filesystem;
   
//try
   
//{
   
//    for (auto& entry : directory_iterator(path))
   
//    {
   
//        if (entry.is_regular_file()) // 只处理常规文件
   
//        {
   
//            auto ext = entry.path().extension().generic_wstring(); // 扩展名
   
//            std::for_each(begin(ext), end(ext), [](auto& c) {if (c >= L'A'&&c <= L'Z') c += L'a' - L'A'; }); // 扩展名转化成小写
   
//            if (ext == L".mp4")
   
//            {
   
//                const auto& old_name = entry.path().stem().generic_wstring();
   
//                const auto& new_name = make_name_name(old_name);
   
//                if (old_name != new_name)
   
//                {
   
//                    wcout << old_name << L" -> " << new_name;

   
//                    auto new_path = entry.path().parent_path() / (new_name + L".mp4");
   
//                    if (exists(new_path)) // 新文件名已经存在
   
//                    {
   
//                        wcout << L" --- [failed] 文件已存在";
   
//                    }
   
//                    else
   
//                    {
   
//                        std::error_code ec;
   
//                        rename(entry, new_path, ec);
   
//                        if (ec)
   
//                            cout << " --- [failed] " << ec.message();
   
//                    }

   
//                    wcout << L'\n';
   
//                }
   
//            }
   
//        }
   
//    }
   
//}
   
//catch (const std::exception& e)
   
//{
   
//    cout << "[failed] " << e.what() << '\n';
   
//    return false;
   
//}
    return true;
}

int main(void)
{
    wcout.imbue(std::locale(std::locale(), "", std::locale::ctype));
    ReduceFileName(u8R"(C:\Users\Administrator\Desktop\[WPF]JJDown\Download\)"); // 使用 Unicode 可避免GBK认字不全
}



1>------ 已启动生成: 项目: test3, 配置: Debug Win32 ------
1>  test3.cpp
1>c:\users\administrator\desktop\test3\test3\test3.cpp(6): error C3083: “filesystem”:“::”左侧的符号必须是一种类型
1>c:\users\administrator\desktop\test3\test3\test3.cpp(6): error C2039: “path”: 不是“std”的成员
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\filesystem(26): note: 参见“std”的声明
1>c:\users\administrator\desktop\test3\test3\test3.cpp(6): error C2143: 语法错误: 缺少“,”(在“&”的前面)
1>c:\users\administrator\desktop\test3\test3\test3.cpp(68): error C2664: “bool ReduceFileName(const int)”: 无法将参数 1 从“const char [53]”转换为“const int”
1>  c:\users\administrator\desktop\test3\test3\test3.cpp(68): note: 没有使该转换得以执行的上下文
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
22 回复
#2
apull2022-05-23 21:54
加参数/std:c++17
#3
追梦人zmrghy2022-05-24 07:27
回复 2楼 apull
bool ReduceFileName(const std::C++17::filesystem::path& path)

这样吗?。。。。
VS2015也可以使用吗???
#4
rjsp2022-05-24 08:10
回复 3楼 追梦人zmrghy
我搜了一下,https://docs.
也就是起码用 VS2019,最好用当前的 VS2022
#5
apull2022-05-24 09:22
回复 3楼 追梦人zmrghy
#include <filesystem>
这个是c++17才有的,所以要加编译参数指定c++17,或者项目属性中选择c++版本为17。
#6
追梦人zmrghy2022-05-24 13:09
回复 4楼 rjsp
你教我的,这种算法用不了了!!!!
电脑用VS2015就很卡,反应好慢!!!
VS2010一点不卡。
VS2010不支持C99,不能搭建FFmpeg环境,刚安装了VS2015。。可以搭建FFmpeg环境了,但太卡了。。。
如果再换成VS2019,我怕电脑根本运行不起来VS2019。。。
#7
rjsp2022-05-24 16:51
回复 6楼 追梦人zmrghy
VS 太卡的话,可以用 MinGW64 等编译器,我试验了一下

a. 在 https:// 下载 codeblocks,它自带 gcc 11.2.0,之所以用nosetup版本,是因为我懒得安装
b. 解压,然后打开 Code::Blocks,在菜单的 Settings\Compiler\Global compiler settings\Toolchain executables 中填上解压目录里的 mingw64 目录
c. 新建一个C++控制台工程,在 main.cpp 中输入代码
#include <iostream>
#include <filesystem>
#include <algorithm>
using namespace std;

bool ReduceFileName( const std::filesystem::path& path )
{
    auto make_name_name = []( const std::wstring& filename )
    {
        size_t a = filename.find( L':' );
        if( a != std::wstring::npos )
        {
            a = filename.find_first_not_of( L"\t\n\v\f\r ", a+1 );
            size_t b = filename.find_first_of( L"((", a );
            b = filename.find_last_not_of( L"\t\n\v\f\r ", b-1 );
            return filename.substr( a, b+1-a );
        }
        return filename;
    };

    using namespace std::filesystem;
    try
    {
        for( auto& entry : directory_iterator(path) )
        {
            if( entry.is_regular_file() ) // 只处理常规文件
            {
                auto ext = entry.path().extension().generic_wstring(); // 扩展名
                std::for_each( begin(ext), end(ext), [](auto& c){if(c>=L'A'&&c<=L'Z') c+=L'a'-L'A';} ); // 扩展名转化成小写
                if( ext == L".mp4" )
                {
                    const auto& old_name = entry.path().stem().generic_wstring();
                    const auto& new_name = make_name_name( old_name );
                    if( old_name != new_name )
                    {
                        wcout << old_name << L" -> " << new_name;

                        auto new_path = entry.path().parent_path()/(new_name+L".mp4");
                        if( exists(new_path) ) // 新文件名已经存在
                        {
                            wcout << L" --- [failed] 文件已存在";
                        }
                        else
                        {
                            std::error_code ec;
                            rename( entry, new_path, ec );
                            if( ec )
                                cout << " --- [failed] " << ec.message();
                        }

                        wcout << L'\n';
                    }
                }
            }
        }
    }
    catch( const std::exception& e )
    {
        cout << "[failed] " << e.what() << '\n';
        return false;
    }
    return true;
}

int main( void )
{
    setlocale( LC_CTYPE, "" ); // 改了这一句

    ReduceFileName( u8R"(C:\Users\Administrator\Desktop\[WPF]JJDown\Download\)" ); // 使用 Unicode 可避免GBK认字不全
}


d. 我用windows自带的记事本打开 main.cpp,发现它是 ANSI 编码(其实是gbk编码),于是我在工程上点右键,选择 build options,在 Other compiler options 中输入
-finput-charset=GBK
-fexec-charset=GBK
-fwide-exec-charset=UTF-16LE
也就是告诉gcc编译器,源文件是gbk编码,可执行文件中char是gbk编码,wchar_t是utf-16 little-endian编码

e. 按F9,运行
#8
追梦人zmrghy2022-05-24 23:39
回复 7楼 rjsp
谢谢!!!
刚刚下载了,有时间了,安装学习一下。。。
#9
追梦人zmrghy2022-05-26 02:00
回复 7楼 rjsp
第一次用,这个软件。。。
好不容易,找到了汉化包。。。
编译器,是选这个吗?
这样配置对吗?
只有本站会员才能查看附件,请 登录


又出错了。。。
只有本站会员才能查看附件,请 登录


测试用的目录和文件。
只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2022-5-26 02:15编辑过]

#10
rjsp2022-05-26 10:35
你没设置编译参数吧?
这样吧,你Code::Blocks关闭,用记事本把 main.cpp 另存为 UTF-8 格式
然后打开 Code::Blocks,编译参数加
-finput-charset=UTF-8
-fexec-charset=GBK
-fwide-exec-charset=UTF-16LE

我把工程打包给你,你试试
只有本站会员才能查看附件,请 登录
#11
追梦人zmrghy2022-05-26 19:30
回复 10楼 rjsp
太笨了,不知道原理。。。
如何,模仿去做,都是错。。。
好像,电脑专和自己过 不去。。。。

只有本站会员才能查看附件,请 登录
#12
追梦人zmrghy2022-05-27 01:32
回复 10楼 rjsp
用了一天的时间,它终于可以认识 const std::filesystem::path 了。。。。。。。。。
编译,可以从第6行,直到第10行了!!!!!!


它默认不支持C++17的,还要自己设置。。。。
只有本站会员才能查看附件,请 登录

只有本站会员才能查看附件,请 登录

只有本站会员才能查看附件,请 登录



第10行,又是什么问题呀。。。
只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2022-5-27 01:34编辑过]

#13
追梦人zmrghy2022-05-27 04:05
回复 10楼 rjsp
不认识 const std::filesystem::path
是因为,const std::filesystem::path 需要C++17的支持。。。

第10行,错误是。。。main.cpp格式不支持宽字符(全角标点符号/汉字)。
可以用UTF-8格式来解决。

现在编译,main.cpp文件中已经没有了错误。。。。
可是fs_path.h头文件中出现了10个错误。。。。
错误信息。。。。
只有本站会员才能查看附件,请 登录




UTF-8让main.cpp文件可以支持宽字符了。。。

这是控制台程序,控制台输入/输出是不支持宽字符的。。。
所以头文件中出错了。。。
还不知道如何解决
#14
rjsp2022-05-27 08:54
不认识 const std::filesystem::path
是因为,const std::filesystem::path 需要C++17的支持。。。
可是我让你下载的 codeblocks-20.03mingw-nosetup 中mingw64默认用的是 C++20 标准;
在菜单 Settings\Compiler\Global compiler settings\Compiler settings\Other compiler options 中有 -std=c++20

第10行,错误是。。。main.cpp格式不支持宽字符(全角标点符号/汉字)。
可以用UTF-8格式来解决。
我之前在10楼就让你
你把Code::Blocks关闭,用记事本把 main.cpp 另存为 UTF-8 格式
然后打开 Code::Blocks,编译参数加
-finput-charset=UTF-8
-fexec-charset=GBK
-fwide-exec-charset=UTF-16LE


10个错误全在fs_path.h头文件中.txt
我看到“C:\MinGW\MinGW\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\bits\fs_path.h”,怎么是“8.1.0”,codeblocks-20.03mingw-nosetup明明自带的是“gcc 11.1.0
#15
追梦人zmrghy2022-05-27 11:46
回复 14楼 rjsp
为什么VS2022 也不认识filesystem 呀。。。

只有本站会员才能查看附件,请 登录
#16
追梦人zmrghy2022-05-27 12:11
回复 14楼 rjsp
可以了呀!!!
谢谢,一直一来的细心讲解。。。
还是中文界面的VS比较熟悉。。。
一会,就找到问题了。
只有本站会员才能查看附件,请 登录

只有本站会员才能查看附件,请 登录

只有本站会员才能查看附件,请 登录
#17
追梦人zmrghy2022-05-27 16:04
回复 14楼 rjsp
重装系统Win11,CodeBlocks第一次解压出来。
打开确实是有-std=c++20,这个选项的。。。。
只有本站会员才能查看附件,请 登录


不知道,如何弄得现在又没有-std=c++20,这个选项了。
删除整个目录,重新解压出来,还是没有。。。
与操作系统也有关系。。。
如何解决呀
只有本站会员才能查看附件,请 登录
#18
rjsp2022-05-27 16:41
这个有没有是无所谓的(只是一个辅佐,可以添加删除),你自己
在菜单 Settings\Compiler\Global compiler settings\Compiler settings\Other compiler options 中添加一个 -std=c++20 (这是全局设置)
或者
打开工程,在菜单 Project\Build options\Other compiler options 中添加一个 -std=c++20 (这是仅对此工程的设置)
但要注意,如果两者都设置的话,不要冲突就行
#19
追梦人zmrghy2022-05-27 17:14
回复 18楼 rjsp
现在找不到,std=c++20呀。。。
还不认识 std::filesystem...

如果,选std=c++17;
就能认识std::filesystem...
但是 fs_path.h 头文件中10个错误。

为什么std=c++20消失了呀。。
#20
追梦人zmrghy2022-05-27 17:36
回复 18楼 rjsp
我好像,看到了Kile Compiler  Kile CX51呀。。
Code::Blocks 还可以做单片机开发呀。。。
#21
rjsp2022-05-27 19:38
Code::Blocks 是个IDE,不是编译器
#22
追梦人zmrghy2022-05-30 13:48
回复 21楼 rjsp
VS2022。C++中如何创建CLR窗体。。。
#23
rjsp2022-05-30 15:05
以下是引用追梦人zmrghy在2022-5-30 13:48:20的发言:

VS2022。C++中如何创建CLR窗体。。。
不会,抵制 managed C++、C++/CLI 等垃圾
1