Right so I am working on reading a large text file (100K+ Characters) and storing it into a large character array.
I have been brainstorming to find the best possible algorithm to do this. Now I know that file reading is an expensive operation in terms of time complexity.
So far I have come up with this using C++:
//Opening the text file from the path given
char * dataset;
int dataLength;
ifstream in(path);
if (!in) {
cout << "Could not open the file." << endl;
exit(-1);
}
//Calculating the number of characters in the file
in.ignore(numeric_limits<streamsize>::max());
dataLength = in.gcount();
//Clearing since EOF bit is set after ignore method above
in.clear();
in.seekg(0, ifstream::beg);
//Initializing the dataset array and copying the data
dataset = new T[dataLength + 1];
in.read(dataset, dataLength);
dataset[dataLength] = '\0';
I am not reading character by character as that renders a very costly operation. Now further trying to improve it, I thought if there would be a way to not read the file twice.
In my algo, I have to read it first (using .ignore) to count the number of characters so I can make a dynamic character array. And then I have to read it to store it into the character array.
Is there any way that when I read the file the first time, I could store the data temporarily into a stream, initialize an array and then ask the stream to copy data into my data structure (array)