| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2622 人关注过本帖
标题:函数调用不成功
只看楼主 加入收藏
青紫墨
Rank: 2
等 级:论坛游民
帖 子:67
专家分:20
注 册:2016-8-10
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:10 
函数调用不成功
这个程序的目的是:打开一个目录下的所有txt,分别显示每个txt的最大数,结果是文件打开失败要怎么改呢,各位大神帮帮忙啊!!
图片附件: 游客没有浏览图片的权限,请 登录注册


#include <io.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include<ctype.h>
 
using namespace std;
int Max();
int main()
{
    _finddata_t sFind;
    long lResult = 0;
     
    lResult = _findfirst("*.txt", &sFind);
    if (lResult == -1)
    {
        cout << "没有找到文件。" << endl;
        return 0;
    }
    int c;
    do
    {
        cout << sFind.name << endl;
        c=Max();
        //cout << sFind.size << endl;
        //cout << lResult << endl;
    }
    while(_findnext(lResult, &sFind) != -1);
    getchar();
    // 文件内容读取,得看内内容是什么样的了。
    return 0;
}
int Max()
{
    inline void makeitem(vector<int>& item,istringstream &in );
//int main()
//{
    string line;
    ifstream f("sFind.name");
    if(!f.is_open())
    {
        cout<<"error openfile"<<endl;return 1;
    }
    vector<int> item;
    vector<vector<int> > arr;
    int num=0;
    while(!f.eof())
    {
        int num=0;
        getline(f,line);
        if(line.empty())continue;
        istringstream input(line);
        item.clear();
        while(!input.eof())
        {
            input>>num;
            item.push_back(num);
        }
        arr.push_back(item);
    }
    f.close();
    //output arr
    for (int i=0;i<arr.size();i++)
    {   
        item=arr[i];
   
        for (int j=0;j<item.size();j++)
        {
         
            cout<<item[j]<<" ";
        
        }
   
        cout<<endl;
    }
    int max[]={0};
    for(int i=0;i<arr.size();i++)
    {
        for(int j=0;j<item.size();j++)
        {
            if(arr[i][j]>max[i])
            {
                max[i]=arr[i][j];
            }
        }
    cout<<max[i]<<" ";
    //cout<<endl;
    }
    int m=max[0];
    for(int i=0;i<item.size();i++)
    {
        m=(m>max[i]?m:max[i]);
        
    }
    cout<<m;
    return m;
    getchar();
}
搜索更多相关主题的帖子: include 
2016-08-22 01:03
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
ifstream f("sFind.name");
你每次打开的都是"sFind.name"这个文件

怎么还在写 while(!f.eof()) 这种错误的代码,我记得上次帮你改过
2016-08-22 08:30
青紫墨
Rank: 2
等 级:论坛游民
帖 子:67
专家分:20
注 册:2016-8-10
收藏
得分:0 
嗯嗯是改过的,我昨天粘贴的是我自己的写的没注意,今天早晨改过

来了,运行结果一

cout<<sFind.name是输出txt文件名,这个目录下不只一个txt,我每输出一次就调用子函数,但是打不开txt文件,我查了好多都得要路径,但是我的txt路径是显示器中显示的
2016-08-22 10:04
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
回复 3楼 青紫墨
你不贴代码,我怎么知道你代码中哪句写错了?
2016-08-22 10:40
青紫墨
Rank: 2
等 级:论坛游民
帖 子:67
专家分:20
注 册:2016-8-10
收藏
得分:0 
程序目的是读出一个目录下的所有txt,并显示出哪个txt的哪个数最大





个是主函数#include <io.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include<ctype.h>
 
using namespace std;
int max();
int main()
{
    _finddata_t sFind;
    long lResult = 0;
     
    lResult = _findfirst("*.txt", &sFind);
    if (lResult == -1)
    {
        cout << "没有找到文件。" << endl;
        return 0;
    }
    int c;
    do
    {
        cout << sFind.name << endl;
        c=max();
        //cout << sFind.size << endl;
        //cout << lResult << endl;
    }
    while(_findnext(lResult, &sFind) != -1);
    getchar();
    // 文件内容读取
    return 0;
}
int max()



这个是子函数,上次你帮我改的


