《程序设计》第四周作业

 using System;
using System.IO;
public class CopyFileAddLineNumber 
{
    public static void Main(string[] args) 
    {
        string infname = "CopyFileAddLineNumber.cs";
        string outfname = "CopyFileAddLineNumber.txt";
        if( args.Length >= 1 ) infname = args[0];
        if( args.Length >= 2 ) outfname = args[1];
  
        try 
        {
            FileStream fin = new FileStream( 
                infname, FileMode.Open, FileAccess.Read );
            FileStream fout = new FileStream(
                outfname, FileMode.Create, FileAccess.Write );
  
            StreamReader brin = new StreamReader( 
                fin, System.Text.Encoding.Default );
            StreamWriter brout  = new StreamWriter(
                fout, System.Text.Encoding.Default );
  
            int cnt = 0; // 行号
            string s = brin.ReadLine();
            while ( s != null ) 
            {
                cnt ++; 
                s = deleteComments(s);                        //去掉以//开始的注释
                brout.WriteLine(cnt + ": \t" + s );           //写出
                Console.WriteLine(cnt + ": \t" + s );     //在控制上显示
                s = brin.ReadLine();                      //读入
            }           
            brin.Close();               // 关闭缓冲读入流及文件读入流的连接.
            brout.Close();
        } 
        catch (FileNotFoundException) 
        {
            Console.WriteLine("File not found!" );
        } 
        catch (IOException e2) 
        {
            Console.WriteLine( e2 );
        }
    }
  
    static string deleteComments( string s ) //去掉以//开始的注释
    {
        if( s==null ) return s;
        int pos = s.IndexOf( "//" );
        if( pos<0 ) return s;
        return s.Substring( 0, pos );
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值