Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6788525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:29:46+00:00 2026-05-26T17:29:46+00:00

The program Reads out a textfile and emails it’s contents The original code (with

  • 0

The program

Reads out a textfile and emails it’s contents

The original code (with help of Sehe)

https://gist.github.com/1342118#file_test.cpp

The problem:

I am trying to run the application multiple times using threading, using a different textfile for each thread. This means I can no longer use a global vector because thread 3 may add to / alter this vector while thread 1 is still working on it. This means this line is becoming problematic:

curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

This calls this function:

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
{
  struct WriteThis *pooh = (struct WriteThis *)userp;

  if(size*nmemb < 1)
    return 0;

  if (pooh->counter < text.size())
  {
      const std::string& data = text[pooh->counter];

      memcpy(ptr, data.data(), data.length());
      pooh->counter++; /* advance pointer */
      return data.length();
  }
  return 0; /* no more data left to deliver */
}

As you can see it uses the globally defined vector “text”. I thought I could fix it by simply making the vector inside that function (read_callback) but since I’m using threading now, the filename is no longer hard coded so I end up having to pass a variable anyway.

Curl seems to fill in the variables that read_callback accepts automatically so how is this done?

Just to clarify, here’s what I have now:

std::vector<string> read_text(char* fname)
{
    std::ifstream myfile (fname);
    std::vector<string> text;

    std::string line;
    while (std::getline(myfile, line))
    {
        text.push_back(line + '\n');
    }

    return text;
}

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
{

    std::vector<string> text;
    text = read_text(textfilename);

  struct WriteThis *pooh = (struct WriteThis *)userp;

  if(size*nmemb < 1)
    return 0;

  if (pooh->counter < text.size())
  {
      const std::string& data = text[pooh->counter];

      memcpy(ptr, data.data(), data.length());
      pooh->counter++; /* advance pointer */
      return data.length();
  }
  return 0; /* no more data left to deliver */
}

But read_text doesn’t work because textfilename hasn’t been passed to the function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:29:47+00:00Added an answer on May 26, 2026 at 5:29 pm

    Read carefully again curl_easy_setopt‘s documentation. You might set the CURLOPT_READDATA to your textfilename and cast the userp to get it inside your read_callback

    edit

    Your userp is actually used; so you should add the textfilename in a new field of your WriteThis struct. Most callback functions take only one user data, but it should be enough (since you can pack arbitrary stuff in it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program reads emails (really just a .txt file structured like an email) and
Edit: figured it out, misplaced a piece of code I have a program that
My c# program reads an xml file when it loads, my installer installs the
My SSIS program reads as input from a .csv file. The file has about
I have a program that reads data from a file line-by-line. I would like
I have a program which reads an XML file using the DOM functions: $doc
I have a python program that reads floating point values using the following regular
After providing the same program which reads a random generated input file and echoes
I am writing a program that reads the input from a file and then
When running a Python program that reads from stdin, I get the following error:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.