Read from text file to a string
I am implementing a functionality where I have to send emails through code and have to read the email subject and body from a text file.
This is how my text file looks like. The # switch indicates start of the message body.
I have implemented the code as
Product Notification Reminder
This is a reminder that the attached product notification tasks assigned to you are still open.
This mail has been automatically generated. Please direct further enquiries to the owner of the tasks.
I am sure there is a better way to write this code and optimize it further so please let me know your comments/suggestions.
void get_envelope_subject_body(static std::string &strEnvSubject, static std::string &strEnvBody)
FILE *fpEnvelopeConfig = NULL;
char *pcMessage = NULL;
char *pcSubject = NULL;
int iCount = 0;
bool isSubject = true;
if ((fpEnvelopeConfig = fopen("D:\\Configfile.txt", "r")) == NULL)
printf("Cannot open the envelope configuration file.");
pcSubject = (char*)malloc(sizeof(cBuffer));
pcSubject = '\0';
while (fgets(cBuffer, sizeof(cBuffer), fpEnvelopeConfig) != NULL)
if (isSubject == true && cBuffer != '#')
//end of subject, start of body
if (cBuffer == '#')
isSubject = false;
if (isSubject == false && cBuffer != '#')
pcMessage = (char*)realloc(pcMessage, iCount *sizeof(cBuffer));
if (iCount == 1)
pcMessage = '\0';
strEnvSubject = pcSubject;
strEnvBody = pcMessage;
Also, the code will be run in Unix whereas I am developing it in windows. So, is the code portable?