2

I was working on my project when I needed to use "curl" to obtain some data from www. Now firstly I tried direct system() function but it didn't worked, strangely everytime it corrupted the whole source code file while compiling with gcc. Luckily I was testing it separately. Then I tested execl() function, this code compiles OK and gcc gives me a .exe file to run, but nothing happens when I run it,blank windows appears. CODE:

    int main(){
        execl("curl","curl","http://livechat.rediff.com/sports/score/score.txt",">blahblah.txt",NULL);
         getch();
    return 0;
    }

Includes are not shown properly but I have included stdio,conio,stdlib and unistd.h. How can I get output of program to store in text file? Also running the above command creates and stores text file in My Documents, I want it to be in local directory from where I run the program. How can I do that?

vish213
  • 748
  • 3
  • 12
  • 27
  • If you are interested there is a second option for performing this particular operation. [libCurl](http://stackoverflow.com/questions/1636333/download-file-using-libcurl-in-c-c) Is there any particular reason you want to use `execl` to execute the curl command line utility? – Blackninja543 Aug 20 '12 at 14:59
  • It just seems simple, I am able to manually run curl from DOS and get output in text file, for learning purpose too, I would like to know what is that I am doing wrong?? Secondly I am not able to use libcurl properly on my system. gcc gives and error "cannot find -lcurl". So I was taking this alternative. – vish213 Aug 20 '12 at 15:06

2 Answers2

2

You need to provide the path of curl, and you cannot use redirection because the application will not be executed through bash. Instead use the -o flag and specify the filename. Also, execl does not return when successful:

#include <unistd.h>
#include <stdio.h>
int main(){
  execl("/usr/bin/curl",
        "curl","http://livechat.rediff.com/sports/score/score.txt",
        "-oblahblah.txt",NULL
  );
  printf("error\n");
  return 0;
}
perreal
  • 94,503
  • 21
  • 155
  • 181
  • I am on windows. curl is in a directory which is in list of Environment variable PATH. The above program is printing "error", I tried changing first parameter to "curl" instead of "/usr/bin/curl". – vish213 Aug 20 '12 at 15:13
  • 1
    change it with the absolute path of curl in your system such as 'C:\\dir/curl.exe' – perreal Aug 20 '12 at 15:14
  • 1
    It worked! Thnq :-) As always--stackoverflow=programmers heaven – vish213 Aug 20 '12 at 15:19
1

If you want your code to return, you should fork a child process to run the command. This way you can check the return code.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define CURL "/usr/bin/curl"

int main()
{
  pid_t pid;
  int status;

  pid = fork();

  if (pid == 0)
  {
    execl(CURL, CURL, arg1, NULL);
  }

  else if (pid < 0)
  {
    printf("Fork failed\n");
    exit (1);
  }

  else
  {
    if (waitpid(pid, &status, 0) != pid)
      status = -1;
  }

  return status;
}

arg1 is whatever argument you want to use with curl or if you aren't using any than you obviously can omit it.

squiguy
  • 32,370
  • 6
  • 56
  • 63
  • Anyways I loved your idea of #define. I really needed this. Just don't know why it didn't came to my mind. May be because I am coding while watching Eng vs SA,and its really getting interesting :-D – vish213 Aug 20 '12 at 15:27
  • @vish213 I just caught that, at least the other answer worked! – squiguy Aug 20 '12 at 15:27
  • I'll need to come on that too later. Just a little question for now,can I run a separate thread for this each time I want to update the scores? I mean use a for loop,sleep for some time(I will need to update score every one minute),then create a new thread-specially to run this execl()? – vish213 Aug 20 '12 at 15:38
  • I'm not too familiar with it, but my best guess would be to create a shell script that can run as a service on your machine, and sleep inside that script call your C program and continually. You will generally does this by creating an infinite loop. Sorry I can't help too much, I'm sure you could look around on the web for some examples. I can look too. – squiguy Aug 20 '12 at 15:42