Click here to Skip to main content
14,865,273 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
lw@zi 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.....
Andreas Gieriet 18-Jan-13 5:27am
   
I assume you should work on byte[] - not on char (this is different to C/C++ world).
E.g.
static byte[] ReadFile(string path)
{
try
{
return File.ReadAllBytes(path);
}
catch (Exception)
{
return new byte[] { };
}
}


Cheers
Andi
prajwal rao 18-Jan-13 5:32am
   
but how can i make a block 4096.. and how can i name them...???
Andreas Gieriet 18-Jan-13 5:56am
   
Did you read the link I suggested above and in my solution #1 below?
Do you know what block code is?
If not, you must first learn that before going on to implement it.
Andi
prajwal rao 18-Jan-13 6:05am
   
ok sir but can you tell me how to read a file of 4096 bits...
Andreas Gieriet 18-Jan-13 6:26am
   
Is it bits or bytes?
If the flie contains ony the 4096 [whatever units] of data and nothing else, you can read the whole file at once. That's how I understand your statement in your quesiton: [...]read a file of size 4096[...]

If it is 4069 bits, that is 4096/8 bytes (= 512 bytes = 2^9 bytes), correct?
Andi
PS: Did you see my solution #1 and #2?
prajwal rao 18-Jan-13 6:31am
   
ya sir i contains 4096 bits
so when we divide by 8..... its 512 bytes.....
so it contains 8 blocks of 512 bytes.....
Andreas Gieriet 18-Jan-13 6:38am
   
Sorry, you give weird statements. Looking at your other posts indicates that this is far beyond your current skills. Learn the very basics first. Take one of the many C# online tutorials and learn what a variable is, how to check if a file exists, how to access the command line arguments, etc.
I have to give up here since I see no chance to get you understand what Block Code means when you don't know to express unambiguously what the difference between bit and byte is.
You can not ask to assist you building a house when you do not know the meaning of the word dor, window, roof, etc. Sorry.
Andi
PS: An *please* learn to also *answer* questions. I did ask you many question above to try to clarify what you know and what you want to achieve and you only answer half of it at best. E.g. I provided code in solution #2 but you do not say it's what you need or what is missing... Difficult.
PPS: Someone else please take over.
prajwal rao 18-Jan-13 6:47am
   
ok...:(
lukeer 18-Jan-13 3:12am
   
Please use the "Improve question" link just below your question to provide such additional information. This time, I did it for you.
Please remove the code from your comment now. It is much more readable between <pre lang="c#">such tags as these</pre> in the question area.
prajwal rao 18-Jan-13 3:14am
   
ok sorry....
Andreas Gieriet 18-Jan-13 5:41am
   
What is your block code? How many bits are added and in what way?
You must provide more data to this.
Andi
prajwal rao 18-Jan-13 5:48am
   
ok there is a file...... it contains so many bits of data....
1)i have to read that file
2)now i have to create each block of 4096....and then i have to give code for those blocks
3) again i have to read those block...
prajwal rao 18-Jan-13 5:50am
   
never mind leave that point number 3..... help me to do till point number 2....please....

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
   
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;
}
   

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