2011-01-29 182 views
3

嗨Iam使用Ubuntu(Linux),使用G ++编译器。C++读取txt文件?

我有一个很奇怪的情况,昨天我的代码工作正常,我没有做任何事情,但今天,它不工作。这里是我的代码:

ifstream file; 
file.open("users.txt", ios::in); 

if(file.is_open()){ 
    int counter = 0; 
    string readLine; 
    file.seekg(0, ios::end); 
    if (file.tellg() == 0) 
     file.close(); 
    else { 
     while(!file.eof()){ 
      getline(file,readLine); 
      cout << "whats happening?" << readLine << endl; 
      // I was suppose to do process here, but i comment it for debug purposes 
     } 
     openFile.close(); 
    } 

我不明白为什么,我花了2的调试时间,昨天,它可以读取用户数据,但今天,我打开相同的项目,但它不能读取该文件。我100%肯定,路径是正确的,文件有内容。我的结果是:

Whats happening? 

这就是所有,没有别的。帮助我,我疯了看这个东西!!!!!!!!

+0

这功课吗? – 2011-01-29 18:50:30

+3

是的,是的。你为什么要问?我上面列出的所有代码都是我自己编写的,当然它的代码非常简单和标准,我只记得 – 2011-01-29 18:52:25

回答

6

file.seekg(0, ios::end);将寻求文件的结尾。在开始阅读之前,你需要回到起点。 is.seekg(0, ios::beg);