Thread: how to convert this C function into C # code?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    6

    how to convert this C function into C # code?

    Hi,

    Can any C-Sharp expert out there convert the C function below, into
    C-Sharp code, that I can insert into an already-existing C-Sharp program, that will compile? Thanks.

    I have an existing C code that compiles in the C environment. I want to insert this function into an already running C-Sharp program. Because I only have a week to get this done, learning C-Sharp at this time is not practical.

    The C function is simple:

    a) get the output of the ipconfig /all command, write it into a tmp file
    b) read that file for the string "gateway.net" or for anything string
    that contains the string "192.168.1"
    c) if this is found, set found = 1
    d) when done, delete the tmp file
    e) if found == 0, stop the program

    Below is that C function.

    Code:
    Check ( )
    
    {
      char str [100];
      int found = 0;
    
      FILE *fi;
    
      system ("ipconfig /all > c:\\temp\\tmp_file");
    
      fi = fopen ("c:\\temp\\tmp_file", "r");
      
      while (! feof (fi))
        {
          fscanf (fi, "%s", str);
    
          if ((strcmp (str, "gateway.net") == 0) || (strstr (str, "192.168.1") != NULL))
            found = 1;
        }
    
      fclose (fi);
      
      remove ("c:\\temp\\tmp_file");
    
      if (found == 0)
        exit (0);
    
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why would you want to convert something which is full of bugs?

    > system ("ipconfig /all > c:\\temp\\tmp_file");
    Chances are, c# already has a method to enumerate all the network connections anyway.

    > Because I only have a week to get this done, learning C-Sharp at this time is not practical.
    You don't have to learn it all, just enough to make a reasonable stab at it.
    1 day on C# network APIs might go a long way to helping you solve the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Salam is right, there are some nice helpers built into C# for this kind of thing. Hint: use the System.Net.NetworkInformation namespace.

    I've just put together a C# version of what you require in a few lines of code, without the need to dump the contents of ipconfig into a file.

    Have a go, now that you know which namespace you need. I can post my code if you want, but it would be better to learn yourself. It's actually quite simple.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How do you get stuck with a job that you're unqualified for? I'm guessing this is for work. Undoubtedly, you'd just steal our code and pass it off as your own.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comment each line and convert this C++ OOP code into C++ code.
    By Shayaan_Mustafa in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2011, 01:23 PM
  2. Convert ASM to C Code
    By clag in forum C Programming
    Replies: 2
    Last Post: 01-28-2010, 03:48 AM
  3. Convert a.out back to C code??
    By Rob4226 in forum C Programming
    Replies: 3
    Last Post: 09-16-2009, 10:33 PM
  4. convert vb.net code to C#
    By Nyoud33 in forum C# Programming
    Replies: 3
    Last Post: 01-11-2009, 03:29 PM
  5. Replies: 14
    Last Post: 04-01-2008, 02:23 AM