2

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)

Parker Queen
  • 619
  • 2
  • 12
  • 27
  • You can locate the end with `seekg`, without reading anything. – molbdnilo Sep 23 '18 at 19:56
  • @molbdnilo could you elaborate on that? – Fureeish Sep 23 '18 at 20:01
  • @molbdnilo That's probably a wrong way of doing it since I am on Windows & reading in non-binary mode. Refer here: https://stackoverflow.com/questions/22984956/tellg-function-give-wrong-size-of-file – Parker Queen Sep 23 '18 at 20:03
  • With C++17, you can use [`file_size`](https://en.cppreference.com/w/cpp/filesystem/file_size) to get the size of a file. – xskxzr Sep 24 '18 at 05:10

0 Answers0