Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding to char array isn't working
    text
    copied!<p>I'm trying to read a text file line by line, and add each line to a char array. But the lines aren't added, at all.</p> <pre><code>//This is the default char array that comes with the cURL code. char *text[]={ "one\n", "two\n", "three\n", " Hello, this is CURL email SMTP\n", NULL }; /*Now we're going to replace that char array, with an array that holds the contents of a textfile. We'll read a textfile out line by line, and add each line to the char array. */ void makemailmessage() { text[0] = '\0'; //Clear text text[0] = "testy\n"; //First line in new char array //Read the text file, add each line to the char array. string line; ifstream myfile ("C:\\Users\\admin\\Downloads\\bbb.txt"); int counter; counter = 1; if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); //Convert the string variable "line" to a char (a) char *a=new char[line.size()+1]; a[line.size()]=0; memcpy(a,line.c_str(),line.size()); //Add \n to the end of "a" (new char will be "str") char str[80]; strcpy (str,a); strcat (str,"\n"); //Add "str" to the char array "text" text[counter] = str; text[counter+1] = "test\n"; //Also added this for testing purposes write_data("C:\\Users\\admin\\Downloads\\checkit.txt", str); //Also for testing purposes //Increase counter by 2 because we added two new items to the char array "text" counter++; counter++; } myfile.close(); text[counter-1] = "testy2\n"; //Ad another text line text[counter] = NULL; //End char array } </code></pre> <p>Each str is written correctly to checkit.txt but for some reason it is not added to the char array because I end up with the char array looking like this:</p> <pre><code>testy test test testy2 </code></pre> <p>What am I doing wrong?</p> <p><strong>UPDATE2:</strong> The reason I am trying to make a char array is because the cURL function I am using needs a char array to form the email body. This is the important part of the cURL code.</p> <pre><code>static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp) { struct WriteThis *pooh = (struct WriteThis *)userp; const char *data; if(size*nmemb &lt; 1) return 0; data = text[pooh-&gt;counter]; //This part is using the char array. if(data) { size_t len = strlen(data); memcpy(ptr, data, len); pooh-&gt;counter++; return len; } return 0; } </code></pre> <p><a href="http://pastebin.com/rtFDEKdh" rel="nofollow">Here's</a> the full code</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload