tags:

views:

182

answers:

5

I am a beginner in C# I wonder how to write in C# C's

static void example(const char *filename)
 {
FILE *f;
outbuf_size = 10000;
outbuf = malloc(outbuf_size);

f = fopen(filename, "wb");
     if (!f) {
       fprintf(stderr, "could not open %s\n", filename);
         exit(1); 
  }

fwrite(outbuf, 1, outbuf_size, f);
fclose(f);
}

Plase help.

BTW: Well I am trying to port FFMPEG api-example presented here using Tao.FFMpeg (Tao is .Net wrapper around C#.. old and with sintax exact as in FFMPEG itself) so could you please read that one and tell what I missed in my code sample...

My problem is - I understand how to port FFMpeg part and I just do not det how to port File IO part\function in the way good for .Net

+5  A: 

Rather than writing all of the code for you, I'm going to direct you to System.IO.File.

Donnie
+1  A: 

Take a look at the following namespace System.IO. Specifically you will want to look at System.IO.File and also System.IO.FileStream

Chris Taylor
+1  A: 

See FileStream.WriteByte help. There is an example.

Vale, me gusta... pero es en espan~ol... and stackoverflow is not...
Blender
English version: http://msdn.microsoft.com/en-us/library/system.io.filestream.writebyte.aspx
Erv Walter
Fixed the link :)
jalf
Sorry about that, sometimes I don't realise that I'm reading Spanish (my mother tongue) or English and this time I forgot to check :) Thanks jalf.
+2  A: 

Your code sample is confusing. Either you mean to put something meaningful in the buffer and write it to the file, or you mean to read from the file into the buffer.

using System.IO;

byte[] bytesFromFile = File.ReadAllBytes(filePath);

byte[] someBytes = new byte[someLength];
// omitted: put some values into someBytes array
File.WriteAllBytes(filePath, someBytes);
Daniel Earwicker
Well I am trying to port FFMPEG api-example presented here http://www.squashedfrog.net/mythtv/devdocs/api-example_8c-source.html using Tao.FFMpeg (Tao is .Net wrapper around C#.. old and with sintax exact as in FFMPEG itself) so could you please read that one and tell what I missed in my code sample...
Blender
+1  A: 

Well, since the sample code of the library you provided reads and writes binary files, I'd suggest System.IO.BinaryReader and System.IO.BinaryWriter.

    static void example(string filename)
    {
        StreamReader sr;
        BinaryWriter bw;

        try
        {
            sr = new StreamReader(filename);

            bw = new BinaryWriter(File.Open("out.bin", FileMode.Create));

            bw.Write(sr.ReadToEnd());

            bw.Flush();
            bw.Close();

            sr.Close();
        }
        catch(Exception ex)
        {
            // Handle the exception
        }
    }
Hamid Nazari