int max()
{
    vector<vector<int> > arr;
    // 数据读入
    {
        ifstream f( "sFind.name");//就是这里出问题了,显示failes to open the file
        if( !f )
        {
            cerr << "failed to open the file\n";
            return 1;
        }


        for( string line; getline(f,line); )
        {
            arr.push_back( vector<int>() ); // 如果你用的编译器符合C++11标准,此处最好用 emplace_back


            istringstream input(line);
            for( int num; input>>num; )
                arr.back().push_back( num );


            if( arr.back().empty() )
                arr.pop_back();
        }
    }


    // 数据显示
    for( size_t r=0; r!=arr.size(); ++r )
    {
        for( size_t c=0; c!=arr[r].size(); ++c )
            cout << '\t' << arr[r][c];
        cout << '\n';
    }


    // 求每行最大值
    for( size_t r=0; r!=arr.size(); ++r )
    {
        int maxvalue = arr[r][0];
        for( size_t c=1; c!=arr[r].size(); ++c )
            maxvalue = max( maxvalue, arr[r][c] );
        cout << maxvalue << '\n';
    }
    cout << endl;


    return 0;
}


(我现在电脑不能上网,只能用手机粘贴,代码比较乱,谅解哈,谢谢你)
2016-08-22 11:31
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:20 
回复 5楼 青紫墨
这代码还一样的,错误原因我在二楼已经说过了
ifstream f("sFind.name");
你每次打开的都是"sFind.name"这个文件
你听明白了没有呀?真是急人,你每次打开的是一个文件名叫sFind,扩展名叫name的文件。

程序代码:
#include <io.h>

#include <cstring>
#include <cerrno>

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

bool foo( const char* filename );

int main( void )
{
    std::locale loc = std::locale::global( std::locale(std::locale(),"",std::locale::ctype) );

    bool bfound = false;
    _finddata_t sFind;
    intptr_t lResult = _findfirst( "*.txt", &sFind );
    if( lResult != -1 )
    {
        do
        {
            if( (sFind.attrib&_A_SUBDIR) == 0 )
            {
                bfound = true;
                cout << sFind.name << ":\n";
                foo( sFind.name );
            }
        }
        while( _findnext(lResult,&sFind) != -1 );

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

    return 0;
}

bool foo( const char* filename )
{
    ifstream is( filename );
    if( !is )
    {
        cerr << "\t[ERROR] 文件打开失败.\n";
        return false;
    }

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

    if( !bfound )
    {
        cerr << "\t[WARNING] 未找到int类型数值.\n";
        return false;
    }

    cout << "\t[OUTPUT] 最大值 = " << maxval << '\n';
    return true;
}

2016-08-22 11:58
青紫墨
Rank: 2
等 级:论坛游民
帖 子:67
专家分:20
注 册:2016-8-10
收藏
得分:0 
回复 6楼 rjsp
我明白你的意思,你说我一直读出的是sFind.name这个文件,我的意思是,cout<<sFind.name会在显示器上显示一个txt的文件名(比如abc.txt),怎么把这个文件名填写到刚才ifstream f(" ")里面去,总之就是把这个文件
名怎么传过

2016-08-22 12:20
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
回复 7楼 青紫墨
怎么回事呀?
代码中已经写了,你自己看一下嘛。若不肯看,何必问呢?
2016-08-22 12:44
青紫墨
Rank: 2
等 级:论坛游民
帖 子:67
专家分:20
注 册:2016-8-10
收藏
得分:0 
回复 8楼 rjsp
嗯嗯看到了,不好意思啊,我以为是用字符串传过去的比如string之类的
谢谢你啊!
2016-08-22 14:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 

有事可以在帖子中说
程序代码:
#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 )
{
    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( "*.txt", &sFind );
    if( lResult != -1 )
    {
        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( 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;
}

一个可能的输出是:
a.txt:
        [WARNING] 未找到int类型数值.
b.txt:
        [WARNING] 未找到int类型数值.
data.txt:
        [WARNING] 未找到int类型数值.
funs.txt:
        [WARNING] 未找到int类型数值.
funs_1.txt:
        [OUTPUT] 最大值 = 0
H矩阵.txt:
        [OUTPUT] 最大值 = 504
in.txt:
        [OUTPUT] 最大值 = 24
out.txt:
        [WARNING] 未找到int类型数值.
prime.txt:
        [WARNING] 未找到int类型数值.
student.txt:
        [WARNING] 未找到int类型数值.
test.txt:
        [WARNING] 未找到int类型数值.
test2.txt:
        [WARNING] 未找到int类型数值.
复件 H矩阵.txt:
        [OUTPUT] 最大值 = 504
在所有匹配的文件中,拥有最大值(504)的文件是:
        H矩阵.txt
        复件 H矩阵.txt

2016-08-23 08:31
快速回复:函数调用不成功
数据加载中...
 
   



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

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