来自文件输入的c ++数组

时间:2013-11-18 07:28:59

标签: c++ arrays file

我有一个二维数组,我希望它包含文件中的行。

在我的主要功能中,我有:

const int listSize = 5;
char cardList[listSize][25];
buildList(cardList, inData);

头文件包含:

void buildList(char (*array)[25], std::ifstream&);

buildList已定义(更新):

void buildList(char (*array)[25], ifstream& inputFile){
    for (int i = 0; i < 5; i++)
        inputFile >> array[i];
}

我一直在:

cannot convert 'char (*)[25]' to 'char**' for argument '1' to 'void buildList(char**, std::ifstream&)'

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

两个错误,array的类型错误,在inputfile >> ...;语句中,它应该是array[i],而不是*array[i]。这是正确的

void buildList(char (*array)[25], ifstream& inputFile){
    for (int i = 0; i < 5; i++)
        inputFile >> array[i];
}

char[N]可转换为char*,但这并不意味着char[N][M]可转换为char**

答案 1 :(得分:0)

//part of the question is about function signature .. the types of parameters
//below we have completely described the type again
//slight improvement of parameter type .. not recommended
//I hope I had not said too much and too little at the same time
//3 possible improvements follow
void buildlist1( char (&da)[5][25], std::ifstream & ifs)
{
    for (int i = 0; i < 5; ++i)//not recommended explicit magic number
    {
        da[i][0] = 0;//compensate for empty file/stream
        ifs >> da[i];//dangerous
        cout << da[i] << "...";//debugging danger data can be lost with data larger than allocated
    }

}
//cleaner parameter type
struct maintaininfo
{
    //consider this struct as possible data to pass as reference or value
    static const int x1 = 5;
    static const int x2 = 25;
    //the array info is available
    char data[x1][x2];
};
void buildlist2( maintaininfo & mi, std::ifstream & ifs)
{
    for (int i = 0; i < maintaininfo::x1; ++i)//number defined in struct/class
    {
        mi.data[i][0] = 0;//compensate for empty file/stream
        ifs >> mi.data[i];//dangerous overflow possibly
        cout << mi.data[i] << "...";//debugging danger data can be lost with data larger than allocated
    }
    //elided similar to above
}


// IMHO I would prefer something in this direction
// The person posing question may have contrary priorities and constraints
void buildlistmodern( std::vector<string> & svs, std::ifstream & ifs)
{
    for (int i = 0; i < 5; ++i)//magic explicit number
    {
        std::string s;//compensate for empty file
        ifs >> s;
        //possibly process the string s here again
        svs.push_back(s);
        cout << svs[i] << "...";
    }

}
int readfile()
{
    const int listsize = 5;
    auto filename = "c:\\delete\\delete.txt";
    {
        cout << endl << "this seems unsafe and old fashioned arrays for casual use are dangerous" << endl ;
        std::ifstream ifs(filename);
        if (ifs.good())
        {
            char cardlist[listsize][25];//dangerous magic explicit numbers
            buildlist1(cardlist, ifs);
            cout << endl << "final tally" << endl;
            for (int i = 0; i < 5; ++i)
            {
                cout << cardlist[i] << "...";
            }
        }
        else cout << "File Problem" << endl;
    }
    {
        cout << endl << "array is encapsulated within type" << endl ;
        std::ifstream ifs(filename);
        if (ifs.good())
        {
            maintaininfo cardlist;
            buildlist2(cardlist, ifs);
            cout << endl << "final tally" << endl;
            for (int i = 0; i < 5; ++i)
            {
                cout << cardlist.data[i] << "...";
            }
        }
        else cout << "File Problem" << endl;
    }




    {
        cout << endl << "this looks more modern ... but may be beyond the scope of the question" << endl;
        std::ifstream ifs(filename);
        if (ifs.good())
        {
            std::vector<string>svs;
            buildlistmodern(svs, ifs);
            cout << endl << "final tally "<< endl;
            if (svs.size() == 0) cout << "No data in file" << endl;
            for (const auto & s : svs)
            {
                cout << s << "...";
            }
            cout << endl << "fixed method ... not recommended ...but you might know better" << endl;
            for (int i = 0; i < 5; ++i)
            {
                cout << svs[i] << "...";
            }
        }
        else cout << "File Problem" << endl;

    }
    return 0;

}