c ++ ifstream不读取整数

时间:2017-10-26 03:31:24

标签: c++ readfile ifstream

我的ifstream没有正确读取整数。我的main如下:

#include <fstream>
#include <vector>
#include <string>

int main() {

    vector<int> myvec;

    Tree myAVL;

    ifstream myfile;
    string filename;

    cout << "Enter the file name you would like to use: ";
    cin >> filename;

    myfile.open(filename);

    while (!myfile) {
        cout << "ERROR: INVALID FILENAME -- Please input valid filename: ";
        cin >> filename;
        myfile.open(filename);
    }

    while (!myfile.eof()) {
        int x;
        myfile >> x;

        if (myvec.size() == 0) {
            myvec.push_back(x);
            myAVL.insert(x);
            continue;
        }

        if (checkVecDuplicate(myvec, x))
        {
            myAVL.insert(x);
            myvec.push_back(x);
        }
    }

    for (int i = 0; i < myvec.size(); i++)
    {
        cout << myvec[i];
    }

    cout << endl << endl;

    Node* temp = myAVL.head;

    myAVL.inorder(temp);
    cout << endl << endl;
    myAVL.preorder(temp);
    cout << endl << endl;

    system("pause");

    return 0;
}

我试图阅读的文字文件是:

14
32
64
55
1
12
3
4
16
72
125
54

为什么这不读取任何整数?在myfile >> x,它读取整数的空值,我不知道为什么。谢谢!

0 个答案:

没有答案