Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C++;? 我想从C++中读取一些 JPG< /Cord>文件。我在互联网上搜索过,但找不到解决方案。我不想使用Boost或其他库,只是用C++函数编写它。例如,我的文件夹中有40个图像,名称为“01.jpg,02.jpg,…40.jpg”,我想给出文件夹地址,读取这40个图像并将它们逐个保存在向量中。我试了好几次,但都失败了。我正在使用VisualStudio。有人能帮我吗?多谢各位_C++_Visual Studio 2010_Io - Fatal编程技术网

如何从C++;? 我想从C++中读取一些 JPG< /Cord>文件。我在互联网上搜索过,但找不到解决方案。我不想使用Boost或其他库,只是用C++函数编写它。例如,我的文件夹中有40个图像,名称为“01.jpg,02.jpg,…40.jpg”,我想给出文件夹地址,读取这40个图像并将它们逐个保存在向量中。我试了好几次,但都失败了。我正在使用VisualStudio。有人能帮我吗?多谢各位

如何从C++;? 我想从C++中读取一些 JPG< /Cord>文件。我在互联网上搜索过,但找不到解决方案。我不想使用Boost或其他库,只是用C++函数编写它。例如,我的文件夹中有40个图像,名称为“01.jpg,02.jpg,…40.jpg”,我想给出文件夹地址,读取这40个图像并将它们逐个保存在向量中。我试了好几次,但都失败了。我正在使用VisualStudio。有人能帮我吗?多谢各位,c++,visual-studio-2010,io,C++,Visual Studio 2010,Io,根据您的评论,我意识到您已经提出了一个可行的解决方案。微软喜欢把它推广为一种更安全的替代方案,如果你在C++中在C.编写程序,那么这是正确的,但是有很多更安全的方法来构建一个不需要你管理缓冲区或者知道它最大尺寸的字符串。如果你想对它有一个习惯用法,我建议你使用并使用C++标准库提供的工具。 下面介绍的解决方案使用一个简单的循环,创建文件名并加载图像。我还介绍了终身管理和所有权语义的使用。根据图像的使用方式,您可能需要改用 #包括 #包括 #包括 #包括 #包括 //只是需要一些例子 结构映像 {

根据您的评论,我意识到您已经提出了一个可行的解决方案。微软喜欢把它推广为一种更安全的替代方案,如果你在C++中在C.编写程序,那么这是正确的,但是有很多更安全的方法来构建一个不需要你管理缓冲区或者知道它最大尺寸的字符串。如果你想对它有一个习惯用法,我建议你使用并使用C++标准库提供的工具。 下面介绍的解决方案使用一个简单的循环,创建文件名并加载图像。我还介绍了终身管理和所有权语义的使用。根据图像的使用方式,您可能需要改用

#包括
#包括
#包括
#包括
#包括
//只是需要一些例子
结构映像
{
图像(const std::string&filename):文件名(filename){}
常量std::字符串文件名;
};
std::unique_ptr LoadImage(常量std::字符串和文件名)
{
返回std::unique_ptr(新图像(文件名));
}
无效加载图像(
常量std::字符串和路径,
const std::string和filespec,
标准:矢量和图像)
{

对于(int i=1;我在sdk中尝试findfirstfile和FindNext文件,或者在mfc中尝试cfilefind。从
开始如何(int i=1;我看到了这个问题的答案,关于如何使用Win32 API来实现这一点:这个问题是关于如何为固定文件名生成字符串的,还是关于如何读取目录内容的?这个问题是关于如何读取目录内容的,在我的例子中,它们是“.jpg”文件。我想我已经通过使用“sprintf_s”解决了这个问题在visual studio中运行。谢谢大家。
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <stdexcept>

// Just need something for example
struct Image
{
    Image(const std::string& filename) : filename_(filename) {}
    const std::string filename_;
};

std::unique_ptr<Image> LoadImage(const std::string& filename)
{
    return std::unique_ptr<Image>(new Image(filename));
}

void LoadImages(
    const std::string& path,
    const std::string& filespec,
    std::vector<std::unique_ptr<Image>>& images)
{
    for(int i = 1; i <= 40; i++)
    {
        std::stringstream filename;

        // Let's construct a pathname
        filename
            << path
            << "\\"
            << filespec
            << std::setfill('0')    // Prepends '0' for images 1-9
            << std::setw(2)         // We always want 2 digits
            << i
            << ".jpg";

        std::unique_ptr<Image> img(LoadImage(filename.str()));
        if(img == nullptr) {
            throw std::runtime_error("Unable to load image");
        }
        images.push_back(std::move(img));
    }
}

int main()
{
    std::vector<std::unique_ptr<Image>>    images;

    LoadImages("c:\\somedirectory\\anotherdirectory", "icon", images);

    // Just dump it
    for(auto it = images.begin(); it != images.end(); ++it)
    {
        std::cout << (*it)->filename_ << std::endl;
    }
}