gpt4 book ai didi

c++ - 类对象未使用 fstream 从文本文件中读取

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:50 25 4
gpt4 key购买 nike

您好,我遇到了 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;
}

最佳答案

您的代码中的问题是以下行:

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
}

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

关于c++ - 类对象未使用 fstream 从文本文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12134609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com