1

I have a 2 dimensional array and I want it to contain the lines from a file.

In my main function I have:

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

header file contains:

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

buildList is defined (updated):

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

I keep getting:

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

Thanks for the help.

2 Answers 2

2

Two errors, the type of array is wrong, and in your inputfile >> ...; statement it should be array[i], not *array[i]. This is correct

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

char[N] is convertable to char* but that does not mean that char[N][M] is convertable to char**.

Sign up to request clarification or add additional context in comments.

4 Comments

I think I may have a third error. I get an error when I try to call buildList with buildList(cardlist, inData). I tried (*cardList)[25] and that didn't seem to work.
undefined reference to 'buildList(char(*)[25], std::basic_ifstream<char, std::char_traits<char> >&)' and also not sure if this is related but 'collect2: error: 1d returned 1 exit status'
That is a different error, it means that the linker cannot find the function you wrote. There are various reason that could be true. I guess the most likely is that you changed the function prototype but you didn't change the function itself? Maybe you should edit the question above and include all your code.
the error seems to have disappeared...maybe I forgot to save. Thanks for the help!
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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.