C ++通过引用传递字符串数组

时间:2014-04-26 21:17:07

标签: c++ arrays string function parameter-passing

我是C ++的新手,我试图编写一个只使用函数(没有矢量或模板)的程序应该能够读取一个extarnal文件并将所有内容保存在一个数组中。 为了对我的数组进行维度,我首先读取文件中的总行数,并根据taht标注我的数组。 然后我重新读取文件中的所有行,然后我开始将它们存储在数组中。 一切顺利。 但是当我拿到代码并试图让我们成为一个功能时,我就陷入了困境。 我找不到通过引用传递数组的任何方法。 每次我发现各种错误。 有谁可以帮助我吗? 谢谢

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

}

1 个答案:

答案 0 :(得分: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)