Click here to Skip to main content
15,919,121 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a code to read a file ... but it shows some error ....
so what can i do....
C#
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;
        }
    } 
}
Posted
Updated 17-Jan-13 22:58pm
v2
Comments
Mohd. Mukhtar 18-Jan-13 5:00am    
What error are you getting?
prajwal rao 18-Jan-13 5:04am    
Error 1 'System.IO.File': array elements cannot be of static type
Error 2 Cannot declare a variable of static type 'System.IO.File[]
Error 3 Cannot create an instance of the static class 'System.IO.Directory
Error 4 'System.IO.File' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'System.IO.File' could be found (are you missing a using directive or an assembly reference?

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

By using StreamReader class:
C#
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);
    }
}

Cheers
Andi
 
Share this answer
 
 
Share this answer
 
Comments
prajwal rao 18-Jan-13 5:28am    
ok thank you....
There are quite a few things you need to look at here, but the compilation error you are probably getting relates to the line:
C#
File[] fe = (new Directory(".")).GetFiles(cmdline[1]);
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:
C#
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:
C#
if (!File.Exists(cmdline[1]))
    {
    Console.WriteLine(cmdline[1] + ": file not found");
    return;
    }



"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!
C#
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;
        }
    }
 
Share this answer
 
v2
Comments
prajwal rao 18-Jan-13 5:20am    
but where to put this code sir...

if (!File.Exists(cmdline[1]))
{
Console.WriteLine(cmdline[1] + ": file not found");
return;
}
OriginalGriff 18-Jan-13 5:25am    
In place of the existing (bad) code:
File[] fe = (new Directory(".")).GetFiles(cmdline[1]);
if (fe.Length == 0)
{
Console.WriteLine(cmdline[1] + ": file not found");
return;
}
prajwal rao 18-Jan-13 5:27am    
ok now i got 1 new error
Error 1 The name 'fe' does not exist in the current context
OriginalGriff 18-Jan-13 5:44am    
That is because you are now trying to use the number of elements in a array (that you couldn't declare) as the number of bytes you should read from a file...
I would suggest you look at creating a FileInfo instance...

FileInfo fi = new FileInfo(path);
long numberOfBytesInTheFile = fi.Length;

Can you guess what you should use instead of the "path" parameter? :laugh:
prajwal rao 18-Jan-13 5:53am    
sorry sir.... i am new for this language.... just learning.....:( i cant guess it...:(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900