c - How is line break handled under Windows and why it affect text rendering in some editor -


i copied excerpt pdf , paste on sublime text. excerpt came line break:

with line break

i wrote small c program remove line break.

#include <stdio.h> #include <stdlib.h> #include <assert.h> int main(){     file* in = fopen("feynman.txt","r");     file* out = fopen("feynmanstripped.txt","w");     assert(in!=null && out!=null);     int c;         c = fgetc(in);         while(c!=eof){         if(c!='\n')             fputc(c,out);         c = fgetc(in);     }  } 

the program executed under cygwin.

the resulting text opened in sublime text , notepad: enter image description here

as can see, line break disappear in notepad, not in sublime text. tried read/write "rb"/"wb" mode, didn't make difference.

i guess might due how windows deals '\n' , '\r', affects how sublime text , notepad render text. working under hood?

( note : copy/paste same text ms word, result same in st )

interesting. yes, related whole \r\n thing, there's no other sane explanation unless files mangled in strange ways. little odd, though, happens when you're opening files in text mode under windows; you'd expect mapping done you. there's rotten in state of cygwin, , fortunately documented here.

long story short: you're not opening file in text mode. surprising because c standard rather says code should doing that, cygwin strays path here. instead, you're opening file in cygwin docs call default mode. whether default mode windows newline mapping depends on number of things such whether file path specified unix or windows path, how file system unix path resolves "mounted," how linked program , other stuff (follow link details). leaves several ways resolve problem:

  • link -ltextmode, forcing default mode mean text mode,
  • fopen("feynman.txt", "rt") (note: not, strictly speaking, standard-conforming code),
  • fopen(".\\feynman.txt", "r") (force windows path)
  • if(c != '\n' && c != '\r') ...
  • maybe others, ones can think of right now.

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -