Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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);
}
 
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);
}

Insert a line

Code:
printf ("Executing this command: %s\n", simbad);

before the "system" call and you'll probably see what's wrong.
 
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.
 
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.
 
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
 
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?
 
the & didn't help. The terminal output was suppress but nothing was written to the file.
 
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
 
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?

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.
 
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.

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);
 
Perhaps check the return value of your system calls?

int system(const char *command);

The system() function returns the exit status of the shell as returned by waitpid(2), or -1 if an error occurred when invoking fork(2) or waitpid(2). A return value of 127 means the execution of the shell failed.
 
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.
 
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 /;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.