标题:为什么同一个程序在cmd和vs2012里调试结果不同
只看楼主
青紫墨
Rank: 2
等 级:论坛游民
帖 子:67
专家分:20
注 册:2016-8-10
结帖率:100%
已结贴  问题点数:20 回复次数:6 
为什么同一个程序在cmd和vs2012里调试结果不同
下面是同一个程序的在cmd和vs2012里的不同显示结果,还有为什么不同路径下有的txt可以打开有的打开失败呢?求指教啊
代码:
#include <io.h>

#include <cstring>
#include <cerrno>

#include <iostream>
#include <fstream>
#include <sstream>
#include <locale>
#include <string>
#include <limits>
#include <vector>
#include <algorithm>
using namespace std;

// 0: 成功
// -1: 文件打开失败
// -2: 未找到int类型数值
int foo( const char* filename, int& maxval );

int main()
{

    string fileName = "D:\\VS2012\\CPP\\linemax1\\linemax1\\*.txt";
    int maxval_of_allfile;
    vector<string> allfile_with_maxval;
  
    _finddata_t sFind;  
    long handle = _findfirst(fileName.c_str(), &sFind);  
   
    bool bfound = false;
    if (handle == -1L)  
    {  
        cerr << "failed to transfer files" << endl;  
        return false;  
    }  
  
    do   
    {  
        if( (sFind.attrib&_A_SUBDIR) == 0 )
            {
                bfound = true;

                cout << sFind.name << ":\n";
                int maxval;
                switch( foo(sFind.name,maxval) )
                {
                case 0:
                    cout << "\t[OUTPUT] 最大值 = " << maxval << '\n';
                    if(maxval>10||maxval<-10)
                    {
                        cout<<"Beyond"<<endl;
                        cout<<'\a'<<endl;
                    }
                    if( allfile_with_maxval.empty() )
                    {
                        maxval_of_allfile = maxval;
                        allfile_with_maxval.push_back( sFind.name );
                    }
                    else if( maxval == maxval_of_allfile )
                    {
                        allfile_with_maxval.push_back( sFind.name );
                    }
                    else if( maxval > maxval_of_allfile )
                    {
                        maxval_of_allfile = maxval;
                        allfile_with_maxval.clear();
                        allfile_with_maxval.push_back( sFind.name );
                    }
                    break;
                case -1:
                    cout << "\t[ERROR] 文件打开失败.\n";
                    break;
                case -2:
                    cout << "\t[WARNING] 未找到int类型数值.\n";
                    break;
                }//cout <<sFind.name <<endl;
            }  
         
    } while (_findnext(handle, &sFind) == 0);  
      
    //system("pause");
    //return true;  
    if( allfile_with_maxval.empty() )
        cout << "所有匹配的文件中皆无int类型.\n";
    else
    {
        cout << "在所有匹配的文件中,拥有最大值(" << maxval_of_allfile << ")的文件是:\n";
        for( size_t i=0; i!=allfile_with_maxval.size(); ++i )
            cout << '\t' << allfile_with_maxval[i] << '\n';

    }
    system("pause");
    return 0;
}

int foo( const char* filename, int& maxval )
{
    maxval = std::numeric_limits<int>::min();

    ifstream is( filename );
    if( !is )
        return -1;

    bool bfound = false;
    for( string s; is>>s; )
    {
        istringstream ss( s );
        int val;
        if( ss>>val && ss.eof() )
        {
            bfound = true;
            maxval = max( maxval, val );
            
        }
    }

    if( !bfound )
        return -2;

    return 0;
}

搜索更多相关主题的帖子: include limits 
2016-08-24 19:29
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
手机上没法仔细看,竟然删掉了设置locale那句。回正题:因为打开文件时参数只包含文件名,那么它就是打开“当前目录”(可用winapi函数GetCurrentDirectory活得当前目录)下的文件。
2016-08-24 21:35
青紫墨
Rank: 2
等 级:论坛游民
帖 子:67
专家分:20
注 册:2016-8-10
得分:0 
回复 2楼 rjsp
没太懂,可是我定义了打开的绝对目录啊,我在设断点后观察,两个目录,一个就能正常打开,
一个就直接打不开,局部变量那些也一模一样,txt里的文件格式也一样。而且cmd和vs显示的不一样,好诡异啊
2016-08-24 23:03
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
当你将”1.txt"传给foo函数后,你认为foo函数应该打开哪个目录下的1.txt。是C盘A目录下的1.txt还是D盘abc目录下的1.txt,抑或是其它?
2016-08-24 23:37
青紫墨
Rank: 2
等 级:论坛游民
帖 子:67
专家分:20
注 册:2016-8-10
得分:0 
回复 4楼 rjsp
肯定是我定义的那个目录下的啊,怎么改呢,还有我想不通为什么会出现不同的显示结果
2016-08-25 00:15
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
以下是引用rjsp在2016-8-24 23:37:36的发言:

