Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将文件复制到动态字符串数组_C++_Pointers_Dynamic_Readfile - Fatal编程技术网

C++ 如何将文件复制到动态字符串数组

C++ 如何将文件复制到动态字符串数组,c++,pointers,dynamic,readfile,C++,Pointers,Dynamic,Readfile,我试图将整个输入文件读入字符串。现在我有: bool DynString::readLine(std::istream& in) { if(in.eof()) { *this = DynString(); // Default string value. return(false); } char s[1001]; in.getline(s, 1001); // Delete old string-

我试图将整个输入文件读入字符串。现在我有:

bool DynString::readLine(std::istream& in)
{
    if(in.eof())
    {
        *this = DynString();    // Default string value.
        return(false);
    }

    char s[1001];
    in.getline(s, 1001);

    // Delete old string-value and create new pBuff string with copy of s
    delete [] pBuff;

    pBuff = new char[strlen(s) + 1];
    DynString pBuff(s);

    return(true);
}

bool DynString::readFile(const char filename[])
{
    std::ifstream in(filename);
    if(! in.is_open() )
    {
        *this = DynString();    // Default string value.
        return(false);
    }

    // Delete old string-value and
    // Read the file-contents into a new pBuff string

    delete [] pBuff;

    DynString tempString;
    return(true);
}
其中pBuff是一个称为DynString的动态字符串对象

我想我要做的是创建一个临时的DynString对象并将其用作临时对象,然后使用readLine方法将临时字符串指定给文本文件的一行。完成后,我将删除旧的字符串数组“pBuff”,然后将temp复制到新的pBuff数组中

这是否需要使用concatonate函数,我只需将temp数组中的元素添加到现有的pBuff中


抱歉,如果这有点让人困惑,它在头文件中有其他方法,但包含的方法太多了。

为什么不使用以下更简单的方法,或者您必须使用DynString类

static std::string readFile(const std::string& sFile)
{
  // open file with appropriate flags
  std::ifstream in1(sFile.c_str(), std::ios_base::in | std::ios_base::binary);
  if (in1.is_open())
  {
    // get length of file:
    in1.seekg (0, std::ios::end);
    std::streamoff length = in1.tellg();
    in1.seekg (0, std::ios::beg);
    // Just in case
    assert(length < UINT32_MAX);
    unsigned uiSize = static_cast<unsigned>(length);
    char* szBuffer = new char[uiSize];
    // read data as a block:
    in1.read (szBuffer, length);
    in1.close();

    std::string sFileContent(szBuffer, uiSize);
    delete[] szBuffer;
    return sFileContent;
  }
  else
  {
     // handle error
  }
}
static std::string readFile(const std::string&sFile)
{
//用适当的标志打开文件
std::ifstreamin1(sFile.c_str(),std::ios_base::in | std::ios_base::binary);
if(in1.is_open())
{
//获取文件的长度:
in1.seekg(0,std::ios::end);
std::streamoff length=in1.tellg();
in1.seekg(0,std::ios::beg);
//以防万一
断言(长度
我必须使用DynString类文件的内容是否要存储在pBuff中?也许只提供标题可以帮助我们理解类应该如何工作。