添加到 char 数组不起作用
我正在尝试逐行读取文本文件,并将每一行添加到字符数组中。但根本没有添加这些行。
//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
}
每个 str 都正确写入 checkit.txt 但由于某种原因它没有添加到 char 数组中,因为我最终得到的 char 数组如下所示:
testy
test
test
testy2
我做错了什么?
更新2: 我尝试创建 char 数组的原因是因为我使用的 cURL 函数需要 char 数组来形成电子邮件正文。这是 cURL 代码的重要部分。
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 < 1)
return 0;
data = text[pooh->counter]; //This part is using the char array.
if(data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
pooh->counter++;
return len;
}
return 0;
}
这是完整代码
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.
//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
}
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:
testy
test
test
testy2
What am I doing wrong?
UPDATE2:
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.
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 < 1)
return 0;
data = text[pooh->counter]; //This part is using the char array.
if(data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
pooh->counter++;
return len;
}
return 0;
}
Here's the full code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,在对此进行了更多讨论之后,这里有一个修复:
C++ 版本
完整的代码文件在这里: https ://gist.github.com/1342118#file_test.cpp
将相关代码替换为:
纯C版本
完整代码文件在这里:https://gist.github.com/1342118#file_test.c
替换
为
将其挂入你的
main
函数,例如像这样或者,如果你愿意,只需调用
read_text("C:\\Users\\admin\\Downloads\\bbb.txt")
:)为了真正完成事情,不要忘记在完成后回收内存 - 正确:
Okay, after chatting on this a bit more, here is a fix:
C++ version
Full code file here: https://gist.github.com/1342118#file_test.cpp
Replace the relevant code with:
Pure C version
Full code file here: https://gist.github.com/1342118#file_test.c
Replace
With
Hook it up in you
main
function, e.g. like soOr, if you so prefer, just calling
read_text("C:\\Users\\admin\\Downloads\\bbb.txt")
:)To really top things off, don't forget to reclaim memory when you're done - properly:
既然这是 C++,为什么不使用
std::vector
并使用 std::string 版本的 getline?std::string
类将关注保存任意长度的字符串所需的内存,而std::vector
类将关注保存任意长度的字符串所需的内存。可以说,保存一个字符串“数组”。编辑:实际上再次查看您的代码,您确实使用了
std::string
,然后分配内存将其存储为char
数组s,然后将指向这些字符串的指针存储在某个固定大小的数组test
中。正如我上面提到的,当您可以使用std::vector
来保存所有std::string
对象时,为什么要这么麻烦呢?头脑=困惑。EDIT2: 你不能也使用 cURLpp 作为 cURL 的 C++ 包装器吗?我没用过,所以无法评价它的效果。
Since this is C++, why not use an
std::vector<string>
and use the std::string version of getline?The
std::string
class will look after the memory needed to hold a string of any sort of length, and thestd::vector
class will worry about the memory needed to hold an "array", so to speak, of strings.EDIT: Actually looking at your code again, you do use an
std::string
and then allocate memory to store it as an array ofchar
s, and then store pointers to those strings in some fixed sized array,test
. Why go to all that trouble when, as I mentioned above, you can use anstd::vector<string>
to hold all yourstd::string
objects? Mind = boggled.EDIT2: Couldn't you also use cURLpp as a C++ wrapper for cURL? I haven't used either so I can't comment on the effectiveness of it.
我做错了什么?
首先,这个:
str
是在堆栈上分配的,具有块范围的作用域。然后将该指针输入到具有更大作用域的数组中。这通常会导致灾难——令人印象深刻的分段错误或平台上的任何类似错误。在这种情况下,由于它在循环中使用,您的程序将崩溃,或者 - 如果星星具有正确的对齐方式 - 您最终将得到数组中的所有指针都指向相同的超出范围的字符串,即最后读过的那一篇。
当您已经在堆中动态分配了
a
时,为什么还要陷入这种麻烦呢?顺便说一句,将
char[]
数组(以及相关的标准 C 库函数)与 C++ 字符串混合并不是一个好主意。甚至都不是一个可以接受的。好吧,这是一个坏主意。只需坚持 C++ 字符串...What am I doing wrong?
For one, this:
str
is allocated on the stack, with block-wide scope. Then you enter that pointer in an array with a greater scope. This is usually a recipe for disaster - a rather impressive segmentation fault or whatever the equivalent is on your platform.In this case, due to its use in a loop, your program will either crash, or - if the stars have the proper alignment - you will end up with all pointers in your array pointing to the same out-of-scope string, namely the one that was last read.
Why do you even go into that trouble, when you have already dynamically allocated
a
in the heap?By the way, mixing
char[]
arrays (and the associated standard C library functions) with C++ strings is NOT a good idea. Not even an acceptable one. OK, it a bad idea. Just stick to C++ strings...