当你将”1.txt"传给foo函数后,你认为foo函数应该打开哪个目录下的1.txt。是C盘A目录下的1.txt还是D盘abc目录下的1.txt,抑或是其它?
凭什么foo能知道"1.txt"就是"D:\\VS2012\\CPP\\linemax1\\linemax1\\1.txt"?
假如你在main函数中不是搜索一个目录,而是搜索两个目录,比如搜索C:\和D:\,你传给foo一个"1.txt",foo是神仙能分辨出你想打开C:\1.txt还是D:\1.txt?
就是在现实生活中,如果你的老板要求“将空调清理一下”,你肯定也得问一句“是哪个房间的空调?”,为什么到了程序设计时就认为foo能神仙般知道是哪个目录下的"1.txt"呢?

#include <io.h>

#include <cstring>
#include <cerrno>

#include <iostream>
#include <fstream>
#include <sstream>
#include <locale>
#include <string>
#include <limits>
#include <vector>
#include <algorithm>
using namespace std;

// 0: 成功
// -1: 文件打开失败
// -2: 未找到int类型数值
int foo( const char* filename, int& maxval );

int main( void )
{
    const char* searchfolder = "D:\\VS2012\\CPP\\linemax1\\linemax1\\";
    const char* searchfilter = "*.txt";


    std::locale loc = std::locale::global( std::locale(std::locale(),"",std::locale::ctype) );

    int maxval_of_allfile;
    vector<string> allfile_with_maxval;

    bool bfound = false;
    _finddata_t sFind;
    intptr_t lResult = _findfirst( (string(searchfolder)+searchfilter).c_str(), &sFind );
    if( lResult != -1 )
    {
        do
        {
            if( (sFind.attrib&_A_SUBDIR) == 0 )
            {
                bfound = true;

                cout << sFind.name << ":\n";
                int maxval;
                switch( foo((string(searchfolder)+sFind.name).c_str(),maxval) )
                {
                case 0:
                    cout << "\t[OUTPUT] 最大值 = " << maxval << '\n';
                    if( allfile_with_maxval.empty() )
                    {
                        maxval_of_allfile = maxval;
                        allfile_with_maxval.push_back( sFind.name );
                    }
                    else if( maxval == maxval_of_allfile )
                    {
                        allfile_with_maxval.push_back( sFind.name );
                    }
                    else if( maxval > maxval_of_allfile )
                    {
                        maxval_of_allfile = maxval;
                        allfile_with_maxval.clear();
                        allfile_with_maxval.push_back( sFind.name );
                    }
                    break;
                case -1:
                    cout << "\t[ERROR] 文件打开失败.\n";
                    break;
                case -2:
                    cout << "\t[WARNING] 未找到int类型数值.\n";
                    break;
                }
            }
        }
        while( _findnext(lResult,&sFind) != -1 );

        _findclose( lResult );
    }
    if( errno != ENOENT )
    {
        cout << strerror(errno) << '\n';
        return 1;
    }
    else if( !bfound )
    {
        cout << "没有找到匹配的文件.\n";
        return 2;
    }

    if( allfile_with_maxval.empty() )
        cout << "所有匹配的文件中皆无int类型.\n";
    else
    {
        cout << "在所有匹配的文件中,拥有最大值(" << maxval_of_allfile << ")的文件是:\n";
        for( size_t i=0; i!=allfile_with_maxval.size(); ++i )
            cout << '\t' << allfile_with_maxval[i] << '\n';
    }

    return 0;
}

int foo( const char* filename, int& maxval )
{
    maxval = std::numeric_limits<int>::min();

    ifstream is( filename );
    if( !is )
        return -1;

    bool bfound = false;
    for( string s; is>>s; )
    {
        istringstream ss( s );
        int val;
        if( ss>>val && ss.eof() )
        {
            bfound = true;
            maxval = max( maxval, val );
        }
    }

    if( !bfound )
        return -2;

    return 0;
}
2016-08-25 08:31
青紫墨
Rank: 2
等 级:论坛游民
帖 子:67
专家分:20
注 册:2016-8-10
得分:0 
又一次感谢版主啊,大神功力深厚哪
2016-08-25 19:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-468164-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.583663 second(s), 9 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved