1.11M Members

Combine 2 programs (curl and popen)

 
0
 

Hello,

I need help to combine a c-program using curl(to retrieve txt files from internet) and another c-program using popen. currently the popen is using the command ls -l (to list the current folder in terminal).
I have 2 files, the first one is curl program,
which have to retrieve 2 text files from the internet and process them later(I haven't make the manipulation process in the code).

#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#define WEBPAGE_URL "http://www.rfc-editor.org/rfc/rfc2578.txt"
#define DESTINATION_FILE "out.txt"
#define WEBPAGE_URL2 "http://www.gnu.org/licenses/lgpl.txt"
#define DESTINATION_FILE2 "out2.txt"
size_t write_data( void *ptr, size_t size, size_t nmeb, void *stream)
{
 return fwrite(ptr,size,nmeb,stream);
}
 
int main()
{
 FILE * file = fopen(DESTINATION_FILE,"w+");
 FILE * file2 = fopen(DESTINATION_FILE2,"w+");
 if(!file){
 perror("File Open:");
 exit(0);
 }
 CURL *handle = curl_easy_init();
 curl_easy_setopt(handle,CURLOPT_URL,WEBPAGE_URL);
 curl_easy_setopt(handle,CURLOPT_URL,WEBPAGE_URL2);
 curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION, write_data);
 curl_easy_setopt(handle,CURLOPT_WRITEDATA, file);
 curl_easy_setopt(handle,CURLOPT_WRITEDATA, file2);
 curl_easy_perform(handle);
 curl_easy_cleanup(handle);
}

and the other is popen program,
which currently is listing the contents in the folder through terminal.

#include <stdio.h>

int main()
{
  FILE *fp;
  char line[130];
   
  fp = popen("ls -l", "r");		

  while ( fgets( line, sizeof line, fp))
  {
    printf("%s", line);
  }
  pclose(fp);
}

Now the thing is, I need to combine these 2 programs, so that only using 1 program files, I can retrieve txt files using curl as the command in popen, and can manipulate the retrieved txt files later on.

When I run the 2 program separately, it has no errors. but when I tried to combine and compile the code, it has some errors.
Here's my try:

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#define WEBPAGE_URL "http://www.rfc-editor.org/rfc/rfc2578.txt"
#define DESTINATION_FILE "out.txt"
#define WEBPAGE_URL2 "http://www.gnu.org/licenses/lgpl.txt"
#define DESTINATION_FILE2 "out2.txt"

size_t write_data( void *ptr, size_t size, size_t nmeb, void *stream)
{
 return fwrite(ptr,size,nmeb,stream);
}

int main()
{
	FILE * file = fopen(DESTINATION_FILE,"w+");
	FILE * file2 = fopen(DESTINATION_FILE2,"w+");
	if(!file){
	 perror("File Open:");
	 exit(0);
	 }
	 CURL *handle = curl_easy_init();
	 curl_easy_setopt(handle,CURLOPT_URL,WEBPAGE_URL);
	 curl_easy_setopt(handle,CURLOPT_URL,WEBPAGE_URL2);
	 curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION, write_data);
	 curl_easy_setopt(handle,CURLOPT_WRITEDATA, file);
	 curl_easy_setopt(handle,CURLOPT_WRITEDATA, file2);
	 curl_easy_perform(handle);
	 curl_easy_cleanup(handle);
	
	FILE *fp;
	char line[130];
   
	fp = popen("handle", "r");

  while ( fgets( line, sizeof line, fp))
  {
    printf("%s", line);
  }
  pclose(fp);
}

and here's the error I got in the terminal during the compile:

Undefined symbols for architecture x86_64:
  "_curl_easy_init", referenced from:
      _main in ccsZ6bsW.o
  "_curl_easy_setopt", referenced from:
      _main in ccsZ6bsW.o
  "_curl_easy_perform", referenced from:
      _main in ccsZ6bsW.o
  "_curl_easy_cleanup", referenced from:
      _main in ccsZ6bsW.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Can anyone help me? I'm totally lost here.

 
0
 

Well, the error you are seeing is due to the fact that you haven't linked against the cirl library for your system (on linux-like systems this would look something like -lcurl as a parameter to the compiler).
Also, popen takes a program name to execute as if you were going to type it on your command line. Passing it a pointer from within your running program will likely not give you the results you desire. You will have to drop the curl setup code (the curl_ stuff) and figure out how to do that from the command line version.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: