C
Another fatty.Why do you use C's language in C++? You'll have a C++ or a classic C. Your code looks like "The cool kid in the fari, and there's an old wrinkled grandmother sitting next to him on the seat."If you use Russian letters, you must use wstring. It's getting rid of a bunch of bagos. Old methods can't work with long charcoding.Why use the printers and the masses? When do the pros have a great thing as a list? !#include <iostream>
#include <list>
#include <fstream>
using namespace std;
typedef struct {
string author;
string book;
unsigned short published;
float price;
string category;
} Bookshelf;
list<Bookshelf> bookshelf;
void add(string author, string book, unsigned short published, float price, string category) {
Bookshelf item = *(new Bookshelf());
item.author = author;
item.book = book;
item.published = published;
item.price = price;
item.category = category;
bookshelf.push_back(item);
}
void freemem(void) {
bookshelf.clear();
}
int main(void) {
add("Vasilij Pupkin", "Kniga", 1990, 20, "Hlam");
add("Tester", "testie", 1991, 10.5, "Hlam");
// write out
ofstream out("output.txt");
for (auto book: bookshelf) {
out << book.author << endl << book.book << endl
<< book.published << endl << book.price << endl
<< book.category << endl;
}
freemem();
out.flush();
out.close();
// read in
string author, book, published, price, category;
ifstream in("output.txt");
while (!in.eof()) {
if (getline(in, author) && getline(in, book) &&
getline(in, published) && getline(in, price) &&
getline(in, category))
{
add(author, book, stoi(published), stof(price), category);
}
}
in.close();
// list
for (auto book: bookshelf) {
cout << "Author: " << book.author << endl
<< "Book: " << book.book << endl
<< "Published: " << book.published << endl
<< "Price: " << book.price << endl
<< "Category: " << book.category << endl << endl;
}
freemem();
return 0;
}
Exclusion.root@__server__:/path/writefile# ./writer
Author: Vasilij Pupkin
Book: Kniga
Published: 1990
Price: 20
Category: Hlam
Author: Tester
Book: testie
Published: 1991
Price: 10.5
Category: Hlam
root@__server__:/path/writefile# cat output.txt
Vasilij Pupkin
Kniga
1990
20
Hlam
Tester
testie
1991
10.5
Hlam