0

I am new to c# and trying to read a binary file. I do so in c++ like this :

int main(int argc, char * * argv) 
{
    FILE * fp;
    fp = fopen(argv[1], "rb"); //argv[1] because while executing at terminal the binary file to be read is at second postion like "./filename.c BinaryInutFile.bin" so at argv[0] we have ./filename.c and at argv[1] we have Binaryfile.bin
    ch = fgetc(fp);
    while (fread( & ch, sizeof(ch), 1, fp)) 
    {
        add_symbol(ch); //this add_symbol()i will use somewhere else, so not so important for now.
    }
    fclose(fp); 
}

So i want help in writing equivalent c# code.Thanks to the helpers. Note: I don't know the size of file and name of the file as well but it's a binary file, I mean the user can change the binary file at terminal it should work for all the number of binary files he check for output. And i will execute at terminal like this "mono filename.exe BinaryFile.bin" where filename.exe is the file which was formed by compiling the filename.cs (by doing "gmcs filename.cs") which contain this code and BinaryFile.cs will be the bbianry file to be tested on my code and "mono" i have used because i am working on Ubantu and i compile using "gmcs FileName.cs" which will give Filename.exe and then execute it as "mono filename.exe BinaryFile.bin"

user252990
  • 27
  • 1
  • 9
  • Do you have to read it char by char? – Jonathas Costa Mar 10 '14 at 12:08
  • Jonathas it's a binary file, so it don't have characters. It has data in 11010101 form. and i have to calculate the frequency of the symbols in that files(I mean how many times each symbol repeats is the frequency, that i will do further in add_symbol() function).Any help how to do that please ? – user252990 Mar 10 '14 at 12:11

1 Answers1

1

There are lots of ways to read a file in C#. If you want a stream approach it looks like:

public static void Main(string[] args)
{
    // your command line arguments will be in args
    using (var stream = new BinaryReader(System.IO.File.OpenRead(args[0])))
    {
        while (stream.BaseStream.Position < stream.BaseStream.Length)
        {
            // all sorts of functions for reading here:
            byte processingValue = stream.ReadByte();
        }
    }
}

If you are processing a smaller file, there is also the option of reading the entire file into memory (string variable) with the following call:

string entireFile = System.IO.File.ReadAllText(fileNamePath);

Either works well - the use is going to depend on your situation. Best of luck!

EDIT - Edited the example to include a main routine so that could be demonstrated. To use this example you need to create a "console application" project and build using either visual studio or using the command line (MSBuild or equivalent). Hope that sets you on the right track!

Community
  • 1
  • 1
drew_w
  • 10,320
  • 4
  • 28
  • 49
  • i have edited my question could you please see the part below of the question ? where is the main function in it ? I have to see what arguments do you have in main function to have access at the second "BinaryFile.bin" which is third argument at terminal after "mono" and "filename.exe" ? – user252990 Mar 10 '14 at 12:23
  • I'll add a "main" to my example although this is something you can find in most other examples! – drew_w Mar 10 '14 at 12:25
  • But in c++ when i run at terminal and i code in notepad++ for that file then i do main(int argc, char * * argv) in main function. And at terminal i do "./filename Input.bin" so in order to read this "Input.bin" i go to second argument by doing "argv[1]" (whereas argv[0] points to ./filename, which is exe file obtained on gcc filename.c -o filename), Don't we have any thing like this in c# ? (argv[1] ? instead of your "fileNamePath" in the code) ? – user252990 Mar 10 '14 at 12:29
  • @user252990 you may have to debug a bit and write the arguments out to the console "Console.WriteLine(args[0])". The program name is not included in the arguments in C#, though I don't know how mono processes this. ALSO - you will note my revised example uses "args[0]". This is the correct way to get command line arguments! – drew_w Mar 10 '14 at 12:31
  • thanks for the help i tried to do console.writeline by doing "mono shekhar_c#.exe out.bin" i found that there is repetition of "out".bin until the file terminates, like this "out.bin out.bin out.bin out.bin out.bin hp@ubuntu:~/Desktop/" . Is it reading this binary file as strings ? becausei have to calculate he frequency of the symbols in this binary file (I mean number of times each symbol appears is frequency). Is what you told is the right way to achieve this ? – user252990 Mar 10 '14 at 12:39
  • @user252990 What it sounds like is that you put the `Console.WriteLine` inside of the `while` statement. That would print it repeatedly. Rather than me debugging in the comments - consider marking this as the answer to your original question of how to read a file and then open a new question for how to run a program on mono (or whatever further issues you have)! – drew_w Mar 10 '14 at 12:42
  • No need for a `BinaryReader` here. You can just open the `FileStream` and call its [ReadByte method](http://msdn.microsoft.com/en-us/library/system.io.filestream.readbyte(v=vs.110).aspx). For example: `while ((inp = fs.ReadByte()) != -1)`. The return value is an `int` that was cast from a `byte`. See the link for more info. – Jim Mischel Mar 10 '14 at 13:12
  • @JimMischel could you please explain in a code snippet please ? To make it as reference. – user252990 Mar 11 '14 at 10:39
  • @JimMischel what is the type of inp ? Is it "int" which you are talking about in next line ? – user252990 Mar 11 '14 at 10:46
  • @user252990: See the linked `ReadByte` documentation for an example. `inp` is an `int`. – Jim Mischel Mar 11 '14 at 12:45