0

I have a http response which I have written in a file using following calls

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, responseCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, responseFile); 

static size_t responseCallback(void *ptr, size_t size, size_t nmemb, void *userdata)
{
   int written = fwrite(ptr, size, nmemb, (FILE *)userdata);
   return written;
}

Now I am trying to read back that data from file using

void getAccessToken()
{
  long len;
  char *buf;
  fseek(responseFile,0,SEEK_SET); //go to beg.
  fseek(responseFile,0,SEEK_END); //go to end
  len=ftell(responseFile); //get position at end (length)
  fseek(responseFile,0,SEEK_SET); //go to beg.
  buf=new char[len];
  fread(buf,len,1,responseFile); //read into buffer

      //some operation to be performed on buf

  delete [] buf;

}   

The operation that I want to perform is retrieving a particular token that I have received in the http response ( which could be done using strtok). I have been able to get the data, but at the end some junk characters are also appearing. Also, the size of the file is 1743 bytes(the same size of data is written by curl call back function) but the buf is being filled till 3510 bytes of data. How is this possible? Any clues ?

1 Answer 1

1

Is anything wrong that I am doing here ?

Yes, you're not keeping track of return values at all. I/O calls can fail, and you must check the return values before depending on the results.

1
  • Thanks unwind. I have been able to solve the basic problem of getting the junk data but the problem now that I am facing is data after performing operations is not correct
    – Ankuj
    Sep 21, 2012 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.