2013-11-18 2 views
1

У меня есть 2-мерный массив, и я хочу, чтобы он содержал строки из файла.C++ массив из ввода файла

В моей главной функции у меня есть:

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

Ошибка 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**.

+0

Возможно, у меня есть третья ошибка. Я получаю сообщение об ошибке при попытке вызвать buildList с помощью buildList (cardlist, inData). Я попробовал (* cardList) [25], и это, похоже, не сработало. – WorldDominator

+0

@WorldDominator Что говорит об ошибке? – john

+0

undefined ссылка на 'buildList (char (*) [25], std :: basic_ifstream > &)', а также не уверен, что это связано, но 'collect2: ошибка: 1d возвращен 1 статус выхода ' – WorldDominator

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; 

} 
Смежные вопросы