void loadData() {//读取数据:图书 ifstream inFile("data.txt"); string line; while (getline(inFile, line)) { string name, author, publisher; int pages, stock; double price; int year, month, day; sscanf(line.c_str(), "%[^,],%[^,],%[^,],%d,%d-%d-%d,%d,%lf", &name[0], &author[0], &publisher[0], &pages, &year, &month, &day, &stock, &price); Time latestTime(year, month, day); Book b(name, author, publisher, pages, latestTime, stock, price); books.push_back(b); } inFile.close(); } string类型的name中应该怎么修改
时间: 2024-01-17 16:04:21 浏览: 56
file-read.rar_c++ 读取txt数据
在使用sscanf函数时,可以将name参数改为一个字符数组,然后将读取到的name字符串拷贝到该字符数组中,例如:
```
char name[100];
sscanf(line.c_str(), "%[^,],%[^,],%[^,],%d,%d-%d-%d,%d,%lf", name, &author[0], &publisher[0], &pages, &year, &month, &day, &stock, &price);
Book b(name, author, publisher, pages, latestTime, stock, price);
```
这样就可以正确读取数据并创建Book对象了。
阅读全文