我是新的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
}
有什麼錯誤? – userxyz
小心使用.eof()http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Eejin