这段代码有什么问题......? [英] whats wrong with this code...?

查看:100
本文介绍了这段代码有什么问题......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码来读取文件...但它显示了一些错误....

所以我该怎么做....

i have a code to read a file ... but it shows some error ....
so what can i do....

using System;
using System.IO;
class FileRead
{
    string filereadbuf;  
    public void ReadFile(string FileName, int FileSize)
    {
        char[] buf = new char[FileSize];  
        
        StreamReader sr = new StreamReader(new FileStream(FileName, FileMode.Open, FileAccess.Read));
        int retval = sr.ReadBlock(buf, 0, FileSize); // 
        Console.Write("Total Bytes Read = " + retval + "\n");
        filereadbuf = new string(buf);  
        Console.WriteLine(filereadbuf); 
        sr.Close();
    }
}
 
class TestFileRead
{
    public static void Main(string[] args)
    {
        String[] cmdline = Environment.GetCommandLineArgs(); 
        Console.WriteLine("File Reader Using Stream Reader & File Stream \n");
        if (cmdline.Length < 2) 
        {
            Console.WriteLine("Usage: " + cmdline[0] + " <input file> ");
            return;
        }
      
      
        File[] fe = (new Directory(".")).GetFiles(cmdline[1]);
        if (fe.Length == 0)
        {
            Console.WriteLine(cmdline[1] + ": file not found"); 
            return;
        }
        FileRead fr = new FileRead();
        try
        {
            fr.ReadFile(cmdline[1], (int)fe[0].Length); 
        }
        catch (IOException e)
        {
            Console.WriteLine("I/O error occured" + e);
            return;
        }
    } 
}

推荐答案

你需要注意很多事情这里,但你可能得到的编译错误与该行有关:

There are quite a few things you need to look at here, but the compilation error you are probably getting relates to the line:
File[] fe = (new Directory(".")).GetFiles(cmdline[1]);

并告诉你,你不能创建一个静态对象数组。



问题是File类是静态的 - 整个应用程序只有一个实例,所以在任何情况下你都不能创建一个File变量,当然也不能创建它们的数组 - 因为你不能得到它来填充它这个行有一个更广泛的问题:GetFiles是Directory类的静态属性,它不返回File对象,它返回一个数组字符串。所以你实际应该在这里说的是:

And tells you that you can''t create an array of static objects.

The problem is that the File class is static - there is one and only one instance of it for the entire application, so you cannot under any circumstances create a File variable, and certainly not an array of them - because you can''t get at the object to fill it with!

Butthere is a wider problem with this line: GetFiles is a static property of the Directory class, and it doesn''t return File objects, it returns an array of strings. So what you actually should have said here is:

string[] fe = Directory.GetFiles(cmdline[1]);

但这实际上并不是你想做的!有一种更容易实现你想要的方法:

But that isn''t actually what you want to do either! There is a much, much easier way to achieve what you want:

if (!File.Exists(cmdline[1]))
    {
    Console.WriteLine(cmdline[1] + ": file not found");
    return;
    }







先生可以编辑上面的代码并更新它请使用if(!File.Exists(loadFille)



嗯......这是你的功课......所以我真的不应该' '...



但是,好的 - 如果你能告诉我到底发生了什么,以及我为什么要移动你的一些代码呢?




"sir can u edit the above code and update it please using if(!File.Exists(loadFille)"

Well...it''s your homework...so I really shouldn''t...

But, ok - if you can tell me exactly what is going on, and why I moved some of your code around!

public static void Main(string[] args)
    {
    String[] cmdline = Environment.GetCommandLineArgs();
    Console.WriteLine("File Reader Using Stream Reader & File Stream \n");
    if (cmdline.Length < 2)
        {
        Console.WriteLine("Usage: " + cmdline[0] + " <input file> ");
        return;
        }
    string loadFile = cmdline[1];

    if (!File.Exists(loadFile))
        {
        Console.WriteLine(loadFile + " : file not found");
        return;
        }
    try
        {
        FileRead fr = new FileRead();
        FileInfo fi = new FileInfo(loadFile);
        fr.ReadFile(loadFile, (int) fi.Length);
        }
    catch (IOException e)
        {
        Console.WriteLine("I/O error occured" + e);
        return;
        }
    }


请参阅以下链接。



无法声明静态类型变量'' c#.net文件上传中的System.IO.File''[ ^ ]



http://www.dotnetspider.com/forum/128609-Cannot-declare-variable-static-type -System.IO.File.aspx [ ^ ]



这可能会对你有帮助。
Refer the below link.

Cannot declare a variable of static type ''System.IO.File'' in c# .net file upload[^]

http://www.dotnetspider.com/forum/128609-Cannot-declare-variable-static-type-System.IO.File.aspx[^]

This may help you.


蛮力方法(需要LINQ):

Brute force approach(requires LINQ):
public static string ReadHeader(string filePath, int maxLen)
{
    return new string(File.ReadAllText(filePath).Take(maxLen).ToArray());
}



使用StreamReader类:


By using StreamReader class:

public static string ReadHeader(string filePath, int maxLen)
{
    using (var reader = new StreamReader(filePath))
    {
        int pos = 0;
        char[] buffer = new char[maxLen];
        int n = reader.ReadBlock(buffer, pos, maxLen) - pos;
        return new string(buffer, 0, n);
    }
}



干杯

Andi


Cheers
Andi


这篇关于这段代码有什么问题......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