vector to output file?

I am unfamiliar with vectors but I still decided to use them in my project.

I am trying to output to .txt files from a vector, and i'm lost.

Heres what I have so far but it wont even compile.
1
2
3
4
5
vector<Book>::iterator ob(outbooks, '\0');
   for ( ob = booklist.begin(); ob != booklist.end(); ++ob ) 
   {  
      copy(booklist.begin(),booklist.end(), ob);
   }


please help.
Thank you.
1
2
3
4
5
vector<Book>::iterator ob;// this ->(outbooks, '\0'); has nothing to do here
   for ( ob = booklist.begin(); ob != booklist.end(); ++ob ) 
   {  
      copy(booklist.begin(),booklist.end(), ob);
   }
Thank you,

But this is not writing to the output file.
Heres my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void Library::addBook()
{
	ofstream outbooks("books.txt", ios::out);
	string title, author;
	int isbn;

	cout << "Enter book's title: " << endl;
	getline(cin, title);
	
	cout << "Enter author's name: " << endl;
	getline(cin, author);
	
	cout << "Enter book's ISBN number: " << endl;
	cin >> isbn;

	//cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

	inbooks = new Book;
	inbooks->setTitle(title);
	inbooks->setAuthor(author);
	inbooks->setISBN(isbn);
	booklist.push_back(*inbooks);
	cin.get();	
   
   //does not write
   vector<Book>::iterator ob;
   for ( ob = booklist.begin(); ob != booklist.end(); ++ob ) 
   {  
      copy(booklist.begin(),booklist.end(), ob);
   }
}
But this is not writing to the output file.
Of course not. You never tell it to do it.

Also, inbooks is leaking.
Thank you, stopped the leakage.

this kind of does it, but it writes the last book, how every many times the list count is.
1
2
3
4
5
6
7
	vector<Book>::iterator ob;
   for ( ob = booklist.begin(); ob != booklist.end(); ++ob ) 
   {  
          outbooks << title << endl;
	  outbooks << author << endl;
	  outbooks << isbn << endl;
   }

I believe I need to count each book before writing them in, maybe a nested loop??
You want to print the book that you are on
outbooks << ob->title << endl;
Last edited on
aha!

thank you!

Topic archived. No new replies allowed.