Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I write a program to read a file of size 4096 and I have to give block code for that. After that to write the file by retrieving those block code?

My current code is this
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 21:09pm
v2
Comments
dan!sh 18-Jan-13 2:45am    
Have tried something?
Andreas Gieriet 18-Jan-13 2:51am    
Do you mean:
1) read 4096 bytes of data from a file
2) "decode" the block code to correct potential data errors and retrieve the payload from the file
3) store the extracted payload to a file
Is it that what you need to do?
Andi
prajwal rao 18-Jan-13 2:54am    
ya you a right sir....
Andreas Gieriet 18-Jan-13 3:22am    
What you show in your code has nothing to do with Block Code. So, what you you really want to achieve? From your wording, I understand that you get 4096 bytes of block encoded data and you need to store the payload into a new file. But I'm not quite sure about your wording.Please check the above mentioned link and make a clear statement if you talk about Block Code with respect to coding theory (and not as you show in your code where you read data chunk (block) wise.
Andi
prajwal rao 18-Jan-13 4:54am    
sir i want to achieve as you mentioned in your first comment....
and that code which is shown only to read a file.....

You may have a look at MSDN: Basic File I/O[^]. It provides several common scenarios and how to implement them by means of C#.

For Block Code, you find plenty of google hits, e.g. http://www.complextoreal.com/chapters/block.pdf[^].

Cheers
Andi
 
Share this answer
 
v2
Your task is to implement the byte[] BlockDecode(byte[] encoded) function.
A starting point might be the following skeleton.
Since you do not provide any information so far on the block code, I can not provide any implementation for it... (see BlockDecode method body below).
C#
static void Main(string[] args)
{
   ...
   string inFilePath = ...;
   string outFilePath = ...;
   ...
   byte[] encoded = File.ReadAllBytes(inFilePath);
   byte[] decoded = BlockDecode(encoded);
   File.WriteAllBytes(outFilePath, decoded);
}
private static byte[] BlockDecode(byte[] encoded)
{
   byte[] decoded = new byte[encoded.Length];
   int decodedBytes = 0;
   // here you must provide the code for extracting the payload
   // from the encoded bytes counting up the number of decoded bytes
   ...
   Array.Resize(ref decoded, decodedBytes);
   return decoded;
}
 
Share this answer
 

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