C++ 需要帮助将.txt文件中的数据读取到数组C++; #包括 #包括 #包括 struct//声明结构类型 { 国际标准书号; 字符串标题; 字符串作者; 字符串发布器; 整数; 双倍价格; }; //常数 const int MAX_SIZE=100; //阵列 教科书库存[最大数量]; void readInventory() { //打开库存文件 ifstream inFile(“inventory.txt”); //检查错误 if(infle.fail()) { cerr库存[pos].ISBN >>库存[pos]。标题 >>库存[pos]。作者 >>库存[pos]。发布者 >>库存[pos]。数量 >>存货[pos]。价格 ) { pos++; } //关闭文件 infle.close(); 回来 }
你好, 我需要这个函数的帮助。这个函数的目标是从一个txt文件中读入信息,并将其读入一个数组结构中。 文本文件本身已按循环的正确顺序设置 我的问题是,对于title部分,一本书的标题可能是多个单词,例如“我的第一本书”。我知道我必须使用getline将该行作为字符串,以将其输入到“title”数据类型中C++ 需要帮助将.txt文件中的数据读取到数组C++; #包括 #包括 #包括 struct//声明结构类型 { 国际标准书号; 字符串标题; 字符串作者; 字符串发布器; 整数; 双倍价格; }; //常数 const int MAX_SIZE=100; //阵列 教科书库存[最大数量]; void readInventory() { //打开库存文件 ifstream inFile(“inventory.txt”); //检查错误 if(infle.fail()) { cerr库存[pos].ISBN >>库存[pos]。标题 >>库存[pos]。作者 >>库存[pos]。发布者 >>库存[pos]。数量 >>存货[pos]。价格 ) { pos++; } //关闭文件 infle.close(); 回来 },c++,arrays,loops,struct,fstream,C++,Arrays,Loops,Struct,Fstream,你好, 我需要这个函数的帮助。这个函数的目标是从一个txt文件中读入信息,并将其读入一个数组结构中。 文本文件本身已按循环的正确顺序设置 我的问题是,对于title部分,一本书的标题可能是多个单词,例如“我的第一本书”。我知道我必须使用getline将该行作为字符串,以将其输入到“title”数据类型中 我还缺少一个infle.ignore(),但我不知道如何将其放入循环中。假定输入格式为: #include <iostream> #include <fstream> #
我还缺少一个infle.ignore(),但我不知道如何将其放入循环中。假定输入格式为:
#include <iostream>
#include <fstream>
#include <string>
struct textbook //Declare struct type
{
int ISBN;
string title;
string author;
string publisher;
int quantity;
double price;
};
// Constants
const int MAX_SIZE = 100;
// Arrays
textbook inventory[MAX_SIZE];
void readInventory()
{
// Open inventory file
ifstream inFile("inventory.txt");
// Check for error
if (inFile.fail())
{
cerr << "Error opening file" << endl;
exit(1);
}
// Loop that reads contents of file into the inventory array.
pos = 0; //position in the array
while (
inFile >> inventory[pos].ISBN
>> inventory[pos].title
>> inventory[pos].author
>> inventory[pos].publisher
>> inventory[pos].quantity
>> inventory[pos].price
)
{
pos++;
}
// Close file
inFile.close();
return;
}
i、 例如,数据成员是行空格分隔的,您可以为您的结构教科书定义操作符>
,它可能看起来像:
ISBN title author publisher price quantity price
然后,要读取文件,只需执行以下操作:
std::istream& operator>> (std::istream& is, textbook& t)
{
if (!is) // check input stream status
{
return is;
}
int ISBN;
std::string title;
std::string author;
std::string publisher;
int quantity;
double price;
// simple format check
if (is >> ISBN >> title >> author >> publisher >> quantity >> price)
{
// assuming you additionally define a (assignment) constructor
t = textbook(ISBN, title, author, publisher, quantity, price);
return is;
}
return is;
}
std::ifstream-ifs(…);
矢量书;
教科书b;
while(ifs>>b)
{
书。推回(b);
}
关于getline()
的使用,请参见此
我们可以看到一个示例txt文件吗?标题与作者之间的区别是什么?没有文件样本或猜测就无法回答。猜测:您可以通过读取区块和&&
操作符:if(infle>>inventory[pos].ISBN&&infle.ignore()&&std::getline(infle,inventory[pos].title)&&&&……)
但最好用getline
将所有内容读作string
s,然后根据需要将string
s转换为数字。inventory.txt文件:20451我的第一本书Mark Lusk Pearson Publishing 40 45.34 9780316布朗家族梅森维克多·小布朗36 105.99 1349877我的生活故事诺拉·M·琼斯CreateSpace独立出版平台20 18如果您对文件格式的假设是正确的,那么这项任务将非常困难,因为很少有作者和书籍有一个单字名称。解决这一问题远远超出了任何人提出这样一个问题的能力。如果您深入阅读该问题,OP将谈论了解他们需要使用getline。这表明有一个分隔符,OP没有提供文件示例,这可用于getline
。创建使用操作符>
的建议非常正确,并且避免了投票否决。我感谢您的反馈。这是编程的一部分学校作业非常“不干涉”,所以我可能采取了错误的方法。我认为我需要使用getline的原因是因为库存文件上的作者和标题行是一个完整的短语,所以我需要将整个字符串放入数组中。我们还得到了一个提示,不要忘记使用。ignore()为了从Buff..c.TimMelman中删除\n,我明白了你的意思。看看<代码> STD::vector < /COD>而不是数组,就像你使用C++一样,而不是C。
,它可以包含在一个实现中,但您应该记住,有多种方法可以做您想做的事情,即使不使用它,我的答案就是一个例子。您的问题是否需要进一步的澄清/帮助?
std::ifstream ifs(...);
std::vector<textbook> books;
textbook b;
while (ifs >> b)
{
books.push_back(b);
}