class对象不是使用fstream从文本文件中读取的

时间:2012-08-26 23:01:15

标签: c++ class fstream

嗨我遇到了fstream变量的问题。我的电影类无法从文本文件中读取信息:

这里输出是产生的:

-858993460 Ì -858993460 -858993460 -9.25596e+061 -858993460 -858993460
-858993460 Ì -858993460 -858993460 -9.25596e+061 -858993460 -858993460
-858993460 Ì -858993460 -858993460 -9.25596e+061 -858993460 -858993460
-858993460 Ì -858993460 -858993460 -9.25596e+061 -858993460 -858993460
-858993460 Ì -858993460 -858993460 -9.25596e+061 -858993460 -858993460
-858993460 Ì -858993460 -858993460 -9.25596e+061 -858993460 -858993460
-858993460 Ì -858993460 -858993460 -9.25596e+061 -858993460 -858993460
-858993460 Ì -858993460 -858993460 -9.25596e+061 -858993460 -858993460

它应该产生:

 110 8.3 2005 275523 A 140 Batman begins
 123 8.2 1965 45515 W 132 For a Few Dollars More
 181 8.1 1946 17648 R 172 The Best Years of Our Lives
 30 8.6 1946 103101 D 130 it's a Wonderful Life
 77 8.3 1952 56368 C 103 Singin' in the Rain
 88 8.3 1995 245089 A 177 Braveheart
 45 8.5 2001 185124 C 122 Amelie
 48 8.5 1962 80746 V 216 Lawrence of Arabia

并输入文字为:

 110 8.3 2005 275523 A 140 Batman begins
 123 8.2 1965 45515 W 132 For a Few Dollars More
 181 8.1 1946 17648 R 172 The Best Years of Our Lives
 30 8.6 1946 103101 D 130 it's a Wonderful Life
 77 8.3 1952 56368 C 103 Singin' in the Rain
 88 8.3 1995 245089 A 177 Braveheart
 45 8.5 2001 185124 C 122 Amelie
 48 8.5 1962 80746 V 216 Lawrence of Arabia
 -1

此时我很难理解为什么会这样做。我正在使用MS VS2008。

这是代码:

#include "movieType.h"
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
movieType movie[9];
ifstream inFile("movie1.txt");

int i =0;

bool notDone=true;
while (notDone) 
{ 
    if (movie[i++].readMovieInfo(inFile)== false)
        notDone=false;    
}

for (int i=0;i<8;++i)
{
    movie[i].printMovieInfo("printList.txt");
}

return 0;
}

和类规范

#include <string>
//include preprocessor directive to give access to string operations

class movieType
{
public:
    movieType();
    //Function: Class constructor
    //Precondition: none
    //Postcondition: instance variable initialized

    ~movieType();
    //Function: class destructor
    //Precondition: object has been initialized
    //Postcondition: memory allocated to class object freed up

    bool readMovieInfo(std::ifstream&);
    //Function: reads one movie at one time from a file
    //Precondition: object has been initialized
    //Postcondition: return false if the rank is <1 else return true

    void printMovieInfo(char*);
    //Function:prints one movie at a time to a file
    //Precondition: object has been initialized
    //Postcondition: none

    char getGenre();
    //Function: returns the movie genre
    //Precondition:object has been initialized
    //Postcondition: none

    int getRank();
    //Function: returns the movie rank
    //Precondition: object has been initialized
    //Postcondition: none

    bool operator>=(movieType) const;
    //Function: overload operator for '<=' for use in heap
    //Precondition: object has been initialized
    //Postcondition:none

    bool operator>(movieType) const;
    //Function: overload operator for '<' for use in heap
    //Precondition: object has been initialized
    //Postcondition:none

private:
    int rank; //movie ranking
    double weight; //calculated wieght for ranking
    int year; //year the movie was released
    int votes; //number of votes 
    char genre; //movie genre
    int length; //movie length in minute
    std::string name; //the name of the movie

};

和类实现:

#include "movieType.h"
//preprocessor directive gives access to movieType class
#include <fstream>
//preprocessor directive gives access fstream operations
#include <string>
//preprocessor directive gives access string operations

using namespace std;
// make fstream and string operations available without calling class std


movieType::movieType()
{
}

movieType::~movieType()
{
}

bool movieType::readMovieInfo(ifstream& inFile)
{
    inFile>>rank>>weight>>year>>votes>>genre>>length;
    getline(inFile,name);

    if (rank < 1)
        return false;
    else
        return true;
}

void movieType::printMovieInfo(char* outFileName)
{
std::ofstream outFile;
if(!outFile.is_open()) 
    outFile.open(outFileName, std::ios::app);
outFile<<name<<" "<<year<<" "<<genre<<" "<<length<<" "<<rank;
outFile<<" "<<weight<<" "<<year<<" "<<votes<<std::endl;

}
int movieType::getRank()
{
return rank;
}

char movieType::getGenre()
{
return genre;
}

bool movieType::operator >=(movieType other) const
{
if (rank >= other.rank)
    return true;
else
    return false;
}

bool movieType::operator >(movieType other) const
{
if (rank > other.rank)
    return true;
else
    return false;
} 

2 个答案:

答案 0 :(得分:1)

代码中的问题如下:

ifstream inFile("movie1.txt");

基本上,您从未检查过该文件是否已成功打开。

尝试以下,并告诉我它的输出结果:

if (!inFile)
{
    std::cout << "Could not open file" << std::endl;
    return 1;
}

我打赌它告诉你文件无法打开。

另外,要检查读取是否成功,请执行以下操作:

if(!(inFile>>rank>>weight>>year>>votes>>genre>>length))
{
     // Something went wrong
}

但是,最好稍微分解一下。

答案 1 :(得分:0)

最可能的情况是流读取失败,在这种情况下,所有movieType实例都将未初始化。您应该检查流中的错误并正确处理它们,即if (inFile.fail()) { ... }if (!inFile)

所以基本上你打印的是未初始化的(即随机的)内存。

C ++中的IOStream操作符通常不会抛出异常,因此进行手动错误检查绝对至关重要。

See this response可以很好地撰写该主题。