C++ 无法写入文件字符指针
我正在编写一个指向文件的字符指针,但当我这样做时,我的程序总是在名称处崩溃,我无法找出原因C++ 无法写入文件字符指针,c++,C++,我正在编写一个指向文件的字符指针,但当我这样做时,我的程序总是在名称处崩溃,我无法找出原因 #include <iostream> #include <iomanip> #include <string> #include "AmaProduct.h" using namespace std; namespace sict{ AmaProduct::AmaProduct(char file){ fileTag_ = file; } const cha
#include <iostream>
#include <iomanip>
#include <string>
#include "AmaProduct.h"
using namespace std;
namespace sict{
AmaProduct::AmaProduct(char file){
fileTag_ = file;
}
const char* AmaProduct::unit()const{
return unit_;
}
void AmaProduct::unit(const char* value){
for (int i = 0; i != 9; i++){
unit_[i] = value[i];
}
unit_[11] = 0;
}
fstream& AmaProduct::store(fstream& file, bool addNewLine)const{
file.open("file.txt");
if (file.is_open()){
file << fileTag_ << "," << sku() << ",";
file<< name() << ",";//here
file<< price() << "," << taxed() << "," << quantity() << "," << unit_ << "," << qtyNeeded();
if (addNewLine){
file << endl;
}
}
file.close();
return file;
}
如果有人对其他文件感兴趣,我也会上传这些文件,有几种可能性,但是如果库蒂不介意阅读所有的代码。但是你是否正在阅读文件并使用相同的指针“如果有人对其他文件感兴趣,我也会上传”请不要!把你的样品减少到最低限度。使用debugger.added name()逐步检查代码,使其更易于
name
astring
或char*
?确定!在load()中调用name(n):您也能提供这个函数吗?谢谢您的回答,但我也演示了如何分配name(n);
#ifndef SICT_AMAPRODUCT_H__
#define SICT_AMAPRODUCT_H__
#include "Streamable.h"
#include "Product.h"
#include "Date.h"
#include "ErrorMessage.h"
#include "general.h"
#include <iostream>
#include <fstream>
namespace sict{
class AmaProduct : public Product{
private:
char fileTag_;
char unit_[11];
protected:
ErrorMessage err_;
public:
AmaProduct(char file='N');
const char* unit()const;
void unit(const char* value);
fstream& store(fstream& file, bool addNewLine = true)const;
fstream& load(std::fstream& file);
ostream& write(ostream& os, bool linear)const;
istream& read(istream& is);
};
}
const char* Product::name()const{
return name_;
}
char* name_;
void Product::name(char* name){
delete[] name_;
name_= new char[strlen(name)+1];
strcpy(name_,name);
}
file.getline(n, ',');
name(n);