从文件中检索和操作数据

时间:2013-04-21 10:41:16

标签: file-io vector

以下代码应该检索与玩家信息相关的数据,将其排序,然后重写现在组织的文件。将举例说明文件。

原始布局:

     3
  2  2

                John  33 M     5
                Anna  20 F     2
                Rody  23 M     1

代码后的内容如下:

     3
  2  2

                Rody  23 M     1
                Anna  20 F     2
                John  33 M     5

我制作了以下代码:

vector<string> playerScoresFromFile(const string filename) //Gets each one of those lines with the name, ..., and score of the person
{
int dim = filename[7] - '0'; // char to integer
vector<string> vec;
string line;

ifstream fin (filename.c_str());


for (int i = 0; i < dim + 1; i++)
{
    getline(fin, line);
}

while(! fin.eof())
{   
    getline(fin, line);
    vec.push_back(line);
}

return vec;
}


vector< vector<int> > readBoardFromFile(const string filename) //gets the board from the file (first 3 numbers)
{
int dim = filename[7] - '0'; // char to integer
string line;
vector< vector<int> >vec(dim, vector<int>(dim));

ifstream fin (filename.c_str());

int i = 0;
int j, k;
while(i < dim)
{
    getline(fin, line);
    int sizeOfLine = line.length();

    if (line[0] == '\0')
    {
        break;
    }
    else
    {
        for (j = 0, k = 0; j < (sizeOfLine / 3); j++, k += 3)
        {
            string elementOfVectorStr = (line.substr(k,3));
            int elementOfVectorInt = stringToInt(elementOfVectorStr);
            if (abs(elementOfVectorInt) > 100) // when the element is a " ", the corresponding integer is always
            {                                  // a very large number, positive or negative
                elementOfVectorInt = 0;
            }
            vec[i][j] = elementOfVectorInt;
        }
    }
    i++;
}

return vec;
}


vector<string> sortPlayersByTime (vector<string> &vec) // Creates a substring of the string extracted by "playerScoresFromFile" and analyses the times (Which are the last numbers to the right)
{
vector<int> timesInt(vec.size());

for (size_t i = 0; i < vec.size(); i++)
{
    string str = vec[i];
    timesInt[i] = stringToInt(str.substr(26));
}


for (size_t i = 0; i < vec.size() - 1; i++)
{
    if(timesInt[i] > timesInt[i+1])
    {
        swap(vec[i], vec[i+1]);
    }
}

return vec;
}



bool isOrdered (const vector<string> vec)  //Checks if the vector is ordered
{
vector<int> timesInt(vec.size());

for (size_t i = 0; i < vec.size(); i++)
{
    string str = vec[i];
    timesInt[i] = stringToInt(str.substr(26));
}

for (size_t i = 0; i < vec.size() - 1; i++)
{
    if(timesInt[i] > timesInt[i+1])
    {
        return false;
    }
}

return true;
}


void writeBoardToFile(vector< vector<int> >&vec, string filename) //Rewrites the board to the file (Those first 3 numbers of the file)
{
ofstream fout(filename.c_str());

for (size_t i = 0; i < vec.size(); i++)
{
    for (size_t j = 0; j < vec.size(); j++)
    {
        if(vec[i][j] != 0)
        {
            fout << setw(3) << vec[i][j];
        }
        else
        {
            fout << setw(3) << " ";
        }
    }

    fout << endl;
}
fout << endl;
}


void vec_to_file(vector<string> vec, string filename) //Rewrites the vector to the file
{

ofstream fout(filename, ios::app);

for (int i = 0; i < vec.size(); i++)
{
    fout << vec[i] <<endl;
}
}


void displayFile (string filename) //Displays the final board to check if it worked
{
vector<string> vec;
string line;

ifstream myfile (filename);

while ( ! myfile.eof() )
{
    getline (myfile, line);
    vec.push_back(line);
}

for (size_t i = 0; i < vec.size(); i++)
{
    cout << vec[i] <<endl;
}

}


int main()
{
    vector< vector<int> > vec = readBoardFromFile("puzzle_2x2_001.txt");

vector<string> vecxz = playerScoresFromFile("puzzle_2x2_001.txt");


writeBoardToFile(vec, "puzzle_2x2_001.txt"); //Writes the board to the file


while (! isOrdered(vecxz))  //This loop should run while they haven't been sorted out, but the program crashes here and I have no idea why.
{
    sortPlayersByTime(vecxz);
}


//vec_to_file(vecxy, "puzzle_2x2_001.txt"); //Should write the vector to the file upon sorting them out successfully.

cin.get();

}

我的问题是程序每次进入while(!isOrdered(vecxz))循环时都会崩溃,但我不明白为什么。任何人都可以帮我一把吗?我要感恩:)

0 个答案:

没有答案