如何打印我从文件中读取的结构中的数据
How to print data in struct that I've read from a file
我正在编写一个程序来跟踪书店库存(起始代码)。该程序的功能之一是它可以以或多或少的格式化输出打印出整个库存。我已经得到它来打印出没有空格或空格的内容。
我将文件读入初始化为结构的数组:
struct Book //declare struct
{
int ISBN; //ISBN within struct
std::string Title; //Title within struct
std::string Author; //Author within struct
std::string Publisher; // Publisher within struct
int Quantity; //Quantity within struct
double Price; //price within struct
};
.txt 文件的内容被读入每个内存分配作为它们的特定数据类型,然后每个都被打印出来。
/* READ CONTENTS OF INVENTORY*/
string readInventory(ifstream &readFile) //define readInventory, pass it a refrence to an infile
{
const int ARRAY_size = 100;
string_book read_out_inventory[ARRAY_size];
ifstream inFile;
string stub = "\n\n***** COMPLETED: files read ******\n\n";
string buffer = " ";
inFile.open("inventory.txt");
if (!inFile.eof())
{
for (int i = 0; i < ARRAY_size; ++i)
{
inFile >> read_out_inventory[i].ISBN;
inFile >> read_out_inventory[i].Title;
inFile >> read_out_inventory[i].Author;
inFile >> read_out_inventory[i].Publisher;
inFile >> read_out_inventory[i].Quantity;
inFile >> read_out_inventory[i].Price;
}
}
cout << setw(43) <<"The books are: \n\n" ;
for (int ARRAY_read = 0; ARRAY_read < ARRAY_size; ARRAY_read++)
{
cout << &read_out_inventory[ARRAY_read].ISBN << endl;
cout << &read_out_inventory[ARRAY_read].Title << endl;
cout << &read_out_inventory[ARRAY_read].Author << endl;
cout << &read_out_inventory[ARRAY_read].Publisher << endl;
cout << &read_out_inventory[ARRAY_read].Quantity << endl;
cout << &read_out_inventory[ARRAY_read].Price << endl;
}
return stub;
}
我 运行 遇到的问题(在其他明显的问题中)是输出是:
0x7fff5fbfe250
0x7fff5fbfe268
0x7fff5fbfe280
0x7fff5fbfe288
0x7fff5fbfe290
0x7fff5fbfe298
0x7fff5fbfe2b0
0x7fff5fbfe2c8
0x7fff5fbfe2e0
0x7fff5fbfe2e8
0x7fff5fbfe2f0
0x7fff5fbfe2f8
0x7fff5fbfe310
0x7fff5fbfe328
0x7fff5fbfe340
0x7fff5fbfe348
0x7fff5fbfe350
0x7fff5fbfe358
0x7fff5fbfe370
0x7fff5fbfe388
0x7fff5fbfe3a0
0x7fff5fbfe3a8
0x7fff5fbfe3b0
0x7fff5fbfe3b8
0x7fff5fbfe3d0
0x7fff5fbfe3e8
0x7fff5fbfe400
0x7fff5fbfe408
0x7fff5fbfe410
...
我什至不完全确定我的问题是什么,所以非常感谢帮助。我看到这个问题在另一个地方已经回答了,但我没有完全理解。
.txt 文件如下所示:
20451
My First Book
Mark Lusk
Pearson Publishing
40
45.34
9780316
Brown Family
Mason Victor
Little Brown
36
105.99
1349877
Story of My Life
Norah M Jones
CreateSpace Independent Publishing Platform
20
18
1234567
The Big Book
Mypals Pennyweather
GreenThumb
4
13.23
当你写下这样一行代码时:
cout << &read_out_inventory[ARRAY_read].ISBN << endl;
您正在做的是以下内容:您要求获取结构的 ISBN
成员的地址(运算符 &)并将其打印出来。所以你在输出中看到的是所有这些地址。
如果您只想打印值(而不是地址)- 不要获取地址并输出您想要的内容 "directly",像这样:
cout << read_out_inventory[ARRAY_read].ISBN << endl;
你的代码和文件中有很多错误
- While printing, you are printing address of every index using & operator.
- You are using Array_size while printing the whole array, however your file has only 2-3 books.
- There are white spaces in your text file and you are reading out of bound number of books.
- Here is the modified code I wrote for you, that works for me :)
string readInventory() //define readInventory, pass it a refrence to an infile
{
const int ARRAY_size = 100;
int countBooksInFile=0;
Book inventory[ARRAY_size];
ifstream inFile;
string stub = "\n\n***** COMPLETED: files read ******\n\n";
inFile.open("inventory.txt");
if (!inFile.eof())
{
inFile >> ws;
inFile >> inventory[countBooksInFile].ISBN;
inFile >> ws;
getline(inFile, inventory[countBooksInFile].Title);
inFile >> ws;
getline(inFile ,inventory[countBooksInFile].Author);
inFile >> ws;
getline(inFile, inventory[countBooksInFile].Publisher);
inFile >> ws;
inFile >> inventory[countBooksInFile].Quantity;
inFile >> ws;
inFile >> inventory[countBooksInFile].Price;
countBooksInFile++;
}
cout << "Total Books: " << countBooksInFile<<endl;
cout << "The books are: \n";
for (int ARRAY_read = 0; ARRAY_read < countBooksInFile; ARRAY_read++)
{
cout << inventory[ARRAY_read].ISBN << endl;
cout << inventory[ARRAY_read].Title << endl;
cout << inventory[ARRAY_read].Author << endl;
cout << inventory[ARRAY_read].Publisher << endl;
cout << inventory[ARRAY_read].Quantity << endl;
cout << inventory[ARRAY_read].Price << endl;
}
return stub;
}
我正在编写一个程序来跟踪书店库存(起始代码)。该程序的功能之一是它可以以或多或少的格式化输出打印出整个库存。我已经得到它来打印出没有空格或空格的内容。
我将文件读入初始化为结构的数组:
struct Book //declare struct
{
int ISBN; //ISBN within struct
std::string Title; //Title within struct
std::string Author; //Author within struct
std::string Publisher; // Publisher within struct
int Quantity; //Quantity within struct
double Price; //price within struct
};
.txt 文件的内容被读入每个内存分配作为它们的特定数据类型,然后每个都被打印出来。
/* READ CONTENTS OF INVENTORY*/
string readInventory(ifstream &readFile) //define readInventory, pass it a refrence to an infile
{
const int ARRAY_size = 100;
string_book read_out_inventory[ARRAY_size];
ifstream inFile;
string stub = "\n\n***** COMPLETED: files read ******\n\n";
string buffer = " ";
inFile.open("inventory.txt");
if (!inFile.eof())
{
for (int i = 0; i < ARRAY_size; ++i)
{
inFile >> read_out_inventory[i].ISBN;
inFile >> read_out_inventory[i].Title;
inFile >> read_out_inventory[i].Author;
inFile >> read_out_inventory[i].Publisher;
inFile >> read_out_inventory[i].Quantity;
inFile >> read_out_inventory[i].Price;
}
}
cout << setw(43) <<"The books are: \n\n" ;
for (int ARRAY_read = 0; ARRAY_read < ARRAY_size; ARRAY_read++)
{
cout << &read_out_inventory[ARRAY_read].ISBN << endl;
cout << &read_out_inventory[ARRAY_read].Title << endl;
cout << &read_out_inventory[ARRAY_read].Author << endl;
cout << &read_out_inventory[ARRAY_read].Publisher << endl;
cout << &read_out_inventory[ARRAY_read].Quantity << endl;
cout << &read_out_inventory[ARRAY_read].Price << endl;
}
return stub;
}
我 运行 遇到的问题(在其他明显的问题中)是输出是:
0x7fff5fbfe250
0x7fff5fbfe268
0x7fff5fbfe280
0x7fff5fbfe288
0x7fff5fbfe290
0x7fff5fbfe298
0x7fff5fbfe2b0
0x7fff5fbfe2c8
0x7fff5fbfe2e0
0x7fff5fbfe2e8
0x7fff5fbfe2f0
0x7fff5fbfe2f8
0x7fff5fbfe310
0x7fff5fbfe328
0x7fff5fbfe340
0x7fff5fbfe348
0x7fff5fbfe350
0x7fff5fbfe358
0x7fff5fbfe370
0x7fff5fbfe388
0x7fff5fbfe3a0
0x7fff5fbfe3a8
0x7fff5fbfe3b0
0x7fff5fbfe3b8
0x7fff5fbfe3d0
0x7fff5fbfe3e8
0x7fff5fbfe400
0x7fff5fbfe408
0x7fff5fbfe410
...
我什至不完全确定我的问题是什么,所以非常感谢帮助。我看到这个问题在另一个地方已经回答了,但我没有完全理解。
.txt 文件如下所示:
20451
My First Book
Mark Lusk
Pearson Publishing
40
45.34
9780316
Brown Family
Mason Victor
Little Brown
36
105.99
1349877
Story of My Life
Norah M Jones
CreateSpace Independent Publishing Platform
20
18
1234567
The Big Book
Mypals Pennyweather
GreenThumb
4
13.23
当你写下这样一行代码时:
cout << &read_out_inventory[ARRAY_read].ISBN << endl;
您正在做的是以下内容:您要求获取结构的 ISBN
成员的地址(运算符 &)并将其打印出来。所以你在输出中看到的是所有这些地址。
如果您只想打印值(而不是地址)- 不要获取地址并输出您想要的内容 "directly",像这样:
cout << read_out_inventory[ARRAY_read].ISBN << endl;
你的代码和文件中有很多错误
- While printing, you are printing address of every index using & operator.
- You are using Array_size while printing the whole array, however your file has only 2-3 books.
- There are white spaces in your text file and you are reading out of bound number of books.
- Here is the modified code I wrote for you, that works for me :)
string readInventory() //define readInventory, pass it a refrence to an infile
{
const int ARRAY_size = 100;
int countBooksInFile=0;
Book inventory[ARRAY_size];
ifstream inFile;
string stub = "\n\n***** COMPLETED: files read ******\n\n";
inFile.open("inventory.txt");
if (!inFile.eof())
{
inFile >> ws;
inFile >> inventory[countBooksInFile].ISBN;
inFile >> ws;
getline(inFile, inventory[countBooksInFile].Title);
inFile >> ws;
getline(inFile ,inventory[countBooksInFile].Author);
inFile >> ws;
getline(inFile, inventory[countBooksInFile].Publisher);
inFile >> ws;
inFile >> inventory[countBooksInFile].Quantity;
inFile >> ws;
inFile >> inventory[countBooksInFile].Price;
countBooksInFile++;
}
cout << "Total Books: " << countBooksInFile<<endl;
cout << "The books are: \n";
for (int ARRAY_read = 0; ARRAY_read < countBooksInFile; ARRAY_read++)
{
cout << inventory[ARRAY_read].ISBN << endl;
cout << inventory[ARRAY_read].Title << endl;
cout << inventory[ARRAY_read].Author << endl;
cout << inventory[ARRAY_read].Publisher << endl;
cout << inventory[ARRAY_read].Quantity << endl;
cout << inventory[ARRAY_read].Price << endl;
}
return stub;
}