Tuesday, 15 January 2013

shell - get wget file size in c program -


I really want to download the file size without downloading the file. That's why I

  wget --spider & lt; Url & gt; I use. 2 & gt; And 1. GRP length | And it works, but now my C program uses this shell code  
  int result = system (" Wget ..... ");   

However, the system returns the exit status code which means that instead of resizing the file, the result will be 0. If I still want to use wget --spider, how do I get this file size in my C programming code? Thanks in advance!

You would like to use the popen () command, which closes and opens a pipe Open up This function also takes one four pointer and one mode in your command. It then gives an indicator in the result of your order. Your code will look something like this.

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; Int main () {char * cmd = "wget ​​--spider>  2> and 1 | grep length | awk '{print $ 2}'"; Four buff [128]; FILE * ptr; If ((ptr = popen (cmd, "r")! = Null) {while (fgets (buf, 128, ptr)! = Null) printf ("% s", buf); } Paclose (PTR); } Return 0; }    

No comments:

Post a Comment