Hello everyone,

first little explanation about what i want my program to do:
i use wget to download a webpage but instead of downloading it into a file i want the webpage sourcecode in a variable so that i can parse it and put it in a database later.

Now the problem:
After allot of googling i figured i should use pipe() fork() exec() to do the wget than get the results from wget trough the pipe to the parent process and go from there.
as you can see in my code below the wget is no problem but i just cant seem to get all the data from the pipe into a variable for parsing.
i tried a million different ways but cant get it to work.
can anyone help me/point me in the right direction?

Im very new to c++ (first try) so if there are things im doing wrong or if there's a better way to achieve what i want please let me know!

int main(int argc, char *argv[])
{
    pid_t nPid;
	char page;
	// Make pipe for communication between parent and child process
    int p[2];
    if ( pipe( p ) != 0 ){ perror( "pipe()" ); exit(255); }
	
	// fork() to get child process
    nPid = fork();
    if ( nPid < 0 ){ perror( "fork()" ); exit(255); }
    else if ( nPid == 0 ) {    // CHILD PROCESS
        close(p[0]);
        if (p[1] != STDOUT_FILENO){
            if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO) cerr << "dup2 error to stdout" << endl;
            close(p[1]);
        }
        execlp( "wget", "wget", "--output-document","-","robots=off","-q","http://www.google.com", NULL);
		exit(255);
	} else {    // PARENT PROCESS
        // put result from wget in var page;
        cout << "page to parse is: \n" << page;
		return(0);
	}
	
    //fflush( stdout );
    return( 0 );
}

Thanks in advance!

found it..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.