Thread: stdlib.h system()

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    86

    stdlib.h system()

    This question is about the funtion system in stdlib.h

    I'm utilising the unix command file from within a c program

    all the command does is tell whether or not an item is a file or not

    my question is whether or not it is possible to run this in the background rather than in the terminal

    by this i mean, I don't want the information about whether or not something is a file to go to the terminal, I want to store it in a variable.

    I know that system will return an integer value based on whetehr the command ran or not, but I need a specific value as returned, to terminal or otherwise, to be stored in a variable within my program.

    any ideas?

  2. #2
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Couldn't you just go like this:
    Code:
    FILE *f1 = fopen(item);
    if(!f1)
        puts("The item is not a file");
    fclose(f1);
    f1 = NULL;

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    that does make sense actually

    thanx

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Quote Originally Posted by Kleid-0
    Couldn't you just go like this:
    Code:
    FILE *f1 = fopen(item);
    if(!f1)
        puts("The item is not a file");
    fclose(f1);
    f1 = NULL;
    So when fopen() fails, what does fclose(NULL)?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by rmps
    So when fopen() fails, what does fclose(NULL)?
    *chukle* Yeah, you don't really want to do that. Plust there's no real point to setting your file pointer to null anyway. I mean, either way if you try to use it after it's closed or after it's failed you're screwed.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    Code:
    FILE *f1 = fopen("file_path");
    if(!f1) {
        printf("The item is not a file");
    } else {
     printf("File exists!"); 
     fclose(f1);
    }
    Happy?
    [/CODE]

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You seem to be under the impression that we care if you know how to fix some one else's code. We don't. You also seem to be under the impression that no one should point out flaws with other people's code, or you get you have a hissy fit and try to show up everyone. Again, that isn't the case, and no one cares about you.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    Q - does that apply universally?

    [blah]
    FWIW - it's usually good practice to set a pointer to NULL when you know you're done with it. Causes fewer really puzzling problems (other than a GPF or segfault) later on if you inadvertantly try to use it again.

    Also in the unix world when you only want to determine a file's existence and you don't plan on really opening it, a better choice is to call stat(). This is because stat only fiddles with the file system, and doesn't go through a lot of mumbo-jumbo creating kernel mode data structures if the file does exist.

    I don't know what Windows does now, but for 16 bit windows pretending to be 32 bit, stat() was the better choice too.
    [/blah]

  9. #9
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by quzah
    You seem to be under the impression that we care if you know how to fix some one else's code. We don't.
    The care bears care! lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 01:04 PM
  2. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 07:56 PM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  4. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM