C++将.txt文件中的字符串输出到成员方法本地范围之外的动态数组

标签 c++ arrays dynamic stream scope

首先,我问的是可能的吗?

我目前有一个带有一个成员方法 (Read) 的程序,该程序在一个成员方法中从本地的 .txt 文件中读取字符串,然后将相同的数据输出到控制台。现在我不能对数组的内容做任何事情,但是我需要数据在 Write 方法和代码中的其他地方使用。我使用了动态 C 样式数组,因为这是我所需要的,不允许 vector 等。解决方案或任何总体方向都很棒!

我的结构由一个 Main.cpp、一个 ArrayStorage.cpp 类定义和一个 ArrayStorage.h 头文件组成。

我的代码如下:

ArrayStorage.cpp

#include <fstream>
#include <iostream>
#include <ostream>
#include <string>
#include "ArrayStorage.h"
using namespace std;


void ArrayStorage::read(ifstream &fin1)
{
    int index = 0;
    int arrayLength = 0;
    string firstWord;

    if(fin1.is_open())
        {
            fin1 >> firstWord;
            fin1 >> arrayLength;

            string* arrayOfWords;
            arrayOfWords = new string[arrayLength];

            while(!fin1.eof())
            {
                fin1 >> arrayOfWords[index];
                cout << arrayOfWords[index];
                cout << "\n";
                index++;
            }

            delete [] arrayOfWords;

            fin1.close();
        }
}

void ArrayStorage::write(ofstream &out1)
{
    //fout.close();
}

ArrayStorage.h

#include <fstream> // Reading/Writing from file requires this standard library.
#include <iostream> // Needed for cin and cout
#include <ostream> // Needed for outputting to output file
#include <string> // Needed for string values
using namespace std;

#pragma once

class ArrayStorage
{
private:



public:

    void read(ifstream &fin1); //reads data from a file
    void write(ofstream &out1); //output data to an output stream(ostream)
    bool exists(); //return true or false depending whether or not a given word exists
    void stdExists(); //^^ use either std::count() or std::find() inside here


};

main.cpp

#include <fstream>
#include <iostream>
using namespace std;

#include "ArrayStorage.h"
#include "LinkedListStorage.h"


int main(int argc, char **argv) {

string find = "pixel";

ifstream fin1("ACW2_data.txt");
ofstream out1("1-In-SortedRead.txt");

if(!fin1.is_open()) 
{
    cout << "FAIL" << endl;
    return 1;
}

ArrayStorage arrayStorage1;

// read in values into data structure
arrayStorage1.read(fin1);

// output values in data structure to file
arrayStorage1.write(out1);

fin1.close();
out1.close();
return(0);
}

最佳答案

正如 AnatolyS 所说,您应该使 arrayOfWords 成为您类的私有(private)成员变量。

然后为了确保没有内存泄漏或访问冲突错误,我建议如下:

  1. 在构造函数中将其初始化为NULL

    void ArrayStorage::ArrayStorage()
        : arrayOfWords(NULL)
    {}
    
  2. 如果不为NULL则在析构函数中删除

    void ArrayStorage::~ArrayStorage()
    {
        if (arrayOfWords)
            delete []arrayOfWords;
    }
    
  3. 读取新文件时,删除旧数组,以防使用同一个类读取多个文件(假设这是您想要的行为)。

    if (arrayOfWords)
        delete []arrayOfWords;
    arrayOfWords = new string[arrayLength];
    
  4. 在您的“写入”函数(或访问数组的任何其他函数)中,在尝试使用它之前检查它是否不为 NULL。

您可以做的另一件事是检查“new”运算符是否不返回 NULL 指针(如果内存不足可能会发生这种情况)。

这个例子说明了为什么像 vector 这样的类如此有用,因为它们为您处理了很多分配/解除分配的细节。

关于C++将.txt文件中的字符串输出到成员方法本地范围之外的动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16595473/

相关文章:

c# - CreateObject 等效于 C# 4、动态关键字和后期绑定(bind)?

dynamic - 如何在 Jenkins 中动态触发下游构建?

c++ - 在 C++ 中将数组作为函数参数传递

javascript - 创建动态 <div> 并使用编辑 jQuery

c++ - 如何使用指针将分钟转换为小时和分钟

c++ - 禁用类指针递增/递减运算符

c++ - 不明确的类继承

c++ - 数组索引和地址返回相同的值

java - 将数组代码更改为 vector

javascript - 如何在 JavaScript 中构建一个计算数组中出现次数的对象?