LaTeX table generator - file in argument list My code generates a LaTeX table from a plain text file which uses whitespace as delimiters. It asks you the title of the columns one-by-one (mind the formatting!). Then it outputs a latex.txt file with a nicely formatted table inside.

How can I improve it?

Here's the code:

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

int main(int argc, char* argv[])
{
    if(argc!=2){
        std::cerr &lt;&lt; "Error loading file." &lt;&lt; std::endl;
        return 1;
    }
    int col = 0, row = 0;
    // First of all. Provide an input file split by whitespaces.
    std::ifstream fin(argv[1],std::ios::in);
    std::vector&lt; std::vector&lt;double&gt; &gt; matrix;
    while(!fin.eof()){
        std::vector&lt;double&gt; v;
        std::string line;
        getline(fin, line);
        std::istringstream nums(line);
        double x;
        while(nums &gt;&gt; x){
            v.push_back(x);
        }
        matrix.push_back(v);
        row++;
    }
    // Basic.
    row--;
    col = matrix[0].size();
    std::ofstream fout("latex.txt",std::ios::out);
    fout &lt;&lt; "\\begin{center}\n\\begin{tabular}{";
    for(int i=0;i&lt;col;i++){
        fout &lt;&lt; "|c";
    }
    fout &lt;&lt; "|}\n";
    fout &lt;&lt; "\\hline\n";
    // Ask for col titles:
    for(int i=0;i&lt;col;i++){
        std::cout &lt;&lt; "What is the title of the " &lt;&lt; i+1 &lt;&lt;  ". column?(Please mind the formatting!)" &lt;&lt; std::endl;
        std::string title;
        getline(std::cin,title);
        fout &lt;&lt; title;
        if(i!=col-1){
            fout &lt;&lt; " &amp; ";
        }else{
            fout &lt;&lt; " \\\\\n\\hline\n";
        }
    }
    for(int i=0;i&lt;row;i++){
        for(int j=0;j&lt;col;j++){
            fout &lt;&lt; matrix[i][j];
            if(j!=col-1){
                fout &lt;&lt; " &amp; ";
            }else{
                fout &lt;&lt; " \\\\\n\\hline\n";
            }
        }
    }
    fout &lt;&lt; "\\end{tabular}\n";
    fout &lt;&lt; "\\end{center}";
    return 0;
}
Argument checking
You test that the user has specified exactly one input file, but if not, then print the misleading message

Error loading file.

You would be better with a usage message, such as
std::cerr &lt;&lt; &quot;Usage: &quot; &lt;&lt; argv[0] &lt;&lt; &quot; filename&quot; &lt;&lt; std::endl;

Well done for correctly using cerr for such messages.
File checking
Here, you use the supplied filename, but never check whether fin was successfully opened.  You probably want:
std::ifstream fin(argv[1],std::ios::in);
if (!fin) {
    std::cerr &lt;&lt; argv[1] &lt;&lt; &quot;: not readable&quot; &lt;&lt; std::endl;
    return 1;
}

Similarly for fout:
std::ofstream fout(&quot;latex.txt&quot;, std::ios::out);
if (!fin) {
    std::cerr &lt;&lt; &quot;latex.txt&quot; &lt;&lt; &quot;: not writeable&quot; &lt;&lt; std::endl;
    return 1;
}

The reading loop also doesn't check for errors, and it has the while (!eof()) antipattern.  This should be something more like (untested):
std::string line;
while (getline(fin, line)) {
    std::vector&lt;double&gt; v;
    std::istringstream nums(line);
    double x;
    while (nums &gt;&gt; x) {
        v.push_back(x);
    }
    if (!nums.eof()) {
        std::cerr &lt;&lt; argv[1] &lt;&lt; &quot;: parse error&quot; &lt;&lt; std::endl;
    }
    matrix.push_back(v);
    row++;
}
if (!fin) {
    std::cerr &lt;&lt; argv[1] &lt;&lt; &quot;: reading failed&quot; &lt;&lt; std::endl;
    return 1;
}

At the very end of the program, check the fail bit of fout:
fout &lt;&lt; &quot;\\end{tabular}\n&quot;;
fout &lt;&lt; &quot;\\end{center}&quot;;
return !fout;

Input values checking
If there were no values, you might want to fail before starting to write the output:
const auto rows = matrix.size();
if (rows == 0) {
    std::cerr &lt;&lt; argv[1] &lt;&lt; &quot;: empty&quot; &lt;&lt; std::endl;
    return 1;
}

I'm not sure whether the input needs to be rectangular (i.e. all rows have the same number of columns).  If so, test it:
const auto cols = matrix[0].size();
for (auto i = cols/cols;  i &lt; matrix.size();  ++i) {
    if (matrix[i].size() != cols) {
        std::cerr &lt;&lt; argv[1] &lt;&lt; &quot;:&quot; &lt;&lt; (i+1)
                  &lt;&lt; &quot;: line has &quot; &lt;&lt; matrix[i].size() &lt;&lt; &quot; columns, expected &quot; &lt;&lt; cols &lt;&lt; std::endl;
        return 1;
    }
}

