解决 main(int argc, char** argv)这种情况下通过命令行传参,改为main函数里面给定参数。

本文是原创文章,未经允许,请勿转载。

  原来程序中是通过运行exe,然后加上参数,然程序运行起来的。也就是main(int argc, char** argv)这里是通过argv参数是从命令行

传过来的。现在想改为在main函数里面给出这个参数,其实我做的是load_image(img, argv[1]);这个函数。然后我希望argv[1]参数是从txt文本

取到的。所以我写了如下程序

    ///////////////////// 把1.txt文件中的所有的图片的路径都push_bach到image_path里面 /////////////////////
    ifstream in("1.txt");
    string filename;
    string line;
    std::vector<string> image_path;
    if (in) // 有该文件
    {
        while (getline(in, line)) // line中不包括每行的换行符
        {
            cout << line << endl;
            image_path.push_back(line);
        }
    }
    else // 没有该文件
    {
        cout << "没有输入的图片的路径!" << endl;
    }
    
    cout<<"image_path[0]= "<<image_path[0]<<endl;
    cout<<"image_path[0] type"<<typeid(image_path[0]).name()<<endl;

    matrix<rgb_pixel> img;
string image_path0_str = image_path[0];
    cout<<"image_path0_str="<<image_path0_str<<endl;

    //load_image(img,image_path[0]);
    string s_path = "/home/student/dlib-master/examples/faces/yangmi_liudehua.jpg";
    cout<<"s_path ="<<s_path<<endl;
    
    
    load_image(img,image_path0_str);
image_path0_str和s_path两个变量进行cout出来的都是 /home/student/dlib-master/examples/faces/yangmi_liudehua.jpg
那我就奇怪了,但是为什么load_image(img,s_path);这样可以load成功,而这个load_image(img,image_path0_str);不?
所以我
   int a= image_path0_str.size();
    int b= s_path.size();
    if(a==b)
    {
       cout<<"这两个变量长度相等"<<endl;
    }
    else
    {
      cout<<"这两个变量长度不相等"<<endl;
    }
    
    if(s_path ==image_path0_str)
    {
      cout<<"这两个变量相等"<<endl;
    }
    else
    {
      cout<<"这两个变量不相等"<<endl;
    }

同事提示我看看长度相等不,有没有空字符之类的。甚至字符串的字节码都可以打出来进行对比,以便发现问题。所以又写了这个,输出的结果是这两个变量长度不相等, 这两个变量不相等。同事提示我,可能是编码的问题。这我就想起来了。我的1.txt是从windows拷贝到Ubuntu1404的。然后果断删除,直接在Ubuntu下建了个1.txt然后输入

这样子之后,我就可以 load_image(img,image_path0_str);成功了。这可是耽搁了我一天的问题。解决了好开心的。

 

posted @ 2017-09-08 18:59  彩印网  阅读(282)  评论(0编辑  收藏  举报