Reading in a file and performing string manipulation In a question I answered I posted the following code:

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;algorithm&gt;
#include &lt;sstream&gt;

int main()
{
    fstream iFile("names.txt", ios::in);

    // This does not work so replacing with code that does
    // iFile &gt;&gt; file;

    // This is not the best way.
    // But the closest to the intention of the original post.

    // Get size of file.
    ifile.seekg(0,std::ios::end);
    std::streampos          length = ifile.tellg();
    ifile.seekg(0,std::ios::beg);

    // Copy file into string.
    std::string       file(length, '\0');
    ifile.read(&amp;buffer[0],length);

    // Original code continues.

    std::istringstream ss(file);
    std::string token;

    std::vector&lt;std::string&gt; names;

    while(std::getline(ss, token, ',')) {
        names.push_back(token);
    }

    for (unsigned int i = 0; i &lt; names.size(); i++) {
         auto it = std::remove_if(names[i].begin(), names[i].end(), [&amp;] (char c) { return c == '"'; });
         names[i] = std::string(names[i].begin(), it);
    }

    for (unsigned int i = 0; i &lt; names.size(); i++) {
        std::cout &lt;&lt; "names["&lt;&lt;i&lt;&lt;"]: " &lt;&lt; names[i] &lt;&lt; std::endl;
    }
}


Would this be inefficient for larger files? Would it be better if I read the file into one string, did all the manipulation on that, and then put it into a vector?
This code looks a little wild. I'd tame it like this:

#include &lt;algorithm&gt; // for std::remove
#include &lt;fstream&gt;   // for std::ifstream
#include &lt;string&gt;    // for std::string and std::getline
#include &lt;utility&gt;   // for std::move
#include &lt;vector&gt;    // for std::vector

std::ifstream infile("thefile.txt");    // we don't really care where you got the name from

std::vector&lt;std::string&gt; names;

for (std::string line; std::getline(infile, line, ','); )
{
    line.erase(std::remove(line.begin(), line.end(), '"'), line.end());
    names.push_back(std::move(line));
}

// done


A few points to note:


Keep it simple. Don't overthink.
Use ready-made algorithms, don't reinvent wheels (std::remove removes values).
Include what you use (std::string, std::getline).
Use algorithms, don't hand-roll your own loops. (erase/remove)
Understand what algorithms can do for you (don't hand-write your own "erase"). Get familiar with the standard library.
Don't leak scopes. The line string is only needed within the loop, and no further.
Efficiency tip: You can move from the line string that you no longer need and save yourself a copy. Also, we process everything in one step; no need to loop repeatedly.


Furthermore, I suggest factoring this logic into a file:

std::vector&lt;std::string&gt; tokenize_file(std::string const &amp; filename, char delimiter)
{
    std::vector&lt;std::string&gt; result;

    // ...

    return result;
}


Usage:

std::vector&lt;std::string&gt; names = tokenize_file("thefile.txt", ',');