ファイルから入力を読み取ることができません。私のコードがstrncpyに達するたびに、私のコードが壊れてしまい、その理由を知ることができません。コードはset name関数で壊れているようです。これは、ここで設定されているファイルからの入力を読み取れません
fstream& AmaProduct::load(std::fstream& file){
char s[7];
char* n;
n = new char[7];
double p;
bool t;
int q;
int nn;
file.open("amaPrd.txt");
if (file.is_open()){
file.ignore(2);
file.getline(s,',');
cout << s;
sku(s);
file.ignore();
file.getline(n,',');
name(n);
file.ignore();
file >> p;
price(p);
file.ignore();
file >> t;
taxed(t);
file.ignore();
file >> q;
file.ignore();
quantity(q);
file.getline(unit_, ',');
file.ignore();
file >> nn;
qtyNeeded(nn);
}
file.close();
return file;
}
:
char sku_[8]
を宣言される
void Product::sku(char* sku){
strncpy(sku_,sku,7);
sku_[7]=0;
}
void Product::price(double price){
price_=price;
}
void Product::name(char* name){
delete[] name_;
name_= new char[strlen(name)+1];
strcpy(name_,name);
}
void Product::taxed(bool tax){
taxed_=tax;
}
void Product::quantity(int q){
quantity_=q;
}
void Product::qtyNeeded(int n){
qtyNeeded_=n;
SKU私は時間のためにこれに取り組んが、解決策を見つけるためには至っていないされています。
コードに特定のエラーメッセージが表示されていますか? –
いいえ、すべてがセグメンテーションフォールトです。 –
'file.getline(n、 '、');'がエラーであることを[既に伝えました](http://stackoverflow.com/a/36668283/4944425)あなたのコードで 'file.getline(n、7、 '、');'を使うべきだということです。ユーザーからの回答をお読みください。 –