2017-12-06 9 views
0

私は、書籍情報(タイトル、著者など)の複数のセットを持つテキストファイルを持っています。私は、ファイルから読み込み、各情報を対応する文字列に割り当てるためにループを使用できる必要があります。私はファイル全体を見渡す場所に作業をしていますが、ファイルを調べているうちにうんざりしています。テキストファイルから特定の要素を読み取るにはどうすればよいですか?

Gone With the Wind, Margaret Mitchell, Hardcover, 1936, 17.49 
The Adventures of Sherlock Holmes, Arthur Doyle, Paperback, 1892, 6.85 
The Illustrated A Brief History of Time, Stephen Hawking, Hardcover, 1996, 9.59 
Frankenstein, Mary Shelley, Paperback, 1818, 7.99 
Command Authority, Tom Clancy, Paperback, 2013, 15.99 
Origin, Dan Brown, Ebook, 2017, 14.99 
The Lost Order, Steve Berry, Audiobook, 2017, 5.95 
The Hunt for Red October, Tom Clacy, Audiobook, 1984, 7.00 
Patriot Games, Tom Clancy, Audiobook, 1987, 22.50 
The 14th Colony, Steve Berry, Paperback, 2016, 9.99 
The Bishop's Pawn, Steve Berry, Ebook, 2018, 14.99 
Pride and Prejudice, Jane Austen, Ebook, 1813, 8.99 
Sense and Sensibility, Jane Austen, Hardcover, 1811, 19.99 
Wuthering Heights, Emily Bronte, Paperback, 1847, 6.99 
Jane Eyre, Charlotte Bronte, Hardcover, 1847, 10.95 
Anna Karenina, Leo Tolstoy, Paperback, 1877, 5.99 
Sahara, Clive Cussler, Ebook, 1992, 5.99 
The Notebook, Nicholas Sparks, Hardcover, 1996, 12.59 
A Walk to Remember, Nicholas Sparks, Ebook, 1999, 7.99 
See Me, Nicholas Sparks, Ebook, 2015, 7.99 
The Last Song, Nicholas Sparks, Paperback, 2009, 5.99 
The Wedding, Nicholas Sparks, Ebook, 2003, 7.99 

私の思考は、それは、コンマまで読んで文字列に情報のその部分を割り当て、その後、継続するということでした。ここで

Book readOne(ifstream &fin) { 
string titleOne; 
getline(fin, titleOne, ','); 
string firstOne; 
getline(fin, firstOne, ','); 
string lastOne; 
getline(fin, lastOne, ','); 
string formatOne; 
getline(fin, formatOne, ','); 
string pubDateOne; 
getline(fin, pubDateOne, ','); 
string priceOne; 
getline(fin, priceOne); 

はテキストファイルです。代わりに、特定のコンマが表示されないかのように出力されます。

Gone With the Wind by Margaret Mitchell Hardcover on 1936. Published on 17.49 
The Adventures of Sherlock Holmes. It costs $0 
The Illustrated A Brief History of Time by Stephen Hawking Hardcover on 1996. Published on 9.59 
Frankenstein. It costs $0 
Command Authority by Tom Clancy Paperback on 2013. Published on 15.99 
Origin. It costs $0 
The Lost Order by Steve Berry Audiobook on 2017. Published on 5.95 
The Hunt for Red October. It costs $0 
Patriot Games by Tom Clancy Audiobook on 1987. Published on 22.50 
The 14th Colony. It costs $0 
The Bishop's Pawn by Steve Berry Ebook on 2018. Published on 14.99 
Pride and Prejudice. It costs $0 
Sense and Sensibility by Jane Austen Hardcover on 1811. Published on 19.99 
Wuthering Heights. It costs $0 
Jane Eyre by Charlotte Bronte Hardcover on 1847. Published on 10.95 
Anna Karenina. It costs $0 
Sahara by Clive Cussler Ebook on 1992. Published on 5.99 
The Notebook. It costs $0 
A Walk to Remember by Nicholas Sparks Ebook on 1999. Published on 7.99 
See Me. It costs $0 
The Last Song by Nicholas Sparks Paperback on 2009. Published on 5.99 
The Wedding. It costs $0 
+0

アレイをロードするためのここでは、whileループを参照してください。https://stackoverflow.com/a/7868998/2836621およびhttps://stackoverflow.com/a/19473883/2836621 –

答えて

0

これはcsv(カンマ区切り値)ファイルに過ぎません。そしてそれを読むための十分なコードサンプルがあります。 this sampleを参照してください。

関連する問題