ID, 책 이름, 작성자, 작성한 연도, 장르 등으로 구성된 책 목록이 파일에 있습니다. 나는파일에서 읽은 줄 분리하기 C++
(추출물)
1 The Da Vinci Code Dan Brown 2003 mistery-detective 20 3
2 Think and Grow Rich Napoleon Hill 1937 non-fiction 50 5
3 Harry Potter and the Half-Blood Prince J. K. Rowling 2005 fantasy 92 8
4 The Catcher in the Rye J. D. Salinger 1951 novel 100 6
하는 배열에게 정보를 구조체하는 올바른 방법의 f.e. Books.txt 파일에서 읽는 방법을 찾을 수 없습니다 Book_list [0] .id는 1, Book_list [0] .name = Da Vinci 코드 등이어야합니다. 아이디어는 각 정보 평화가 탭으로 구분된다는 것입니다. 나는 충분히 명확하지 않은 것을 묻는다면 미리 미안하다.
#include <iostream>
#include <string>
#include <fstream>
struct Book
{
int id;
std::string name;
std::string author;
int year;
std::string genre;
int times_given;
int available;
};
int main()
{
int book_list_size = 500;
int number_of_books = 0;
Book book_list[book_list_size];
std::ifstream book_input_stream("Books.txt");
std::string line;
while (std::getline(book_input_stream, line))
{
++number_of_books;
}
book_input_stream.close();
book_input_stream.open("Books.txt");
for (int i = 0; i < number_of_books; i++)
{
book_input_stream >> book_list[i].id >> book_list[i].name
>> book_list[i].author >> book_list[i].year
>> book_list[i].genre >> book_list[i].times_given
>> book_list[i].available;
}
for (int i = 0; i < number_of_books; i++)
{
std::cout << book_list[i].id << " " << book_list[i].name << " "
<< book_list[i].author << " " << book_list[i].year << " "
<< book_list[i].genre << " " << book_list[i].times_given << " "
<< book_list[i].available << std::endl;
}
book_input_stream.close();
return 0;
}
특히이 답변을 확인하십시오 : http://stackoverflow.com/a/23070803/1413395 –
이와 같은 파일이 제공 되었습니까, 아니면 파일을 만드시겠습니까? – aceBox
텍스트 파일을 변경할 수 있다면 구분 기호를 사용하는 것이 가장 좋습니다 ...이 경우 다양한 필드를 구별 할 수 있습니다. – aceBox