объект класса не читает из текстового файла с помощью fsream

Привет, у меня проблема с переменной 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;
} 

person user1561949    schedule 26.08.2012    source источник
comment
Я создал текстовый файл с вашим примером СЛЕДУЕТ создать, протестировал код и получил правильные результаты. Пожалуйста, покажите точно, как выглядит текстовый файл, так как он отличается от кода, который вы показываете в разделе СЛЕДУЕТ создавать. (Кроме того, вы всегда должны проверять, что все читаются и пишутся там, где выполняются правильно, так как проблема в том, что ваш поток где-то дает сбой).   -  person Jesse Good    schedule 27.08.2012
comment
Я довольно новичок в С++. Как бы вы проверили правильность выполнения всех операций чтения и записи?   -  person user1561949    schedule 27.08.2012
comment
Я дал ссылку на хорошую рецензию в своем ответе.   -  person Alexander Gessler    schedule 27.08.2012
comment
@jesseGood, я снова запустил свою программу и получил те же результаты, что и опубликовал. Мне любопытно, как вы можете запустить ту же программу и получить правильный результат.   -  person user1561949    schedule 27.08.2012
comment
@ user1561949: Посмотрите мой ответ и дайте мне знать, если мое предположение неверно. Я подозреваю, что путь к файлу неверен.   -  person Jesse Good    schedule 27.08.2012
comment
@Jesse Good: с тех пор, как я начал использовать fstream, я всегда добавлял входной текстовый файл в ту же папку, что и main.cpp, и это всегда работало. и я сделал то же самое здесь. Как исправить путь к файлу?   -  person user1561949    schedule 27.08.2012
comment
@ user1561949: Подобные вещи зависят от ОС. Просмотрите SetCurrentDirectory и GetCurrentDirectory и не забудьте указать windows.h. Кроме того, убедитесь, что вы не ошиблись в имени файла и т. д.   -  person Jesse Good    schedule 27.08.2012


Ответы (2)


Проблема в вашем коде заключается в следующей строке:

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
}

Однако, возможно, было бы лучше немного разбить это.

person Jesse Good    schedule 26.08.2012
comment
:) Вы выиграли пари! Не удалось открыть файл. Я в безвыходном положении, как я могу открыть его, если файл не может быть открыт? - person user1561949; 27.08.2012
comment
@user1561949: user1561949: Самый простой способ — указать полный путь, то есть c:/mydirectory/blah/movie1.txt или, если вы знаете пример относительного пути: debug/movie1.txt. - person Jesse Good; 27.08.2012
comment
вот что я написал, но я все еще не могу открыть файл: ‹pre› ifstream inFile(C:/Documents and Settings/Administrator/My Documents/Visual Studio 2008/Projects/131part1/131part1/movie1.txt) ‹code › - person user1561949; 27.08.2012
comment
@user1561949: Хорошо, чтобы выяснить проблему, проверьте errno, см. здесь для пример. - person Jesse Good; 27.08.2012
comment
Хорошо: я совершил глупую ошибку. Я сохранил входной файл как: movie1.txt.txt Большое спасибо за помощь! - person user1561949; 27.08.2012

Наиболее вероятным случаем является сбой чтения потока, и в этом случае все экземпляры movieType останутся неинициализированными. Вы должны проверять поток на наличие ошибок и обрабатывать их соответствующим образом, то есть if (inFile.fail()) { ... } или просто if (!inFile).

Таким образом, вы печатаете неинициализированную (то есть случайную) память.

Операторы IOStream в C++ обычно не генерируют исключения, абсолютно необходимо выполнять проверку ошибок вручную.

см. этот ответ для хорошего описания темы.

person Alexander Gessler    schedule 26.08.2012