That cols/cols is just a shorter way to write (std::vector&lt;double&gt;::size_type)1.
Overall approach
You probably don't want to be typing column headings each time you update the data file, so you're most likely to redirect stdin from a file (and you may well want to automate with Make or similar).  I'd suggest that you accept two filename arguments - column headings first, one per line; numeric data second.  That way, you can use the number of lines of the first file as the expected column count when reading the data file (and you could error early when reading a non-conforming line).
Reading the column names first gives you another potential advantage: you could transform the input to output line by line, rather than having to store all lines to the end.  This makes the program a much better pipeline player, and is a good practice when the inputs might be very large.
You might consider simply writing the output to standard out, rather than opening latex.txt; that gives you more flexibility in how you use it.  You'd have to not prompt for the headings then, though!
My version
I've only compiled this, not run it.  In particular, I may well have bad logic in reading the double values from the line string, as I don't normally read whole lines and then parse them.  I've made a couple of other changes, such as reducing scope of temporary variables, closing fin as soon as we've finished reading, and sprinkling const fairy-dust where I can.
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;
#include &lt;vector&gt;

int main(int argc, char* argv[])
{
    if (argc!=2) {
        std::cerr &lt;&lt; &quot;Usage: &quot; &lt;&lt; argv[0] &lt;&lt; &quot; filename&quot; &lt;&lt; std::endl;
        return 1;
    }

    std::ifstream fin(argv[1], std::ios::in);
    if (!fin) {
        std::cerr &lt;&lt; argv[1] &lt;&lt; &quot;: not readable&quot; &lt;&lt; std::endl;
        return 1;
    }

    std::vector&lt; std::vector&lt;double&gt; &gt; matrix;

    // Read input values into matrix
    {
        std::string line;
        while (getline(fin, line)) {
            std::vector&lt;double&gt; v;
            std::istringstream nums(line);
            double x;
            while (nums &gt;&gt; x) {
                v.push_back(x);
            }
            if (!nums) {
                std::cerr &lt;&lt; argv[1] &lt;&lt; &quot;: parse error&quot; &lt;&lt; std::endl;
            }
            matrix.push_back(v);
        }
    }
    if (!fin) {
        std::cerr &lt;&lt; argv[1] &lt;&lt; &quot;: reading failed&quot; &lt;&lt; std::endl;
        return 1;
    }
    fin.close();

    // Test that input isn't empty
    const auto rows = matrix.size();
    if (rows == 0) {
        std::cerr &lt;&lt; argv[1] &lt;&lt; &quot;: empty&quot; &lt;&lt; std::endl;
        return 1;
    }
    // Test that input is rectangular
    const auto cols = matrix[0].size();
    for (auto i = cols/cols;  i &lt; matrix.size();  ++i) {
        if (matrix[i].size() != cols) {
            std::cerr &lt;&lt; argv[1] &lt;&lt; &quot;:&quot; &lt;&lt; (i+1)
                      &lt;&lt; &quot;: line has &quot; &lt;&lt; matrix[i].size() &lt;&lt; &quot; columns; expected &quot; &lt;&lt; cols &lt;&lt; std::endl;
            return 1;
        }
    }

    auto const&amp; outfile = &quot;latex.txt&quot;;
    std::ofstream fout(outfile, std::ios::out);
    if (!fin) {
        std::cerr &lt;&lt; outfile &lt;&lt; &quot;: not writeable&quot; &lt;&lt; std::endl;
        return 1;
    }

    static auto const col_sep = &quot; &amp; &quot;;
    static auto const row_sep = &quot; \\\\\n\\hline\n&quot;;

    fout &lt;&lt; &quot;\\begin{center}\n\\begin{tabular}{&quot;;
    for (int i=0;  i&lt;col;  ++i) {
        fout &lt;&lt; &quot;|c&quot;;
    }
    fout &lt;&lt; &quot;|}\n&quot;
         &lt;&lt; &quot;\\hline\n&quot;;

    // Ask for col titles:
    for (int i=0;  i&lt;cols;  ++i) {
        std::cout &lt;&lt; &quot;Enter the title of column &quot; &lt;&lt; i+1 &lt;&lt; &quot;, as TeX source:&quot; &lt;&lt; std::endl;
        std::string title;
        getline(std::cin,title);
        fout &lt;&lt; title
             &lt;&lt; (i==cols-1 ? row_sep : col_sep);
    }

    // Write out the values
    for (const auto&amp; v: matrix) {
        for (auto i=0u;  i&lt;cols;  ++i) {
            fout &lt;&lt; v[i]
                 &lt;&lt; (i==cols-1 ? row_sep : col_sep);
        }
    }

    fout &lt;&lt; &quot;\\end{tabular}\n&quot;
         &lt;&lt; &quot;\\end{center}&quot;;
    fout.close();

    if (!fout) {
        std::cerr &lt;&lt; &quot;Failed to write &quot; &lt;&lt; outfile &lt;&lt; std::endl;
        return 1;
    }

    return 0;
}