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

この時点で、なぜそれが行われているのか理解するのに苦労しています。MSVS2008を使用しています。

コードは次のとおりです。

#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 に答える