Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我在这个程序上有个错误_C# - Fatal编程技术网

C# 我在这个程序上有个错误

C# 我在这个程序上有个错误,c#,C#,在我有“using System;”、“using System.IO”和“namespace BinaryFileApplication”的行中。所以我真的不知道该怎么处理它们才能让C程序识别它们 using System; using System.IO; namespace BinaryFileApplication { class Program { static void Main(string[] args) { BinaryWriter bw;

在我有“using System;”、“using System.IO”和“namespace BinaryFileApplication”的行中。所以我真的不知道该怎么处理它们才能让C程序识别它们

using System;
using System.IO;

namespace BinaryFileApplication
{

class Program
{

    static void Main(string[] args)
    {
        BinaryWriter bw;
        BinaryReader br;
        int i = 25;
        double d = 3.14157;
        bool b = true;
        string s = "I am happy";

        //create the file
        try
        {
            bw = new BinaryWriter(new FileStream("mydata", FileMode.Create));
        }
        catch (IOException e)
        {
            Console.WriteLine(e.Message + "\n Cannot create file.");
            return;
        }

        //writing into the file
        try
        {
            bw.Write(i);
            bw.Write(d);
            bw.Write(b);
            bw.Write(s);
        }
        catch (IOException e)
        {
            Console.WriteLine(e.Message + "\n Cannot write to file.");
            return;
        }

        bw.Close();

        //reading from the file
        try
        {
            br = new BinaryReader(new FileStream("mydata", FileMode.Open));
        }
        catch (IOException e)
        {
            Console.WriteLine(e.Message + "\n Cannot open file.");
            return;
        }

        try
        {
            i = br.ReadInt32();
            Console.WriteLine("Integer data: {0}", i);
            d = br.ReadDouble();
            Console.WriteLine("Double data: {0}", d);
            b = br.ReadBoolean();
            Console.WriteLine("Boolean data: {0}", b);
            s = br.ReadString();
            Console.WriteLine("String data: {0}", s);
        }
        catch (IOException e)
        {
            Console.WriteLine(e.Message + "\n Cannot read from file.");
            return;
        }

        br.Close();

        Console.ReadKey();

    }

}

你不能。这看起来像.Net代码。为什么期望“C”编译器理解它呢?

这段代码不是C,而是C。您必须使用C#编译器(在VisualStudio中或使用Mono或类似的工具)构建它

如果您真的想将其构建为C,则必须将其转换为合法的C代码。类似的情况如下:

#include <stdio.h>

int main( int argc, char **argv )
{
  FILE *bw, *br;
  int i = 25;
  double d = 3.14157;
  _Bool b = true;
  char s[] = "I am happy";

  bw = fopen( "mydata", "wb" );
  if ( bw )
  {
    if ( fwrite( &i, sizeof i, 1, bw ) != sizeof i )
      // error writing i;
    if ( fwrite( &d, sizeof d, 1, bw ) != sizeof d )
      // error writing d
    if ( fwrite( &b, sizeof b, 1, bw ) != sizeof b )
      // error writing b
    if ( fwrite( s, sizeof s, 1, bw ) != sizeof s ) // no & operator on s
      // error writing s
    fclose( bw );
  }

  // remainder left as exercise
}
#包括
int main(int argc,字符**argv)
{
文件*bw,*br;
int i=25;
双d=3.14157;
_布尔b=真;
char s[]=“我很快乐”;
bw=fopen(“我的数据”、“wb”);
如果(bw)
{
if(fwrite(&i,sizeof i,1,bw)!=sizeof i)
//错误写入i;
if(fwrite(&d,sizeof d,1,bw)!=sizeof d)
//写入数据时出错
if(fwrite(&b,sizeof b,1,bw)!=sizeof b)
//写入b时出错
if(fwrite(s,sizeof s,1,bw)!=sizeof s)//s上的否运算符(&O)
//错误写入
fclose(bw);
}
//剩下的作为练习
}
C没有名称空间1,它没有类,它没有
BinaryReader
BinaryWriter
,它不做结构化异常处理,等等。它是一种与C完全不同的语言


  • 也就是说,它没有用户可定义的名称空间。有 4个内置名称空间:标签名称、结构和联合标记名称、结构和联合成员名称以及其他所有内容。

    错误是什么?C编译器无法识别它们-您不能只将C代码编译为C。您必须将其转换为C代码。欢迎使用stackoverflow,请添加适当的标记,以便其他人可以帮助您。或者,您需要使用C编译器来构建它。