Search code examples
javanewlinepascaleol

New line in different languages


I've encountered a problem while working on my testing system. I'm creating a file in Java, writing to it in Java, but reading from it in Pascal compilators. So, this may be not unclear, but when I do something like this in Java (Eclipse)

    File file = new File("D:/i.txt");
    BufferedReader bf = new BufferedReader(new FileReader(file));
    PrintWriter pw = new PrintWriter(new FileWriter(file));

    pw.print("hey\n");
    pw.print("you");
    bf.close();
    pw.close();

It gives me a file that looks like


hey
you

And when I run this code on Pascal language

begin
  assign(input,'D:/i.txt'); reset(input);
  while not eoln(input) do write(1);
 end.

Which means: Write "1" until you find a new line separator. It won't stop to write ones.

But this is okay. Here is another strange thing: Pascal must have a line break or a line separator, or new line indicator and I found this to be Char number 10 on ASCII table (LF, new line).

So, I decided to do other way.

    File file = new File("D:/i.txt");
    BufferedReader bf = new BufferedReader(new FileReader(file));
    PrintWriter pw = new PrintWriter(new FileWriter(file));

    pw.print("hey"+(char)10);
    pw.print("you");
    bf.close();
    pw.close();

This one would give me the same output file as the first bit of code ( at last apparently ). But all my Pascal compilators still keep complaining and writing hundreds of ones.

How can I solve the problem with new lines? Thank you.


Solution

  • I think the infinite loop (writing hundreds of ones) is because you never read anything from the input so it is never at the end-of-line. Try putting a read(input,ch); in the loop.