newline in win xp
-
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
-
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
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;
-
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;
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
-
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
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;