まず、私が求めていることは可能ですか?
私は現在、.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);
}