结构成员的文件输入示例?
我有以下结构:
struct productInfo
{
int item;
string details;
double cost;
};
我有一个文件,将输入 10 种不同的产品,每种产品包含一个项目、详细信息和成本。我尝试使用 inFile.getline 输入它,但它不起作用。谁能给我一个如何做到这一点的例子?我将不胜感激。
编辑 该文件包含 10 行,如下所示:
570314,SanDisk Sansa Clip 8 GB MP3 Player Black,55.99
您能否提供一个示例。
编辑 抱歉,大家,我是 C++ 新手,我不太理解这些建议。这是我尝试过的。
void readFile(ifstream & inFile, productInfo products[])
{
inFile.ignore(LINE_LEN,'\n'); // The first line is not needed
for (int index = 0; index < 10; index++)
{
inFile.getline(products[index].item,SIZE,DELIMETER);
inFile.getline(products[index].details,SIZE,DELIMETER);
inFile.getline(products[index].cost,SIZE,DELIMETER);
}
}
I have the following structure:
struct productInfo
{
int item;
string details;
double cost;
};
I have a file that will input 10 different products that each contain an item, details, and cost. I have tried to input it using inFile.getline but it just doesn't work. Can anyone give me an example of how to do this? I would appreciate it.
Edit
The file contains 10 lines that look like this:
570314,SanDisk Sansa Clip 8 GB MP3 Player Black,55.99
Can you provide an example please.
Edit
Sorry guys, I am new to C++ and I don't really understand the suggestions. This is what I have tried.
void readFile(ifstream & inFile, productInfo products[])
{
inFile.ignore(LINE_LEN,'\n'); // The first line is not needed
for (int index = 0; index < 10; index++)
{
inFile.getline(products[index].item,SIZE,DELIMETER);
inFile.getline(products[index].details,SIZE,DELIMETER);
inFile.getline(products[index].cost,SIZE,DELIMETER);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是另一种方法,使用
fstream
读取文件并使用getline()
读取文件中的每一行。由于其他帖子,该行本身的解析被故意省略已经这样做了。在读取每一行并将其解析为产品信息后,应用程序将其存储在向量上,因此可以在内存中访问所有产品。
编辑:我决定尝试解析该行并编写下面的代码。一些 C++ 人员可能不喜欢
strtok()
处理事情的方法,但它确实存在。This is another approach that uses
fstream
to read the file andgetline()
to read each line on the file. The parsing of the line itself was left out on purpose since other posts have already done that.After each line is read and parsed into a productInfo, the application stores it on a vector, so all products could be accessed in memory.
EDIT: I decided to take a shot at parsing the line and wrote the code below. Some C++ folks might not like the
strtok()
method of handling things but there it is.这取决于文件中的内容?如果是文本,您可以在文件输入流上使用重定向运算符:
int i;
输入文件>>我;
如果它是二进制的,您可以将其读入 &your_struct。
It depends on what's in the file? If it's text, you can use the redirect operator on a file input stream:
int i;
infile >> i;
If it's binary, you can just read it in to &your_struct.
你必须
0) 创建一个新的productInfo实例,pinfo;
1) 读取文本(使用 getline)到第一个逗号(','),将该字符串转换为 int,并将其放入 pinfo.item。
2)读取文本到下一个逗号并将其放入pinfo.details中;
3) 读取文本到末尾行,将字符串转换为双精度型,并将其放入 pinfo.cost 中。
然后继续执行此操作,直到到达文件末尾。
You have to
0) Create a new instance of productInfo, pinfo;
1) read text (using getline) to the first comma (','), convert this string to an int, and put it into pinfo.item.
2) read text to the next comma and put it into pinfo.details;
3) read text to the endline, convert the string to a double, and put it into pinfo.cost.
Then just keep doing this until you reach the end of the file.
以下是我使用
getline
的方法。请注意,我使用它一次从输入文件中读取,然后再次将该行截断为“,”。注意:如果输入是,则该程序有错误
Here is how I would use
getline
. Note that I use it once to read from the input file, and then again to chop that line at ",".N.b.: This program is buggy if the input is