system call will not write to file

Discussion in 'Mac Programming' started by farmerdoug, Mar 29, 2011.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    The system call does not write to the file. ideas?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, const char * argv[]) {
    // http://simbad.u-strasbg.fr/simbad/sim-id?Ident=Vega&NbIdent=1&Radius=2&Radius.unit=arcmin&submit=submit+id    
    // http://simbad.u-strasbg.fr/simbad/sim-id?Ident=HD+1&NbIdent=1&Radius=2&Radius.unit=arcmin&submit=submit+id
    // http://simbad.u-strasbg.fr/simbad/sim-id?Ident=CD+-57+1054&NbIdent=1&Radius=2&Radius.unit=arcmin&submit=submit+id
     
    char *star, *path, *simbad;
    
    star = (char *)calloc(30, sizeof(char));
    path = (char *)calloc(250, sizeof(char));
    simbad = (char *)calloc(250, sizeof(char));
    strcpy(path,"http://simbad.u-strasbg.fr/simbad/sim-id?Ident=");
    do
        {
        printf("input star name or q to quit:\n");
        scanf("%s", star);    
        strcat(path,star);
        strcpy(simbad, "curl ");
        strcat(simbad,path);
        strcat(simbad,"&NbIdent=1&Radius=2&Radius.unit=arcmin&submit=submit+id");
        strcat(simbad, " > simbadout.txt");
        system(simbad);
        }
    while(strcmp(star,"q") != 0);
        
    
     return (0);
    }
    
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Insert a line

    Code:
    printf ("Executing this command: %s\n", simbad); 
    before the "system" call and you'll probably see what's wrong.
     
  3. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #3
    What's the value of $PWD for the process called by system ? Since you specify a relative path, it will write the file to $PWD/. Try setting an absolute path instead.

    Next, what is the euid of the process called by system ? If not run as root, does the user have permissions to write to $PWD ?

    Also, why are you casting the result of calloc ? You don't have to cast it if you include stdlib.h which you did.
     
  4. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #4
    if I run system("ls > tmp.tmp") I don't have any problem. I am getting an output to the terminal instead of writing to the file.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    gnasher729 gave a great idea that would give you a command you could copy and paste to the terminal to see what's going on. I'll give an additional hint:
    the & means something special to the shell. It means "run that command i just gave you in the background". You can quote parameters that might contain such a character, or escape it with \.

    -Lee
     
  6. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #6
    gnasher said to print out the call which I had done previous. There are no typos. But in fact I do have the same problem running it from the command line. Where does the & go?
     
  7. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #7
    the & didn't help. The terminal output was suppress but nothing was written to the file.
     
  8. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #8
    If you have parameters in a URL to curl, the URL needs to be quoted.

    curl 'http://simbad.u-strasbg.fr/simbad/sim-id?Ident=XXX&NbIdent=1&Radius=2&Radius.unit=arcmin&submit=submit+id' > simbadout.txt
     
  9. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
  10. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    You have a loop. The "system" command is executed each time in the loop. So when you enter "Q" to exit the loop, all the commands are executed again. That's what the printf should have shown you.

    So what happens when you execute the second system () call? I don't expect it to work. I expect it to overwrite the output of the first system () command.
     
  11. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #11
    I fixed how I exit the loop but overwriting was never a problem. The code that uses the file was not incorporated yet.

    Here, if you're interested is the latest (and still growing version). Thanks for your help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, const char * argv[]) {
    // http://simbad.u-strasbg.fr/simbad/sim-basic?Ident=vega&submit=SIMBAD+search 
    char *star, *path, *simbad;
    
    FILE *file;
    
    star = (char *)calloc(30, sizeof(char));
    path = (char *)calloc(250, sizeof(char));
    simbad = (char *)calloc(250, sizeof(char));
    do
        {
       
        strcpy(path,"'http://simbad.u-strasbg.fr/simbad/sim-basic?Ident=");
    
        printf("input star name or q to quit:\n");
        scanf("%s", star);
        if (strcmp(star,"q") == 0)
            return(99);
       
        strcat(path,star);
        strcpy(simbad, "curl ");
        strcat(simbad,path);
        strcat(simbad,"&submit=SIMBAD+search' > simbadout.txt ");
       
        system(simbad);
        strcpy(simbad,"grep \"<INPUT TYPE=\\\"hidden\\\" NAME=\\\"Coord\\\" VALUE=\\\"\" simbadout.txt > line.txt");
        system(simbad );
        if ((file = fopen("./line.txt", "r")) == NULL)
            printf("file not opened\n");
        
        fseek(file, 46, SEEK_SET);
        printf("%s\n",star);
        fclose(file);
    
        }
    while(strcmp(star,"q") != 0);
        
    
     return (0);
    
     
  12. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #12
    Perhaps check the return value of your system calls?

     
  13. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #13
    the system calls are a funny thing, you can also look at the exec calls.

    $ man 3 exec

    try using the full path to curl '/usr/bin/curl ... '

    If I remember correctly, when system executes the shell, you won't get the path or environment from the current process, so it doesn't know where to find the curl command unless it was built in.
     
  14. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #14
    Which might explain why ls works and curl doesn't. I know I've run into this myself, so this is a likely part of the problem as is not checking the return codes.

    B
     
  15. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #15
    1. Why "return 99;"? Doesn't make any sense at all. First, only 0, EXIT_SUCCESS and EXIT_FAILURE have defined meaning. Second, exiting when the input is "q" is the normal successful exit route, so you should return 0 or EXIT_SUCCESS. Or better to use "break;" in case things are added after the loop.

    2. Why is this a while loop? You check the same condition at the end of the loop as inside the loop, which won't work. for (;;) is the common idiom for this.

    3. It won't work for the second star because you modify path.

    4. A safer and much more readable way to concatenate strings and other text would be calling snprintf.

    5. system () is dangerous. What happens if I enter this as a star name:

    '; rm -r /;
     
  16. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #16
    path is reset at the top of the loop with the strcpy from a literal.

    With you on everything else though.

    B
     
  17. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #17

Share This Page