Thread: Just Can't Get This Code to Work

  1. #16
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    OK, thanks!

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Out of curiosity I've looked through your code and my alarms went off after seeing this part:
    Code:
    while(c)
      {
        printf("\n> ");
        char str[65536];
        gets(str);
    ...
    Please get rid of gets() as soon as possible. There is already enough broken code on this planet.
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    Bye, Andreas

    PS: I don't think writing a BASIC interpreter is a beginner's project.

  3. #18
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    PS: I don't think writing a BASIC interpreter is a beginner's project.
    This. Times 1000.

    Think about learning the at least the basics of the language (and really, you should have a good grasp of the entire language) before doing this.

  4. #19
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    Quote Originally Posted by rags_to_riches View Post
    This. Times 1000.

    Think about learning the at least the basics of the language (and really, you should have a good grasp of the entire language) before doing this.
    Yep, I know... I'm so used to languages with C syntax that the transition to C isn't all that bad.

    Anyways, I've got another question... I'm using this code (still not perfectly complete) to try to pass a command to the system.

    Code:
    else if(tok[0]=='s' && tok[1]=='t' && tok[2]=='a' && tok[3]=='r' && tok[4]=='t' && (strlen(tok)) == 5)
      {
      //printf ("chrs");
          char *q1 = strchr (comStr,'"');
          if(q1==NULL)
          {
              //char *q4 = strchr(comStr, " ");
              printf("Non-literal string encountered; code has not yet been implemented");
          }
              //if (q4==NULL)
              //{
                  //q4 = strlen(comStr);
                  //printf("Here");
                  //printf("%.*s", q4-q1-1, q1 + 1);
              //}
          //}
           // if not NULL, q1 points to the first quote
          if (q1) {
              char *q2 = strchr(q1 + 1, '"'); // if not NULL, q2 points to the second quote
          if (q2) 
          {
              //printf (("%.*s", q2-q1-1, q1));
              int result = (system (("%.*s", q2-q1-1, q1)));
              if(result == 32512)
              {
                  printf("Error: %.*s cannot be started", q2-q1-1, q1);
              }
              else if(result == 32256)
              {
              }
              else if(result == 0) return 1;
              else
              {
                  printf ("Error: An unknown error code %i was encountered", result);
              }
          } 
          }
          return 1;
          }
    Here's the program output.

    Code:
    Portable BASIC v1.39, rev2
    Copyright (C) Waterfront Software 2012
    
    Approx. 2130509825 bytes free to C
    > start "sh"
    app_166@android:/sdcard/C $ exit
    
    > start "ifconfig"
    
    > start "ifconfig eth0"
    /system/bin/sh: ifconfig eth0: not found
    Error: "ifconfig eth cannot be started
    >
    Obviously, the code isn't properly extracting the string. My question is: why? Almost the exact same code works on the print statement, but for some reason it fails on the start statement.
    Last edited by WMH; 09-19-2012 at 11:22 AM.

  5. #20
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by WMH View Post
    Code:
              int result = (system (("%.*s", q2-q1-1, q1)));
    I can guarantee that this line does not do what you think it does.

    This statement passes one argument to the system function. That argument is the result of the expression ("%.*s", q2-q1-1, q1), which is the last value (q1). The other operands to the comma operator ("%.*s" and q2-q1-1) are evaluated but their values are discarded. Since those operands have no side effects, the statement is the same as "int result = system(q1)".

  6. #21
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Obviously, the code isn't properly extracting the string. My question is: why? Almost the exact same code works on the print statement, but for some reason it fails on the start statement.
    The long and short of it is that system() and printf() are totally different functions, and don't take the same type of parameters. The mere fact that they both typically have some sort of string as a parameter doesn't give you carte blanche to do whatever you want with some double quotes. You should read the documentation for the functions you're using, or at least find a good tutorial.
    Quote Originally Posted by WMH View Post
    Yep, I know... I'm so used to languages with C syntax that the transition to C isn't all that bad.
    I'm pretty sure rags_to_riches was referring to the basics of how the language works, not merely it's syntax. You have some pretty fundamental issues to work out still, and a large-scale project like this isn't the place to do it, too many factors at play when there's a problem. Java has an almost identical syntax, and is a totally different language. Take for example your misunderstanding of the comma operator (see christop's reply in post #20 and mine in post #14 several days ago). It does not behave like the + operator in Java, which intelligently concatenates strings in the right format. Also, you appear unaware of extremely fundamental functions like strcmp. And what is up with the following:
    Code:
    int result = (system (("%.*s", q2-q1-1, q1)));
    if(result == 32512)
    ...
    else if(result == 32256)
    What are those hideous magic numbers? At the very least, you should define a constant. But better yet, read the documentation from your implementation for the system() function to see how to properly handle the return code and determine success or failure. For example, on my system (Linux), system() returns a value of the same format as the wait/waitpid funcitons. Use the macros you see in the waitpid man page (like WEXITSTATUS) if you are using Linux.

    One more thing, your indentation is pretty awful in that post. If your actual code is that messy, you don't stand much of a chance. If your code is clear and easy to read, then it's less likely you make mistakes, and easier to find and fix them when you do. If it's not that messy, but just posted that way, be kind to those helping you and reformat it so it's easy for us to read.

    Seriously, please take our advice, and spend a few weeks to really work on your C fundamentals, until you understand pointers, strings, arrays, function calls and all the basics (read: not just syntax). If you have lots of free time (part time job, light class load at school, etc), then you could probably learn what you need in a few long days to a week. Then, at the very least, you wont spend so much time waiting for replies to basic questions on forums or listening to us pontificate.
    Last edited by anduril462; 09-19-2012 at 04:58 PM. Reason: Add punctuation and a few words

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I work out the AWT and TAT in my code ?
    By spendotw in forum C Programming
    Replies: 0
    Last Post: 01-21-2012, 07:51 AM
  2. Why does this code work?
    By Whitewings in forum C Programming
    Replies: 8
    Last Post: 08-16-2011, 12:58 PM
  3. this code won't work
    By med linux in forum C Programming
    Replies: 7
    Last Post: 03-23-2011, 08:35 AM
  4. My Code Does Not Work? Why?
    By mintsmike in forum C Programming
    Replies: 8
    Last Post: 03-24-2009, 02:03 PM
  5. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM