/* C# source code. * Append a newline to a file (if the last line does not contain one, * and write the result to another file. * Usage: * .\bfile2bfile inputfilename outputfilename * which means the command takes two arguments. The first one must * be an existing file. The second file will be created if not existing * and overwritten if existing. * * Compilation: * csc bfile2bfile.cs * * see also od(1), xdd(1) * * Richard J. Mathar, 2011-08-20 */ using System ; using System.IO ; public class bfile2bfile { static void Main(string[] args) { /* input and output file streams */ FileStream inp = new FileStream(args[0],FileMode.Open) ; FileStream oup = new FileStream(args[1],FileMode.Create) ; /* keep a previous byte to remember whether a \n has been seen */ int prevb = -1 ; int b = inp.ReadByte() ; while ( b != -1) { oup.WriteByte((byte)b) ; prevb = b ; b = inp.ReadByte() ; } /* If the last byte wasn't a line feed, append one */ if ( prevb != '\n') oup.WriteByte(10) ; inp.Close() ; oup.Close() ; } }