1

まず、私が求めていることは可能ですか?

私は現在、.txt ファイルから文字列を 1 つのメンバー メソッドでローカルに読み取り、同じデータをコンソールに出力する 1 つのメンバー メソッド (Read) を持つプログラムを持っています。現状では、配列の内容に対して何もできませんが、Write メソッドやコード内の他の場所で使用するデータが必要です。私は動的な C スタイルの配列を使用しました。これは私に必要なものであり、ベクトルなどは許可されていないためです。解決策または一般的な方向性は素晴らしいでしょう!

私の構造は、1 つの 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


};

メイン.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);
}
4

1 に答える 1

0

AnatolyS が言ったように、arrayOfWords をクラスのプライベート メンバー変数にする必要があります。

次に、メモリ リークやアクセス違反エラーがないことを確認するために、次のことをお勧めします。

  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 でないことを確認してください。

もう 1 つの方法は、"new" 演算子が NULL ポインターを返さないことを確認することです (これは、メモリ不足の場合に発生する可能性があります)。

この例は、vector のようなクラスがこれらの割り当て/解放の詳細の多くを処理するため、非常に便利である理由を示しています。

于 2013-05-16T20:13:50.813 に答える