将.txt文件中的字符串输出到成员方法局部作用域之外的动态数组
C++ Outputting strings from a .txt file in to a dynamic array outside of a member methods local scope
首先,我问的是可能的吗?
我目前有一个具有一个成员方法(Read)的程序,该程序在一个成员方法中从本地的.txt文件中读取字符串,并将相同的数据输出到控制台。现在,我不能对数组的内容做任何事情,但是我需要在Write方法和代码中的其他任何地方使用数据。我使用了一个动态的C风格数组,因为这是我所需要的,不允许向量等。一个解决方案或任何一般的方向将是非常棒的!
我的结构由一个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成为类的私有成员变量。
然后确保没有内存泄漏或访问冲突错误,我建议如下:
-
在构造函数中初始化为NULL
void ArrayStorage::ArrayStorage() : arrayOfWords(NULL) {}
-
在析构函数中删除它,如果它不是NULL
void ArrayStorage::~ArrayStorage() { if (arrayOfWords) delete []arrayOfWords; }
-
当读取一个新文件时,如果使用同一个类读取多个文件(假设这是你想要的行为),请删除旧数组。
if (arrayOfWords) delete []arrayOfWords; arrayOfWords = new string[arrayLength];
-
在你的"写"函数(或任何其他函数访问数组),检查它不是NULL之前,试图使用它。
你可以做的另一件事是检查"new"操作符是否返回NULL指针(如果内存不足可能会发生这种情况)。
相关文章:
- 为什么我的数组双精度函数不起作用?
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- 为什么我的 if else 语句不起作用并从数组中输出正确的索引?
- 为什么使用数组元素查找最大数字的程序不起作用?
- C++ - 块不移动,我的数组不起作用
- 我正在尝试解决一个需要数组总和值但代码不起作用的问题,我想做这样的事情
- 如何从类函数返回指向类作用域数组的指针?
- SDL-使用SDL_TEXTURE的std ::向量不起作用,数组正常工作
- 错误:数组大小表达式必须具有整型或无作用域枚举类型,而不是'double'
- C++数组类定义和实现,未在作用域中声明
- GNU 编译器与 Visual Studio 在分配的数组上分配了长度常量,在作用域中
- 用于移动不起作用的数组元素的函数
- C/ c++中数组的参数传递和作用域
- 可能的c++ 2位位域数组
- 将.txt文件中的字符串输出到成员方法局部作用域之外的动态数组
- 选择要在函数中作用的数组,而不必为每个数组的副本指定函数的版本
- 为结构的数组创建指针作用域和构造函数
- C++数组作用域
- 2bit位域数组对性能和缓存效率的影响
- c++对象正在创建同一数组的两个实例,但作用域不同