I am trying to write a line reader to populate a string array which is private member of the same class. I want the loader function called by constructor to dynamically resize member array and populate it. This didn't worked. Then I managed to populate a local array in the loader function. But I couldn't copy these values to private member of the class.
I think there must be a way of copying values from local "ReadLines" array to class private member "Lines" array.
I have already read how vector class implemented internally. But I still think dynamically populating a string array must be achievable by some other simple way, similar to which I used to resize local array in Read() function.
I searched the net, but couldn't find any answer without standart or self implemented vector classes. Has old methods before vectors (if any) completely forgotten? Is vector class that magical?
Isn't there any other way than vectors?
linereader.h :
class LineReader
{
public:
LineReader();
void Read();
private:
string Lines[];
int LineCount;
};
linereader.cpp :
#include <string>
#include <iostream>
using namespace std;
#include <fstream>
#include "linereader.h"
LineReader::LineReader()
{
Read();
cout << "Line Count : " << LineCount << endl;
cout << "Lines Size : " << sizeof(Lines) << endl;
cout << "Lines 0 : ";
cout << Lines[0] << endl; //Gives segmantation fault
}
void LineReader::Read()
{
std::ifstream infile("lines.txt");
string *ReadLines = new string[1];
string line;
int linenumber = 0;
while (infile >> line)
{
cout << endl << linenumber << " :: " << line << " ";
string* temp_Lines = new string[linenumber + 1];
for(int i = 0; i < linenumber; i++){
cout << i << ",";
temp_Lines[i] = ReadLines[i];
}
cout << "[" << linenumber << "]";
delete [] ReadLines;
ReadLines = temp_Lines;
ReadLines[linenumber] = line;
linenumber++;
}
infile.close();
cout << endl << "----------------------------------" << endl;
cout << "ReadLines Count : " << linenumber << endl;
LineCount = linenumber;
for(int i = 0; i < linenumber; i++){
cout << "ReadLines "<< i + 1 << " " << ReadLines[i] << endl;
}
/////////////////////////////////////
// HERE IS THE PROBLEM //
// how to copy ReadLines to Lines? //
/////////////////////////////////////
//string *Lines = new string[linenumber + 1]; // FLOODING TERMINAL WITH EMPTY LINES
// Lines = ReadLines; // not working
// Lines = *ReadLines; // error: cannot convert
// Lines = **ReadLines; // error: no match for ‘operator*’
// *Lines = ReadLines; // error: invalid conversion from
// *Lines = *ReadLines; // FLOODING TERMINAL WHEN RUN
// *Lines = **ReadLines; // error: no match for ‘operator*’
// **Lines = ReadLines; // error: no match for ‘operator*’
// **Lines = *ReadLines; // error: no match for ‘operator*
// **Lines = **ReadLines; // error: no match for ‘operator*
}
int main(int argc, char* argv[])
{
LineReader linereader;
return 0;
}
lines.txt :
AAA
BBB
CCC
DDD
EEE
FFF
GGG
compilation :
g++ linereader.cpp -o linereader
OUTPUT :
0 :: AAA [0]
1 :: BBB 0,[1]
2 :: CCC 0,1,[2]
3 :: DDD 0,1,2,[3]
4 :: EEE 0,1,2,3,[4]
5 :: FFF 0,1,2,3,4,[5]
6 :: GGG 0,1,2,3,4,5,[6]
----------------------------------
ReadLines Count : 7
ReadLines 1 AAA
ReadLines 2 BBB
ReadLines 3 CCC
ReadLines 4 DDD
ReadLines 5 EEE
ReadLines 6 FFF
ReadLines 7 GGG
Line Count : 7
Lines Size : 0
Segmentation fault