【发布时间】:2015-04-16 09:41:06
【问题描述】:
这是我的代码
enum Status {IN, OUT };
const int TITLE_SIZE = 50, ISBN_SIZE = 13, AUTHOR_SIZE = 25;
struct Info
{
char title[TITLE_SIZE];
char isbn[ISBN_SIZE];
char author[AUTHOR_SIZE];
Status inOrOut;
};
int main()
{
fstream dataFile;
string filename;
int numOfBooks = 0;
Info *test = 0;
int enumHolder = 0;
cout << "How many books does the file contain? ";
cin >> numOfBooks;
test = new Info[numOfBooks];
cout << "Enter a file (with path) for input and output: ";
cin >> filename;
dataFile.open(filename.c_str(), ios::in );
if (dataFile.fail())
{
cout << "Could not open file; closing program" << "\n";
return 0;
}
for (int i=0; i < (numOfBooks-1); i++)
{
dataFile >> test[i].title;
dataFile >> test[i].isbn;
dataFile >> test[i].author;
dataFile >> enumHolder;
test[i].inOrOut = static_cast<Status>(enumHolder);
}
for (int j=0; j < (numOfBooks-1); j++)
{
cout << test[j].title << " ";
cout << test[j].isbn << " ";
cout << test[j].author << " ";
cout << test[j].inOrOut << " ";
cout << "\n";
}
这是 .txt 文件
书
012345678901
哥们儿
1 那篇文章
210987654321
先生。教授博士
0
这是输出
该文件包含多少本书? 2
输入输入和输出的文件(带路径):d:/documents/input.txt
书 012345678901 0
问题
在第一个测试[i]中的数据文件停止阅读.Author?是使用 static_cast 造成的吗?
【问题讨论】:
标签: c++ file structure static-cast