【问题标题】:What is C# for C " FILE *f;"?C" FILE *f;" 的 C# 是什么?
【发布时间】:2010-07-17 14:04:35
【问题描述】:

我是 C# 的初学者 我想知道如何用 C# C 编写代码

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

请帮忙。

顺便说一句:嗯,我正在尝试使用 Tao.FFMpeg 移植 FFMPEG api-example 呈现here(Tao 是 C# 周围的 .Net 包装器。旧的并且与 FFMPEG 本身一样具有sintax)所以请您阅读那个并告诉我在我的代码示例中遗漏了什么......

我的问题是 - 我了解如何移植 FFMpeg 部分,但我只是不知道如何以适合 .Net 的方式移植文件 IO 部分\函数

【问题讨论】:

  • 呵呵……你没有给outbuf赋值……(更具体地说,数组的内容是未定义的)
  • 查看 MSDN File class 文档

标签: c# .net c++ c file


【解决方案1】:

我不会为您编写所有代码,而是将您定向到System.IO.File

【讨论】:

    【解决方案2】:

    好吧,由于您提供的库的示例代码读取和写入二进制文件,我建议使用 System.IO.BinaryReader 和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
            }
        }
    

    【讨论】:

      【解决方案3】:

      您的代码示例令人困惑。您的意思是在缓冲区中放入有意义的内容并将其写入文件,或者您的意思是从文件中读取到缓冲区中。

      using System.IO;
      
      byte[] bytesFromFile = File.ReadAllBytes(filePath);
      
      byte[] someBytes = new byte[someLength];
      // omitted: put some values into someBytes array
      File.WriteAllBytes(filePath, someBytes);
      

      【讨论】:

      • 嗯,我正在尝试使用 Tao.FFMpeg 移植此处提供的 FFMPEG api-example squashedfrog.net/mythtv/devdocs/api-example_8c-source.html(Tao 是 C# 周围的 .Net 包装器。旧的并且与 FFMPEG 本身一样使用 sintax)所以你能请阅读那个并告诉我在我的代码示例中遗漏了什么......
      【解决方案4】:

      看看下面的命名空间System.IO。具体来说,您需要查看System.IO.FileSystem.IO.FileStream

      【讨论】:

        【解决方案5】:

        请参阅FileStream.WriteByte 帮助。有一个例子。

        【讨论】:

        • 抱歉,有时我没有意识到我正在阅读西班牙语(我的母语)或英语,而这次我忘了检查 :) 谢谢 jalf。
        猜你喜欢
        • 2010-12-14
        • 1970-01-01
        • 2021-10-06
        • 2020-08-07
        • 1970-01-01
        • 2011-08-06
        • 1970-01-01
        • 2016-02-29
        • 2010-10-31
        相关资源
        最近更新 更多