问题描述
首先,我对c ++很新,所以请耐心等待。我正在尝试
来创建一个保存项目清单的程序。我想用
使用结构来存储产品名称,购买价格,卖价和
a应税标志(一个Y / N字符)然后写这个全部一个文件
(最好只是一个普通的旧文本文件),然后在以后阅读,所以
我可以保持一个正在运行的库存。我运行
的问题是当我写入文件时似乎保存它但是当我读它时
回来它似乎没有给我任何东西。从我到目前为止你能够弄清楚到目前为止,你不能只将一个字符串保存到一个文件中你需要以某种方式操纵它但是非常新的(并没有那么好)
$ c $ b在c ++上我无法找到一种实用的方法。
有没有简单的方法来处理这个或者我可能会更好地尝试
除了结构之外的其他方式吗?
这是我到目前为止所做的,试图将代码限制为什么这个问题与
有关,所以它可能会更有意义。如果需要更多
将很乐意发布。
************************ *************
struct product
{
int stockNum;
字符串名称;
浮动费用;
浮动价格;
char税;
};
产品库存[25];
ofstream st(" c:/stock.txt");
ifstream stI(" c:/stock.txt");
stI.read((char *)& inventory,sizeof(inventory));
>
if(stockChoice == 1)
{
stockChoice = 0;
inventory [counter] .stockNum = counter;
cout<< 产品编号: <<库存[柜台] .stockNum;
cout<< \ n输入产品名称:" ;;
cin> inventory [counter] .name;
cout<< \ n输入产品购买成本:" ;;
cin> inventory [counter] .cost;
cout<< \ n输入产品购买价格:" ;;
cin> inventory [counter] .price;
cout<< \ nIs产品应税(Y / N):" ;;
cin> inventory [counter] .tax;
cout<< " \ n\\\
产品输入:" <<库存[柜台] .stockNum<<
库存[柜台] .name<< " " <<库存[柜台] .cost<< " " <<
库存[柜台] .price<< " " <<库存[柜台] .tax;
柜台++;
}
st.write((char *) &库存,sizeof(库存));
******************************** *
谢谢!
至少要使''double''类型的''cost''和'''''。
不要使用全局变量。
并且不要使用原始数组。
使用std :: vector,并使用它的''at''函数而不是[] -indexing(它'
更安全,因为它检测到范围错误)。
作为一个新手,不要/曾经/使用演员阵容,尤其不是C风格演员阵容。
这意味着对于各种问题,阅读是一种非常有用的功能
你将要解决的问题,以及你现在正在解决的问题。
对于''read''你应该以二进制模式打开文件,但是不要尝试
:相反,使用一个文本文件,其中包含每个记录的项目拥有
行。使用通常的<<和> i / o的运算符,除了字符串,
,您应该通过''getline''输入。
这似乎是你没有表现出来的循环的一部分。
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet和电子邮件中最烦人的是什么?
[ snip]
至少要打''''和''''''''''''''''''''''''''''''''''''
我还会考虑将''tax''变成''bool''。否则,如果
''tax''被设置为Y或N之外的东西会发生什么?
-
Marcus Kwok
将''invalid''替换为''net''以回复
我会查看boost :: serialization ..
First of all I am very new to c++ so please bear with me. I am trying
to create a program that keeps an inventory of items. I am trying to
use a struct to store a product name, purchase price, sell price, and
a taxable flag (a Y/N char) and then write this all out to a file
(preferably just a plain old text file) and then read it in later so
that I can keep a running inventory. The problem that I am running
into is when I write to the file it seems save it but when I read it
back in it doesn''t seem to give me anything. From what I have been
able to figure out so far you can''t just save a String to a file you
have to manipulate it somehow but being very new (and not that good)
at c++ I can''t figure out a practical way to do it.
Is there any simple way to handle this or might I be better off trying
to do it somehow other than a Struct?
This is what I have so far, tried to limit the code to what relates to
this problem so it might make a little more sense. If more is needed
will be happy to post.
*************************************
struct product
{
int stockNum;
string name;
float cost;
float price;
char tax;
};
product inventory[25];
ofstream st("c:/stock.txt");
ifstream stI("c:/stock.txt");
stI.read((char *)&inventory, sizeof(inventory));
if (stockChoice == 1)
{
stockChoice = 0;
inventory[counter].stockNum = counter;
cout << "Product Number: " << inventory[counter].stockNum;
cout << "\nEnter product name: ";
cin >inventory[counter].name;
cout << "\nEnter product purchase cost: ";
cin >inventory[counter].cost;
cout << "\nEnter product purchase price: ";
cin >inventory[counter].price;
cout << "\nIs product taxable (Y/N): ";
cin >inventory[counter].tax;
cout << "\n\nProduct Entered: " << inventory[counter].stockNum <<
inventory[counter].name << " " << inventory[counter].cost << " " <<
inventory[counter].price << " " << inventory[counter].tax;
counter++;
}
st.write((char *)&inventory, sizeof(inventory));
*********************************
Thank you!
At least make ''cost'' and ''price'' of type ''double''.
Don''t use globals.
And don''t use raw arrays.
Use std::vector, and use its ''at'' function instead of []-indexing (it''s
safer in that it detects range errors).
As a novice, don''t /ever/ use casts, especially not C style casts.
That means that ''read'' is an ungood function for the kinds of problems
you''ll be solving, and the kind of problem you are solving right now.
For ''read'' you should have opened the file in binary mode, but don''t try
that: instead, use a text file, with each item of a record on its own
line. Use the usual << and >operators for i/o, except for the string,
which you should input via ''getline''.
This seems to be part of a loop you haven''t shown.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[snip]
At least make ''cost'' and ''price'' of type ''double''.
I would also consider making ''tax'' a ''bool''. Otherwise, what happens if
''tax'' is set to something besides ''Y'' or ''N''?
--
Marcus Kwok
Replace ''invalid'' with ''net'' to reply
I''d check boost::serialization..
这篇关于将struct输出到文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!