help w/ while (or other better for this)

This is a discussion on help w/ while (or other better for this) within the Linux Programming forums, part of the Platform Specific Boards category; hi. i have a file "temp" just to store a number every time a x key is pressed, the number ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    18

    help w/ while (or other better for this)

    hi.

    i have a file "temp" just to store a number

    every time a x key is pressed, the number in file increase +1, and every time a y key is pressed, the number in file decrease -1, until 0 (0 is the minimal)

    this part is working.

    whats left:
    when the number in the file change from 1 to 0 the program must to execute a system command (a shellscript) and when the number in the file change from 0 to 1 the program must to execute another system command

    the scripts only must be executed when the number change from 0 to 1 and from 1 to 0 (not when change from 1 to 2, 3 > 4, 4 > 5, etc)

    but i dont know the best way to do this...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    if num == 0
    if num == 1
    How is that hard?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Can you describe your problem in more detail? Are you supposed to write a program which monitors this file, looking for changes from 0->1 or 1->0, and react accordingly?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    18
    yes, but isnt a program just for this, this must be a function inside the normal program.

    A function to monitor this file, looking for changes from 0->1 or 1->0, and react accordingly.

    In the same time the file change the content from 0->1, the program must to execute a system() to execute a shellscript, and in the same time the file change the content from 1->0, the program must to execute a system() to execute another shellscript

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and what part of this is it that you are not able to do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    18
    Quote Originally Posted by matsp View Post
    Yes, and what part of this is it that you are not able to do?

    --
    Mats
    this:

    A function to monitor this file, looking for changes from 0->1 or 1->0, and react accordingly.

    In the same time the file change the content from 0->1, the program must to execute a system() to execute a shellscript, and in the same time the file change the content from 1->0, the program must to execute a system() to execute another shellscript
    something like this:
    Code:
    infinite loop{
     num='cat ./num'
      if num == 0
       system("./script1");
      if num == 1
       system("./script2");
    }
    or

    Code:
    infinite loop{
     read file ./num
      if content of num == 0
       system("./script1");
      if content of num == 1
       system("./script2");
    }
    Last edited by fbs777; 05-26-2009 at 08:38 PM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You will need some kind of delay in your infinite loop or this will max your CPU out, which will have very negative and noticeable effects.

    Read up on sleep() or nanosleep().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, why don't you try to write a C program that does what you have just described in some sort of shell-script like way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21