C++ FileIO 以读取和写入对象变量
C++ FileIO to read and write into objects variables
我正在尝试创建一个小型图书馆系统,用户可以在其中添加新的书籍详细信息(名称,作者和价格(。当实现 FileIO 系统以使用 getline 函数从文件中读取每本书的详细信息时,当我尝试将它们存储在临时变量中时,分离我的变量变得更加困难。
前任:
"不是一个四个字母的单词,克里斯·杰里科,17.67,">
"哈利波特,JK罗琳,23.98,">
PS:有没有比加逗号更好的解决方案?
我试图添加一个","字符来分隔我的每个字符串,但我需要一个更好、更有效的解决方案来与 getLine 函数配合使用。
int main(){
vector<Book> library;
//----Loading File Data_START
string line;
int nbArg=0;
string tempName, tempAuthor, tempPrice;
ifstream myfileL("List.txt");
if (myfileL.is_open())
{
while (getline(myfileL, line))
{
tempPrice=tempAuthor = tempName = "";
for (int j = 0; j < line.size(); j++){
if (line.at(j) == ','){
nbArg++;
}
else{
switch (nbArg){
case 0:
tempName += (line.at(j));
break;
case 1:
tempPrice += (line.at(j));
break;
case 2:
tempAuthor += (line.at(j));
break;
}
}
}
cout << tempName << endl << tempAuthor << endl << tempPrice << endl;
cout << "End of Line"<< endl;
nbArg = 0;
}
cout << "---------------------------" << endl;
myfileL.close();
}
else cout << "Unable to open file";
//----Loading File Data_END
char inputKey = 's';
cout << "-----------------WELCOME----------------" << endl;
while (inputKey != 'q')
{
cout << "---------------------------------------" << endl;
cout << "Click "1" to add a book to your library" << endl;
cout << "Click "2" to show how the number of books your possess" << endl;
cout << "Click "3" to show details about your books" << endl;
cout << "Click "q" to quit" << endl;
cin >> inputKey;
switch (inputKey)
{
case '1':
addElem(library);
break;
case '2':
cout << "You now own " << libSize(library) << " books !" << endl;
break;
case '3':
showDetails(library);
break;
case 'q':
cout << "GOODBYE!" << endl;
Sleep(2000);
break;
}
}
return 0;
}
使用可以包含结构的专用文件格式(例如 json、xml(。这将防止很多很多问题。
如果不能,请添加一个分隔符(就像您所做的那样(,但选择一个在实际字符串中出现的可能性最小的分隔符。例如行尾(每个变量都会在它自己的行上(或 \0 或 \t。
这是一个平稳加载库的解决方案。您可以让 BookList.txt 文件在名称、作者和价格之间用TAB
s t
分隔,而不是逗号,
,然后使用 getline()
用TAB
分隔,如下例所示。
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
const char TAB = 't';
struct StructBook {
string Name, Author;
double Price;
};
vector<StructBook> Library;
void GetBookDetails(string LineBookDetails, StructBook & Book) {
string string_Price;
stringstream StringStreamBookDetails(LineBookDetails);
getline(StringStreamBookDetails, Book.Name, TAB);
getline(StringStreamBookDetails, Book.Author, TAB);
getline(StringStreamBookDetails, string_Price, TAB);
stringstream(string_Price) >> Book.Price;
}
bool LoadLibrary() {
ifstream FileBookList("BookList.txt");
if(FileBookList.is_open()) {
string LineBookDetails;
StructBook Book;
while(getline(FileBookList, LineBookDetails)) {
GetBookDetails(LineBookDetails, Book);
Library.push_back(Book);
cout << Book.Name << ", " << Book.Author << ", " << Book.Price << endl;
}
FileBookList.close();
return true;
} else {
return false;
}
}
int main(){
if(!LoadLibrary())
cout << "Error: Unable to load library";
//Rest of your program goes here
return 0;
}
您的BookList.txt
应如下所示:
No Is a four letter word Chris Jericho 17.67
Harry Potter JK Rowling 23.98
相关文章:
- 你能重载对象变量名本身返回的内容吗
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- 如何使用std::lower_bound比较对象变量,而不使用第二个对象进行比较
- 将私有对象变量与用户输入的变量进行比较
- 具有已定义操作重载的 C++ 非类型化值对象/变量库
- 如何对对象数组进行排序,而不考虑对象变量类型
- 在功能中设置对象变量
- 如何创建对象变量,以便每次分配新的指针
- C++ FileIO 以读取和写入对象变量
- 私下声明的对象变量的突变器/访问器方法
- C++ 列表中的对象变量在迭代过程中不会更新
- 稍后使用私有对象变量时如何初始化数组(初始化对象后)
- 通过向量更改对象变量的值
- C 全局对象变量内存释放
- 基本数据类型变量 VS 对象变量
- C 复制分配运算符,用于参考对象变量
- c++从列表中移除元素,并将其分配给对象变量
- 我们是否需要在C++中同步局部对象变量
- 点运算符和箭头运算符之间的区别 结构对象变量 在 C 或 C++ 中创建树
- 无法访问在 std 回调中传递的对象变量