1

嗨,我遇到了 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;
} 
4

2 回答 2

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
}

但是,最好将其分解一下。

于 2012-08-26T23:26:40.823 回答
0

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

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

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

请参阅此回复以获取有关该主题的良好文章。

于 2012-08-26T23:16:04.533 回答