Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. newline in win xp

newline in win xp

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SHaroz
    wrote on last edited by
    #1

    I'm writing a program in c whose output is directed to a file (i.e. myprog > myfile). My problem is that whenever I print a newline character, printf("\n"), the program outputs both a carriage return and a newline. So while the output should have just one character whose value is 10, the program outputs a 13 followed by a 10. I this this might have something to do with Windows XP. Has anyone else heard of this? I would greatly appreciate ANY help that can be offered. Thanks, Steve

    M 1 Reply Last reply
    0
    • S SHaroz

      I'm writing a program in c whose output is directed to a file (i.e. myprog > myfile). My problem is that whenever I print a newline character, printf("\n"), the program outputs both a carriage return and a newline. So while the output should have just one character whose value is 10, the program outputs a 13 followed by a 10. I this this might have something to do with Windows XP. Has anyone else heard of this? I would greatly appreciate ANY help that can be offered. Thanks, Steve

      M Offline
      M Offline
      Mike Nordell
      wrote on last edited by
      #2

      SHaroz wrote: My problem is that whenever I print a newline character, printf("\n"), the program outputs both a carriage return and a newline. This is the character secuence used to denote "new line" in MS-DOS and derived products (like Windows XP). It's handled by the CRT (C RunTime library) - whenever it sees a LF, it emits CRLF. To get "correct" line endings, open the file in binary mode. If, like in your example, it's supposed to work even with stdout, I believe you have to freopen "CONOUT$" (or whatever it's called - look up the docs for CreateFile). ++luck;

      S 1 Reply Last reply
      0
      • M Mike Nordell

        SHaroz wrote: My problem is that whenever I print a newline character, printf("\n"), the program outputs both a carriage return and a newline. This is the character secuence used to denote "new line" in MS-DOS and derived products (like Windows XP). It's handled by the CRT (C RunTime library) - whenever it sees a LF, it emits CRLF. To get "correct" line endings, open the file in binary mode. If, like in your example, it's supposed to work even with stdout, I believe you have to freopen "CONOUT$" (or whatever it's called - look up the docs for CreateFile). ++luck;

        S Offline
        S Offline
        SHaroz
        wrote on last edited by
        #3

        I tried mystream = freopen("myfile.out", wb, stdout) and it worked. The newline prints as one character. My problem is that I don't know how to find the file that stdout points to. I could avoid that if I knew a way to simply change the mode of stdout. I also thought that if the program is run like this "myprog > output.txt", argv[2] would be "output.txt". But that didn't work. Keep in mind that this needs to be ANSI compatable, so I can't use any function that starts with an underscore (i.e. _setmode). I tried looking up "CONOUT", but found nothing. Do you think you could offer any more advice? Thanks, Steve

        M 1 Reply Last reply
        0
        • S SHaroz

          I tried mystream = freopen("myfile.out", wb, stdout) and it worked. The newline prints as one character. My problem is that I don't know how to find the file that stdout points to. I could avoid that if I knew a way to simply change the mode of stdout. I also thought that if the program is run like this "myprog > output.txt", argv[2] would be "output.txt". But that didn't work. Keep in mind that this needs to be ANSI compatable, so I can't use any function that starts with an underscore (i.e. _setmode). I tried looking up "CONOUT", but found nothing. Do you think you could offer any more advice? Thanks, Steve

          M Offline
          M Offline
          Mike Nordell
          wrote on last edited by
          #4

          SHaroz wrote: Keep in mind that this needs to be ANSI compatable You didn't say that in your original post. If you need to stay within the bounds of ANSI C, and you still need to emit only LF when running on Win32 (which is an "illegal" Win32 EOL sequence), then you might try linking with binmode.obj. If that doesn't work, the only thing I can come to think of is to do your output to an internal string buffer, and then resort to fwrite. ++luck;

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups