views:

541

answers:

1

Hi all:

I have a c# project in my Visual studio 2008 which when built, outputs a bunch of files in a temp directory for other programs to use i.e. JsTestDriver, JsCoverage, etc (all a bit Unix-related).

Trouble is, everytime when the files were generated, they seem to contain weird return carriage/line feed which upsets the programs that would use them next.

What I'm currently doing is manually create a notepad file, rename it to a different name, then copy and paste the entire content of the generated file (and save) to solve this problem. This is, of course, tedious and not something I enjoy doing (ultimately I want this whole process to be automated).

So my question is: how do I get Visual Studio to output the correct/proper CR/LF so I no longer need to go through the manual process?

If the above is too vague, I'll be happy to clarify. Thanks in advance.

+1  A: 

Yes, it's a bit too vague at the moment - you should provide:

  • More details about how you're writing out the files
  • More details about exactly what's in the files afterwards. Use a hex editor to find this out.

The simplest way of changing what happens in terms of line endings is probably to just be explicit about it in the code. For example, instead of:

output.WriteLine(foo);

write

output.Write(foo);
// Here LineTerminator would be a constant - or you could make
// it a variable somewhere
output.Write(LineTerminator);

... possibly encapsulating this in your own class to make it easier (so you can have your own WriteLine method which does the right thing, perhaps).

EDIT: I've been assuming that it's your own code writing out the files. If that's not the case, the easiest solution is probably to find or write a tool to convert the files, and put it as a postbuild step. I'm sure such tools exist (I think there was one called dos2unix a while ago) but it may be as easy to write your own as to find one which does exactly what you want.

Jon Skeet
@Jon Skeet: yes you are right, I did not write the code myself. Before your answer, I was under the assumption that the CR/LF comes from VS so I was looking for an option to convert them to linux compatible format. Now come to think about it, they should come from the code which generated the files (initially I thought it was some sort of custom build process). The generated files look the same as other files in Notepad++. Havent try with hex editor yet. Looks like I'll have to search for the logic to see if theres anything suspicious. Thanks.
BeraCim
It's probably just using TextWriter.WriteLine, which will use the system default line break (i.e. CR/LF on Windows).
Jon Skeet
@Jon Skeet: I found that the generated files were created using copy command from msbuild e.g. <Copy SourceFiles="blah" DestinationFiles="blah"/>. The "source file" itself was just created like any other text file. The CR/LF doesnt appear to be coded manually. On another note, the strange thing is that if I copy the entire content of the generated file, and paste + save it into a notepad, the problem disappears... and I'm doing all of the above in Windows. I'm wondering whether the CR/LF is really the problem now even though there is nothing else I could see that is suspicious...
BeraCim
@Jon Skeet: I found the problem. It turned out that there was something wrong with the encoding of the file. With properly encoded characters, everything worked fine. Thanks.
BeraCim