C ++ ifstream工作但不是流

时间:2014-04-18 16:35:12

标签: c++ ifstream ofstream

基本上当我关闭程序时,矢量被放入一个txt文件中,当程序启动时,它会从该文件中读取。

如果我在txt文件中放入一些信息并启动程序,它可以正常工作(ifstream工作) 但是如果没有文件(ofstream不起作用)我就无法在文件中添加内容/创建文件

这是我的代码:

Library::Library()
{
    ifstream fin("libBooks.txt");
    string inputLine;
    if (fin.fail())
    {
        lastBookIDUsed = 0;
    }
    getline(fin, inputLine);
    lastBookIDUsed = stringToInt(inputLine);
    while (!fin.eof())
    {
        getline(fin, inputLine);
        if ((!fin.eof())&&(inputLine.length()!=0))
        {
            vector<string> token = tokenize(inputLine);
            addBook(token[1], token[2], token[3], stringToInt(token[0]), stringToInt(token[4]));
        }
        else
        {
            break;
        }
    }
    fin.close();

    ifstream finPatron("libPatrons.txt");
    string inputLinePatron;
    if (finPatron.fail())
    {
        lastPatronIDUsed = 0;
    }
    getline(finPatron, inputLinePatron);
    lastPatronIDUsed = stringToInt(inputLinePatron);
    while (!finPatron.eof())
    {
        getline(finPatron, inputLinePatron);
        if ((!finPatron.eof()) && (inputLinePatron.length() != 0))
        {
            vector<string> token = tokenize(inputLinePatron);
            vector<int> TEMPbooksCheckedOutID;
            //Create a vector for the IDs of books this patron has
            int numOfBooksCheckedOut = token.size() - 5;
            if (numOfBooksCheckedOut < 1)
            {               
                for (int i = 0; i < numOfBooksCheckedOut; i++)
                {
                    TEMPbooksCheckedOutID.push_back(stringToInt(token[4 + i]));
                }
            }
            addPatron(token[1], token[2], token[3], stringToInt(token[0]), TEMPbooksCheckedOutID, stringToInt(token[4 + numOfBooksCheckedOut])); 
        }
        else
        {
            break;
        }
    }
    finPatron.close();
}

Library::~Library()
{
    ofstream fout("libBooks.txt");
    if (!fout.is_open())
        std::cout << "Could not open libBooks" << endl;
    fout << lastBookIDUsed << endl;

    vector<Book>::iterator iter;

    iter = vBook.begin();

    while (iter != vBook.end())
    {
        if (!(*iter).isDeleted)
        {
            fout << (*iter).id << " ";
            fout << (*iter).name << ";";
            fout << (*iter).author << ";";
            fout << (*iter).subject << ";";
            fout << (*iter).checkOutBy << endl;

        }
        iter++;
    }
    fout.close();

    ofstream foutPatron("libPatrons.txt");
    if (!foutPatron.is_open())
        std::cout << "Could not open libPatrons" << endl;
    foutPatron << lastPatronIDUsed << endl;

    vector<Patron>::iterator iterPatron;

    iterPatron = vPatron.begin();

    while (iterPatron != vPatron.end())
    {
        if (!(*iterPatron).isDeleted)
        {
            foutPatron << (*iterPatron).id << " ";
            foutPatron << (*iterPatron).name << ";";
            foutPatron << (*iterPatron).phoneNumber << ";";
            foutPatron << (*iterPatron).email << ";";

            vector<int>::iterator iter2;
            iter2 = (*iterPatron).booksCheckedOutID.begin();

            while (iter2 != (*iterPatron).booksCheckedOutID.end())
            {
                foutPatron << (*iter2) << ";";
                iter2++;
            }
            foutPatron << (*iterPatron).hasAtLeastOneBook << endl;
        }
        iterPatron++;
    }
    foutPatron.close();
}

谢谢!

0 个答案:

没有答案