我正在制作一个用于搜索外部数据文件(测试电子邮件)的课程。此方法“fill_email”是从 .txt 中提取字符到节点的字符数组中,直到字符 '|' 出现。1)我的代码需要一个临时指针,我想知道我是否可以跳过并直接将字符直接放入节点中?2)我的代码还需要一个需要重复 5 次的 while 循环(每个字段一次),有没有什么方法可以为我做一个 while 循环来改变这些字段?PS 我的代码需要动态分配的数组,我肯定把这个方法放在我代码的私有部分。
电子邮件结构的标题部分及其节点:
struct email { // struct for storing email data
char * sent; // date and time email was sent
char * from; // email address of sender
char * to; // email address of reciever
char * subject; // subject line of email
char * contents; // contents of email
};
struct email_node { // node for storing email data and next
email email_data;
email_node * next;
};
从外部数据文件填充节点指针的实现。
int Classify::Fill_email(email_node* & node, char filename[]) {
char* sent_temp = new char[25]; // These are the five temporary arrays for extracting from the
char* from_temp = new char[50]; // external data file and then strcpying to the dynamic character
char* to_temp = new char[50]; // arrays in the node.
char* subject_temp = new char[100];
char* contents_temp = new char[500];
int size = 0;
ifstream source_file(filename);
//I hate copy/pasting, but I really wasn't sure how to do this for each field otherwise.
while(source_file.good()) { // Reads until source_file is empty
sent_temp[size] = source_file.get(); // It should extract the text from my external file "Sample_Emails.txt"
if (sent_temp[size] == '|')
break;
size++; // and put them into a temporary array for the field
}
sent_temp[size] = '\0'; // So that the character array ends properly.
size = 0; // Just for the next while loop
//
while(source_file.good()) // Goes on for 4 more variables copy pasted....
email_head->email_data.sent = new char[strlen(sent_temp)]; // For initialziing the field
strcpy(node->email_data.sent, sent_temp); // Copying the data from the temporary arrays to the fields in the node
//
delete sent_temp;
//
if(!(source_file.good())) // If the external data file is empty, return 1
return 1;
return 0;
//
};