Thread: file existance getting screwy on me...

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    file existance getting screwy on me...

    This is a hashed down version, but shows what I'm doing syntax wise:

    Code:
    repeatmenu:
    // code to display options
    // switch stuff
        case '1':
        if ((system("Pgm.exe >NUL") == 0))
        {
         printf("There was an error!?!?!?\nblabla....");
         getch();
        }
        else
        {
         // Program executed fine.
         // do any records / log file work here, or nothing at all
        }
        goto repeatmenu;
        break;
    Obviously I have the default, and escape cases, I just wanted to concentrate on the if statement. I want to check for errors upone execution of "this program". If it does produce an error I want to surpress it. I want to supress any output at all from the dos program. (since I'm familar with the pgm) Under the possible circumstances I know what errors will occur how, so I can trap them, surpress them, then display my own.

    Yes, I know.........WHY?!?!?!?!?
    Just cause.

    Anyhow,
    Under that conditions i'm running the program, it's acting up on me. It executes the program fine ( displaying no error messages ), then when your done with the program and it bombs you back to dos.......it displays the error message...why? I've done this before and am familar with it, I must be missing something...
    The world is waiting. I must leave you now.

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    system() returns the value returned by the command interpreter. It will only return 0 if the command interpreter itself returns zero - in other words this is not guarenteed.

    Under that conditions i'm running the program, it's acting up on me. It executes the program fine ( displaying no error messages ), then when your done with the program and it bombs you back to dos.......it displays the error message...why?
    The return value for an error from system() is -1.

    so by my reckoning, this:

    if ((system("Pgm.exe >NUL") == 0))

    should read this:

    if ((system("Pgm.exe >NUL") == -1))

    If you need to know what the error is from system() you will need to check the value of errno.

    Using VC++ the return values could be:

    E2BIG - Argument list (which is system-dependent) is too big.

    ENOENT - Command interpreter cannot be found.

    ENOEXEC - Command-interpreter file has invalid format and is not executable.

    ENOMEM - Not enough (valid) memory is available to execute command
    Last edited by foniks munkee; 02-15-2002 at 06:47 PM.

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Ok - I have re-read your post and see that you are talking about it printing the error from DOS and not from if statement.

    If the DOS program (pgm.exe), is making of use of perror() to print the error messages - then you wont be able to supress it using a redirect to NULL.

    If you run this code from the DOS command line and try running it without suppressing the output, then with the redirect you will see what I mean.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	printf("\n\nThis is text 1\n");
    	fprintf(stdout, "\n%s\n", "This is text 2");
    	perror("This is an error!");
    
    	return 0;
    }

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    here

    Ok,

    Go here.

    That is a page on my web site that I setup (temporarily) for anybody who's been viewing this post, or thinks they can help.
    Instead of trying to get things straight with everybody who's nice enough to help, I'll just let you see and expierence it for yuorself. It's happening in person, with you, and on your computer. That's much easier to solve. I have it all explained on that page.

    I had it working before.
    I remember it was an insanely small amount of code.
    I sincerely thank anyone who refreshes my memory, cause it's driving me bonkers.
    The world is waiting. I must leave you now.

  5. #5
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Allright. I will admit - I haven't downloaded the programs, but it looks like you are trying to redirect stderr.

    if ((system("Pgm.exe 2> NULL") == 0))

    will do this under DOS - I have no idea how to redirect both stdout and stderr at the same time under DOS however. Much easier under *NIX.

    But I still think you should be testing the result of system like such..

    if ((system("Pgm.exe 2> NULL") == -1))

  6. #6
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Downloaded the program and yes using 2> works.. But system keeps returning 255 regardless of whether there is a problem with the file or not.

    This is what MSDN has to say re: system()
    It returns the value 0 only if the command interpreter returns the value 0. A return value of – 1 indicates an error, and errno is set to one of the following values:
    Help?

  7. #7
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    dam

    Ok, that didn't help. I guess I'll never remember the way I had it before. I know it was rare / not used often / etc, but it worked just fine.

    IF program encounters error during startup and bombs back to dos = printf("error message");

    IF program runs fine = // nothing

    I don't remember, (grrrr), I have been searching boards, temp e-mail locations, etc, etc, where I may have posted this entire source code, or where it may be stored.....nothing. All I know is it was:

    Code:
    if(system("mamepp 1942 >NUL") == 0 )// 0 might not be right
    // Null is used to make the mamepp program silent, and this if statemt is used to redirect the expected error into a simple message
    {
        // if an error occured and the program didn't start
    }
    else
    {
        // if the program ran fine
    }
    It was insanely simple.

    Since I can't figure that out (that bugs the heck outta me), is there another way to do it?

    You are running a comman line program and surpressing all of it's output. This program will display the same thing everytime while loading. You are surpressing it's output to replace it's output with your own loading message. If an error occurs, any kind at all, from that program, you display a message of your own (which means you'd have to make sure you know which error will occur in the conditions you'd be running the program....which I do). If it doesn't encounter an error during the loading proccess of the program, the if statement does nothing cause everything went fine.

    Lamen terms:
    Code:
    if (the program errors)
    {
        // display an error message
    }
    else
    {
       // the program didn't error, do nothing
    }
    What gets to me, is I had it working before just fine, and now, I can't do anything. What is even worse, was it was simlpe! Now I may have to use miles of code to do something that should have an easy proccess.
    The world is waiting. I must leave you now.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The first thing you need to do is work out whether the programmers of the program you're trying to run are 'int main' or 'void main' programmers.

    Because if its the latter, you're sunk

    Here's a short batch file to check what the exit status is
    Code:
    @echo off
    prog.exe
    IF ERRORLEVEL 1 echo Gak!!!
    If it ever prints "Gak" for any normal program exit, or doesn't print "Gak" for any fail exit, then you're dealing with a bunch of void main programmers who don't know how to return a proper status back to the calling environment (your program in this case).

    Ok, test time
    1. compile this program as mystat.exe
    Code:
    #include <stdio.h>
    int main ( int argc, char *argv[] ) {
        int status = atoi( argv[1] );
        printf( "Returning a status of %d\n", status );
        return status;
    }
    2. compile this program as teststat.exe
    Code:
    #include <stdio.h>
    int main ( ) {
        char cmd[100];
        int i,res;
        for ( i = 0 ; i < 10 ; i++ ) {
            sprintf( cmd, "mystat.exe %d", i );
            res = system( cmd );
            printf( "Actual status=%d, should be %d\n", res, i );
        }
        return 0;
    }
    If you don't get
    Returning a status of 0
    Actual status=0, should be 0
    Returning a status of 1
    Actual status=1, should be 1
    etc,

    Then something is broke

  9. #9
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    SALEM!!!

    Thank you Salem for helping me...

    The first thing you need to do is work out whether the programmers of the program you're trying to run are 'int main' or 'void main' programmers.

    Because if its the latter, you're sunk

    Here's a short batch file to check what the exit status is

    code:--------------------------------------------------------------------------------
    @echo off
    prog.exe
    IF ERRORLEVEL 1 echo Gak!!!
    --------------------------------------------------------------------------------


    If it ever prints "Gak" for any normal program exit, or doesn't print "Gak" for any fail exit, then you're dealing with a bunch of void main programmers who don't know how to return a proper status back to the calling environment (your program in this case).
    Despite the fact that I changed gak!!! to dam, this works. I realize the test too. When I use it to test for a successful execution, It does not print dam. When I use it to test for a failed execution, it DOES print dam. So this program IS returning values to the operating system, or my program in this case (which is what I predicted ). One of the developers involved (team of 100) with this program, is pro. The others all contribute to an emulator that suuports 3000 arcade machines (think of ALL the assembly conversions in that 6,000+ lines of source code...DAM).

    I'm going to take your reply step-by-step.
    The first test worked, just to be sure, that returning of prog.exe's status was the purpose of this test, right?

    What were the other tests for (as I run them...)?
    The world is waiting. I must leave you now.

  10. #10
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Ok, the first test with the batch file worked.

    -edit-

    Ok, the 2nd program calls the first one. The 2nd one needs to be ran and the first one doesn't have to be touched.

    This program returns the status of 0 to YOUR computer. It then shows you, your status, and what the status should be. The status should be what the program returned. If the program returned 956,000 ( i know, just an example ), that's what your status should be.

    No matter what status the program returns, my status is always 0.

    This is no longer a test with the program I'm calling, right? It returns status' fine because that's what we tested with that batch file. It is now something with my computer (so to speak) right?

    Atleast were getting somewhere (running these programs I've said, "ooooh, i see!' way too many times).
    Last edited by Shadow; 02-16-2002 at 03:14 AM.
    The world is waiting. I must leave you now.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > returning a status of 1
    > actual status=0, should be 1
    This means your system() function is not doing what you wanted.

    You know mystat.exe is returning 1, because you told it to, but somehow, system() decided to return 0 instead.

    It's OK for system() to return 0 in this case (it is implementation defined after all) - it's just not helpful to you.

    Basically, you can't use your version of system() to get at the return status of the program you ran.
    My version of system() does do this.

    Time to look at the spawn functions.

    > so I should only run the 2nd one.
    Yes, that's correct.

  12. #12
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Basically, you can't use your version of system() ( what does that mean? ) to get at the return status of the program you ran.
    My version of system() does do this.
    Would you make this more specific please?

    Time to look at the spawn functions.
    I'm reasearching right now, source codes, web sites, etc.
    What will that do? What will I need to do to apply usefulness in my overall purpose.

    What gets me is, "my system" used to do this fine. Now, it's like something is very WRONG. What could be the possiblities? Spawn functions, hm, ok...
    The world is waiting. I must leave you now.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Would you make this more specific please?
    This is what the standard has to say
    Description
    2 If string is a null pointer, the system function determines whether the host
    environment has a command processor. If string is not a null pointer, the system
    function passes the string pointed to by string to that command processor to be
    executed in a manner which the implementation shall document; this might then cause the
    program calling system to behave in a non-conforming manner or to terminate.
    Returns
    3 If the argument is a null pointer, the system function returns nonzero only if a
    command processor is available. If the argument is not a null pointer, and the system
    function does return, it returns an implementation-defined value.
    > What could be the possiblities?
    Have you changed your OS or compiler, or upgraded either of them?

    > What will that do? What will I need to do to apply usefulness in my overall purpose
    Approximately,
    result = system( "mamepp 1942");
    is this
    result = spawnl( P_WAIT, "mamepp.exe", "mamepp.exe", "1942", NULL );

    However, redirecting output is going to be a little trickier using this approach.

  14. #14
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > What could be the possiblities?
    Have you changed your OS or compiler, or upgraded either of them?
    As you see (thanks for continuing this mess), I'm trying, I really am. I've been searching posts, searching for everything I've been involved with at this site or any other since I can remember, and nothing.

    To your question:
    No. When I originally made this program, I was using the same ver of Win I'm using now. Win98SE. I am using the same exact brand, and _even_ version number of the compiler. Turbo C++ 1.01. I got it to work with this setup, and it was simple at that.

    I remember at first when I was writting the program, (in a very "messy" way) I would check for errrors just like that batch file did. I would make my program write a temp batch file in the background, execute it for the error checking, then delete it (for the next rewrite which would check existance on another proccess). This was later cultured out and replaced by _this_ new system, which now would allow me to write a log file if there was an error (batch files cannot do this). If the program executed fine, I would write that in the log file, if the program had a problem loading the zip file through the external program, I would also log that. Like I said, with one little, simple if statement, I could poke, stab, and strangle a program in any way I wanted, and redirect it's output messages to a way of my choosing.

    Everything is the same as before (setup wise, -as so it seems- ). This leads me to my next question. Since I "believe" my system, compiler, yadda yadda yadda, is alllll setup how I had it last time, when this simple if statement worked, is it at all possible that I changed something -internally- somewhere in the operating system or something, by a "for fun" programming mistake? Like, I permantly "marked" something....SOMEWHERE, that is now preventing me from doing this?

    For instance,
    I could write a program to check for the existance of notepad, and take action. Depending on how I checked for notepad though, could deny access to it from Windows. I have done this before by accident. I had to put a new version of notepad on my computer (re-copy it...)because I checked for the existance of it in the wrong way. A programming "boo boo". Because of that, notepad was now messed up and wouldn't run.

    Would this at all be the case? It's all I can think of. You had me run mystat.exe, and that got me thinking about this. That simple little if statement worked before, and now it doesn't. This isn't programming anymore I think. Do you think that could be it?

    That would mean re-installing something, I think. Possibly (oh boy) re-installing Windows & the compiler or something...
    The world is waiting. I must leave you now.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Heh, I don't think you're going to like this....

    If you have this
    &nbsp;&nbsp;&nbsp;&nbsp;system("foo.exe");
    then system runs foo.exe directly, and the status which status returns is the exit status of foo.exe

    However, when you have
    &nbsp;&nbsp;&nbsp;&nbsp;system("foo.exe > nul");
    Because you have redirection, it forces system to use the command interpreter, and runs something like this instead.
    &nbsp;&nbsp;&nbsp;&nbsp;system("command.com /c foo.exe > nul");
    where command.com is determined by the COMSPEC environment variable.

    There are now two (nested) child processes - the command interpreter (command.com) and foo.exe (with it's stdout redirected).

    Now for the bad news
    The exit status which system gets is the exit status of command.com (which is always 0), not the exit status of foo.exe (which is now running as a child of command.com). As far as command.com is concerned, it successfully ran the command.

    The tests
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( ) {
        char cmd[100];
        int i,res;
    
        // this forces DJGPP to use the crippled command.com to handle
        // io redirection (normally, its handled internally)
        __system_flags &= ~__system_redirect;
    
        for ( i = 0 ; i < 10 ; i++ ) {
            sprintf( cmd, "mystat.exe %d > nul", i );
            res = system( cmd );
            printf( "Actual status=%d, should be %d\n", res, i );
        }
        return 0;
    }
    With __system_flags &= ~__system_redirect (ie, use command.com), I get
    Actual status=0, should be 0
    Actual status=0, should be 1
    Actual status=0, should be 2
    Actual status=0, should be 3
    Doh!!!

    Without __system_flags &= ~__system_redirect, (use an internal redirector), I get
    Actual status=0, should be 0
    Actual status=1, should be 1
    Actual status=2, should be 2
    Actual status=3, should be 3
    Woohoo!!!

    NOTE - __system_flags is for DJGPP only. Whether your compiler has similar magic to modify the behaviour of redirection in system() calls would require some digging in the documentation on your part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM