0

So I just noticed something weird about opening .txt files in sublime that were created in vim.

It seems sublime adds an empty line at the end of the .txt file. For example, if I use vim/gedit to type the following 2 line file:

1
2

When I open this .txt file in Sublime, it opens as

1
2
~

where I have used "~" to represent an empty line. Could someone try this and tell me if it's the same for you?

This is the code I was using that doesn't seem to work when I create the .txt file in sublime:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
  int      a;
  int      b;
  ifstream inFile;
  bool     validInputFound;

  inFile.open("inputVals.txt");

  if (inFile.fail())
  {
    cout << "Unable to open input file!" << endl;
    exit(1);
  }

  validInputFound = false;
  while (!validInputFound)
  {
    inFile >> a;

    if (inFile.eof())
    {
      cout << "EOF before reading a" << endl;
      exit(2);
    }
    else if (inFile.fail())
    {
      inFile.clear();
      inFile.ignore(200, '\n');
    }
    else
    {
      validInputFound = true;
    }
  }
  cout << "Read a: " << a << endl;
  validInputFound = false;

  while (!validInputFound)
  {
    inFile >> b;

    if (inFile.eof())
    {
      cout << "EOF before reading b" << endl;
      exit(2);
    }
    else if (inFile.fail())
    {
      inFile.clear();
      inFile.ignore(200, '\n');
    }
    else
    {
      validInputFound = true;
    }
  }
  cout << "Read b: " << b << endl;

  cout << "Sum: " << a + b << endl;
  inFile.close();

  return (0);
}

The expected output is:

Read a: 1
Read b: 2
Sum: 3

But if you create the inputVals.txt file in Sublime, you obtain:

Read a: 1
EOF before reading b
roulette01
  • 1,984
  • 2
  • 13
  • 26
  • What is your point ? Do you suspect one of these editors to add some CR/LF ? If you have some doubt, just open the file in an hex editor and check. – kebs Mar 13 '16 at 19:05
  • well, my OP is asking a question, not stating a point. – roulette01 Mar 13 '16 at 19:09
  • Sure, but that question is a bit off topic to me, that's why I was asking for some clarification. If you suspect an editor is doing weird things, than make sure it is true, then ask "why is XXX doing YYY". To me, this is just some display behavior, thus off topic for SO. Furthemore, what is the point ? What difference does it make, from a programmers point of view that XXX is showing a blank line at the end of the file ? – kebs Mar 13 '16 at 19:16
  • Oh, so I am currently taking a C++ class and the code provided by the instructor did not work when I created an input.txt file (that the code reads) in sublime, but works in vim/gedit. I will edit the code into the OP. – roulette01 Mar 13 '16 at 19:18
  • Sublime Text has a setting `ensure_newline_at_eof_on_save`. It could be possible, that you have set the setting to true. – r-stein Mar 14 '16 at 18:21

1 Answers1

0

It looks like vim adds the newline and not the Sublime Text.

As to why: Why would Vim add a new line at the end of a file?

Community
  • 1
  • 1
Taylan
  • 3,045
  • 3
  • 28
  • 38