2014-04-26 27 views
0

我是新的C++,我试图编写一个程序,只使用函数(无向量或模板)应该能够读取extarnal文件并将所有内容保存在一个数组中。 为了确定我的数组的大小,我首先读取了文件中的总行数,并根据我的数组来确定了数组的大小。 然后我重新读取文件中的所有行,并开始将它们存储在数组中。 一切顺利。 但是当我得到代码,并试图让我们成为一个功能,我卡住了。 我找不到任何通过引用传递数组的方法。 每次我发现各种错误。 有人可以帮我吗? 谢谢C++传递一个字符串数组引用

while (!infile.eof()) 
{ 
// read the file line by line 
getline (infile,line); 
++lines_count2; 

// call a function that take out the tags from the line 
parseLine(&allLines[lines_count], lines_count2, line); 

} // end while 



    // FUNCTION PARSE 
    // Within parseHTML, strip out all of the html tags leaving a pure text line. 
    void parseLine(string (&allLines)[lines_count], int lines_count2, string line) 
{ 
// I don-t take empty lines 
if (line!="") 
{ 

// local var 
string del1="<!DOCTYPE", del2="<script"; 
int eraStart=0, eraEnd=0; 

    // I don't take line that start with <!DOCTYPE 
    eraStart = line.find(del1); 
    if (eraStart!=std::string::npos) 
     line.clear(); 

    // I don't take line that start with <script 
    eraStart = line.find(del2); 
    if (eraStart!=std::string::npos) 
    line.clear(); 

    //out 
    cout << "Starting situation: " << line << "\n \n"; 

    // A. counting the occourence of the character ">" in the line 
    size_t eraOccur = count(line.begin(), line.end(), '<'); 
    cout << "numero occorenze" << eraOccur << "\n \n"; 

    // declaring the local var 
    string str2 ("<"), str3 (">"); 

    // B. loop in the line for each occurence...looking for <and> position 
    for (int i=0; i<eraOccur; i++) 
    { 

     // looking for position char "<" 
     eraStart = line.find(str2); 
     if (eraStart!=string::npos) 
     { 
      cout << "first 'needle' found at: " << eraStart << '\n'; 

      // looking for position char ">" 
      eraEnd = line.find(str3); 
      if (eraEnd!=string::npos) 
      cout << "second 'needle' found at: " << eraEnd << '\n'; 
      eraEnd=eraEnd-eraStart+1; 

      //delete everything between <and> 
      cout << "start " << eraStart << " end " << eraEnd << "\n \n";  
      line.erase(eraStart, eraEnd); 
     } 
    } 

cout << "memo situation: " << line << "\n \n"; 

// C. memorize the result into an array 
allLines[lines_count2] = line; 

}  // end if 

}

+0

有什么错误? – userxyz

+0

小心使用.eof()http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Eejin

回答

0

假设你的阵列被宣布为string allLines[lines_count]你应该要申报的功能

void parseLine(string* allLines, int lines_count2, string line)

void parseLine(string allLines[], int lines_count2, string line)

,并调用功能parseLine(allLines, lines_count2, line)

+0

Yesss Youda! yesss .... 谢谢。 我正在寻找不同的解决方案,如: 'void parseLine(string(&allLines)[lines_count],int lines_count2,string line)' 但他们没有工作 – Cubo

相关问题