【发布时间】:2016-09-14 21:59:01
【问题描述】:
我应该创建一个程序来索引我收藏中的书籍。该结构包含通常的书籍信息:标题、作者、出版商等。但是,我没有得到任何输出。一个问题是标题会有空格。
/* Book Inventory assignment 2 by Heath Martens. */
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <cstring> // I had to throw this in in order to get memcpy to work.
using namespace std;
typedef struct book{
char title[100];
char author[100];
char publisher[100];
float price;
int isbn;
int pages;
int copies;
} Book;
Book collection[100];
int currentIndex;
void
indexBook(Book *my_book)
{
memcpy(&collection[currentIndex], my_book, sizeof(Book));
currentIndex++;
}
void
readfile(void)
{
fstream my_stream;
string line = " ";
my_stream.open("input.txt");
int i=0;
for (i = 0; i < currentIndex; i++)
{
while (getline(my_stream, line))
{
cin >> line >> collection[i].title;
cin >> line >> collection[i].author;
cin >> line >> collection[i].publisher;
cin >> line >> collection[i].price;
cin >> line >> collection[i].isbn;
cin >> line >> collection[i].pages;
cin >> line >> collection[i].copies;
}
}
my_stream.close();
}
void
printCollection(void)
{
int i;
for (i = 0; i < currentIndex; i++)
{
cout << "Title: " << "\t\t" << collection[i].title << endl;
cout << "Author: " << "\t" << collection[i].author << endl;
cout << "Publisher: " << "\t" << collection[i].publisher << endl;
cout << "Price: " << "\t\t" << collection[i].price << endl;
cout << "ISBN: " << "\t\t" << collection[i].isbn << endl;
cout << "Pages: " << "\t\t" << collection[i].pages << endl;
cout << "Copies: " << "\t" << collection[i].copies << endl;
}
}
void printCollection(void);
int
main(void)
{
currentIndex = 0;
Book *my_book = new Book;
indexBook(my_book);
readfile();
printCollection();
delete my_book;
return 0;
}
这是我指定使用的 txt 文件。
Magician: Apprentice
Raymond E. Feist
Spectra (January 1, 1994)
5.02
0553564943
512
1
Magician: Master
Raymond E. Feist
Spectra (January 1, 1994)
7.99
0553564935
499
1
这是基于所提供的一些示例的更新代码。
void
readfile(void)
{
fstream my_stream ("input.txt");
if(!my_stream)
{
return;
}
string line = " ";
int i=0;
for (i = 0; i < currentIndex; i++)
{
if(!std::getline(my_stream, line))
{
break;
}
memcpy(collection[currentIndex].title, line.c_str(), std::min(sizeof(collection[currentIndex].title), line.size()));
memcpy(collection[currentIndex].author, line.c_str(), std::min(sizeof(collection[currentIndex].author), line.size()));
memcpy(collection[currentIndex].publisher, line.c_str(), std::min(sizeof(collection[currentIndex].publisher), line.size()));
my_stream >> collection[currentIndex].price;
my_stream >> collection[currentIndex].isbn;
my_stream >> collection[currentIndex].pages;
my_stream >> collection[currentIndex].copies;
my_stream.ignore();
if(!my_stream)
{
break;
}
}
【问题讨论】:
-
如果您使用 C++,您应该考虑使用
std::string而不是 char 数组。 -
在互联网上搜索“stackoverflow c++ 读取文件结构”,了解如何将数据文件读入结构的示例。已经有太多类似的问题和答案了。
-
提示:无论您的 txt 文件中有多少本书,您似乎只增加一次
currentIndex。 -
请提出一个具体的问题,并展示一个您尝试过但不起作用的最小示例。
-
这不是问题,但是
std::fstream my_stream("input.txt");会打开文件;无需单独致电my_stream.open()。std::fstream的析构函数将关闭文件;无需单独致电my_stream.close()。