login about faq


Hello, so basically, I'm trying to make a command line application, and if you type "on", it turns on the function I have specified... which I will not disclose. And if you type "off", it turns it off. I need to do something like this, I'll do this in Objective-C, as that's currently the only way I know how to do it, but I have no idea what the text line in the Terminal is called.

  if ([[textField stringValue] isEqualToString:@"on"])

        // Statements here

  }

  if ([[textField stringValue] isEqualToString:@"off"])

        // Statements here

  }

Now, how would I be able to do that in C for a command line application?

Thanks for any help!

-UPDATE-

Basically, I have answered below the correct answer to this question. I would like to keep this question open, so that wandering viewers may ask questions if they have any. Please do not close it, as it will just be a waste of both of our time, as I will just reopen it. Thank you.

asked Sep 24 '10 at 21:12

AppleHack23's gravatar image

AppleHack23
(suspended)

edited Sep 24 '10 at 23:58


   /* I was reading through my C for Dummies book and I found the right answer. Simple, don't know why I didn't               think of that. Anyway, for people who were wondering, the correct code would be as follows: */

   // Our project name will be called "CExampleProject".

   #include <stdio.h> // Why do we include that? That's a file filled with APIs we need!

   int main() // This is what's going to run as the process first loads.
   {

          char option; // char means character, so it's a phrase with characters. option is our character, that's what we        defined.

          printf("Please type YES to continue, or NO to terminate the program.\n"); /* When we use print, in printf, it        doesn't               exactly mean printing from a printer. Definition: an indentation or mark left on a surface or soft        substance by pressure. <--- That's basically saying that print means to mark. The "\n" that we use at the end, is letting        it know that something different is going to happen. The "\" alerts it and prepares it for a unconventional phrase, such        as a letter that represents a key not able to be keystroked. The "n" means the return, or enter key. */

          scanf("%s" option); // The %s is for a text field, or something similar to a text field to also be included in the        action.

          if (option=='YES') { /* That's our condition, that says right there that if our option field, or our imaginary field,        has the text "YES", as the only text in the field, then it will continue on to the statement listed next. */
              printf("Okay, you chose to continue.\n");
              // And then of course you can insert some more code right here.
          }

          if (option=='NO') { /* Now we're saying that if the only text in option is "NO", then we are going to run the        statement that follows. */
              printf("Okay, you chose to quit. Exiting in 5 seconds.\n");
              exit(5); /* the exit(); function tells the process or application to terminate. in the parentheses is where you        would put the duration before it finally executes. */
           }

           else {             /* else means whatever else happens, something not specified. Above we have targeted items,        here is for everything else. */
               printf("Invalid argument.\n");
               system("./CExampleProject"); // We're just going to pretend you've already cd into the folder holding the        file.
               // What we did here, is we made it run the whole process over again. Kind of like a loop.
               // And yes, I know... system(); isn't always the best way to run shell commands... :p
            }
   return(0); // Always do this at the end of your code!! It's very important!!
   }

   /* And now our code is finished... Now, Makefiles are another thing! :p But make sure to save this as        CExampleProject.c */

   // We have not made the executable yet... this is just the source file.

   // This was intended to be done on a Mac.

answered Sep 24 '10 at 23:47

AppleHack23's gravatar image

AppleHack23
(suspended)

edited Sep 24 '10 at 23:52

there are several issues with this code:

char option; << this is not declared as an array, which can cause memory management issues...

scanf("%s",option); << some compilers will allow this, but it is good practice to use a &before option(meaning store this information in the memory location), and dont forget the comma

if (option=='YES') << use the strcomp() some compilers will not allow you to do this directly (in fact i have not found one that will)

also know that when you call system it is an operating system dependent command and may not work on all operating systems

(Sep 25 '10 at 00:24) trueb trueb's gravatar image

Read the last comment in the code. :p

(Sep 25 '10 at 08:08) AppleHack23 AppleHack23's gravatar image

i get that it is designed to run on mac, but a good programmer will try to avoid running shell commands period, however some times shell commands make a program easy to create (and compact) for insistence in the answer i submitted i used system("pause"), this is because for some reason any version of windows newer than 2000 will try to exit out of the application as soon as it can...

(Sep 25 '10 at 10:46) trueb trueb's gravatar image

Well, for the project I am using it for, I need to run system commands to power on and off things. :p

(Sep 25 '10 at 10:52) AppleHack23 AppleHack23's gravatar image

here is a quick solution

/*command line input C
 *Tony Rueb 9-24-10
 *Copyright 2010 Tony Rueb
 *tonyrueb.com*/

// Includes
#include<stdio.h>

//function prototypes

//functions
int main(void)
{
    char command[25];//declare command
    int i;
    for(i=0;i<25;i++)// sets everything to null
    {
        command[i] = 0;
    }
    printf("Created by Tony Rueb\n\nCopyright 2010 Tony Rueb\n\n");
    //get input
    printf("Please select on or off: ");//prompt user
    scanf("%s",&command);//get input

    //check input
    if (!strcmp(command, "on"))//check to see if string is what you want
    {
        printf("\n\nfunction is on\n\n");//do some thing
    }
    else if (!strcmp(command, "off"))//check to see if string is what you want
    {
        printf("\n\nfunction is off\n\n");//do some thing
    }
    else
    {
        printf("\n\nInvalid input\n\n");
    }
    system("pause");//windows dependent command
    return 0;
}

answered Sep 25 '10 at 00:16

trueb's gravatar image

trueb
14.9k4999256

edited Sep 25 '10 at 00:25

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported


Join Us in the Chat Room

Tags:

×222
×27
×20
×17
×13
×10
×2
×1
×1

Asked: Sep 24 '10 at 21:12

Seen: 1,484 times

Last updated: Sep 25 '10 at 10:52