Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,774 members, 7,809,997 topics. Date: Friday, 26 April 2024 at 06:28 PM

Needs C/C++ Help With Cleaning Up Codes For A Sendmail Application - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Needs C/C++ Help With Cleaning Up Codes For A Sendmail Application (1215 Views)

Programming FUN With Code Snippets!! -C++,C#, Java,php,python Or Any Language!! / Simple Code Challenge: C#, Java, C, C++ / Is C# Better Than C/C++? (2) (3) (4)

(1) (Reply) (Go Down)

Needs C/C++ Help With Cleaning Up Codes For A Sendmail Application by Nobody: 5:16pm On Jun 17, 2012
I created this application some years ago to help me test out sending out of email in my php programs.

How It Works:
- It allows you to make use of the PHP mail function on your local test server
- It pipes the mail into a local database on the system
- I now created a php/mysql application for checking the emails, deleting and all that

Status: It works just fine, but i want to make it easy for others to download and use

Reason for thread:
- The codes are dirty, i did much of hardcoding because i am not so good with c/c++, i am more of a webmaster, but use vb6.0 best
- But i wanted something lightweight and fast with as little dependency as possible.


Platform: Codes with CodeBlocks


Source code:

[img]http://tools..net/v/OzVvPiEgLC0qMSI/OA==[/img]




The program is called from a pipe by the wamp server, this means that external programs and files need their
full pathnames.

Problem 1 - line 14
- Here i need to open output.txt which lies inside the same folder as the executable.
This means that i need a code that will be get the absolute app of where the app is running.

Problem 2 - line 20
I need to call the save.bat with the absolute path of the application similar to problem 1.
Re: Needs C/C++ Help With Cleaning Up Codes For A Sendmail Application by lordZOUGA(m): 9:20pm On Jun 17, 2012
can you please say exactly what it is you are trying to do. you seem to be trying to write a certain string into "output.txt" that is located relatively to the folder the executable is in. for that, just use the name of your file.

here is what I'd do in C++

#include <iostream>
#include <fstream>

/*Assuming you are trying to write a character of size BUFFERSIZE into
a file called "output.txt"*/
using namespace std;
#define BUFFERSIZE 1
int main()
{
char buffer[BUFFERSIZE];

fstream myFile;//create handle for the file stream
myFile.open("output.txt", ios::out);//open it and set the mode to output

if(myFile.is_open()){//check if file is open
myFile << buffer;//print to file
}

myFile.close();//close stream

system("aBatch.bat"wink;
return 0;
}
Re: Needs C/C++ Help With Cleaning Up Codes For A Sendmail Application by Nobody: 2:23am On Jun 18, 2012
Now i tried it that way, but for some reasons, i suspect it is because of the way the sendmail executable is called, writing to output.txt without using a full path causes it to use an entirely different folder rather than the application folder - that is why i need the full path.

After searching again, i just got these codes, i will try it shortly and see the results.
#include <iostream>
#include <fstream>

#include <stdio.h>
#include <unistd.h>
#include <string.h>

using namespace std;

int main (int argc, char **argv) {

char the_path[256];

getcwd(the_path, 255);

printf("%s\n", the_path);
}
Re: Needs C/C++ Help With Cleaning Up Codes For A Sendmail Application by Nobody: 2:26am On Jun 18, 2012
Even that did not work,


#include <iostream>
#include <fstream>

#include <unistd.h>
#include <string.h>


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "process.h"


#include <stdio.h>
#include <stdlib.h>

#define BUFFERSIZE 1

using namespace std;

int main (int argc, char **argv) {

char the_path[256];
getcwd(the_path, 255);


unsigned char buffer[BUFFERSIZE];
int bytes_read=0;
int buffer_size=0;


FILE * myfile;


myfile = fopen (strcat(the_path, "\\output2.txt"wink,"w"wink;
myfile = fopen ("/wamp/sendmail/output.txt","w"wink;


buffer_size=sizeof(unsigned char)*BUFFERSIZE;
/* open stdin for reading */
while((bytes_read=fread(&buffer, buffer_size, 1, stdin))==buffer_size){
fprintf(myfile, "%c", buffer[0]);
}
//close output file
fclose(myfile);

system("/wamp/sendmail/save.bat"wink;
}

When i searched for the location of the output2.txt - i discovered it was located in the apache folder.

c:/wamp/bin/apache/apache2.2.11/apache.exe is the one that is calling the pipe on c:/wamp/sendmail/sendmail.exe
So i need a code that will help me to get the path c:/wamp/sendmail/ - if this were to be a visual basic 6 program, app.path will have done it.

But here, it seems the current working directory belongs to the piping program, so i am still stuck here. Mission Impossible 5.
Re: Needs C/C++ Help With Cleaning Up Codes For A Sendmail Application by Nobody: 2:52am On Jun 18, 2012
I tried again using windows.h and this time it appears to work:


#include <iostream>
#include <fstream>

#include <unistd.h>
#include <string.h>


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "process.h"

#include <tchar.h>
#include <Windows.h>

#include <stdio.h>
#include <stdlib.h>

#define BUFFERSIZE 1

using namespace std;

int main (int argc, char **argv) {

char the_path[256];
getcwd(the_path, 255);


unsigned char buffer[BUFFERSIZE];
int bytes_read=0;
int buffer_size=0;


FILE * myfile;

TCHAR szEXEPath[2048];
GetModuleFileName ( NULL, szEXEPath, 2048 );


myfile = fopen (strcat(szEXEPath, ".txt"wink,"w"wink;
//myfile = fopen ("/wamp/sendmail/output.txt","w"wink;


buffer_size=sizeof(unsigned char)*BUFFERSIZE;
/* open stdin for reading */
while((bytes_read=fread(&buffer, buffer_size, 1, stdin))==buffer_size){
fprintf(myfile, "%c", buffer[0]);
}
//close output file
fclose(myfile);

//system("/wamp/sendmail/save.bat"wink;
system(strcat(szEXEPath, ".bat"wink);
}

Yipee, next i need to know how to load a remote url from inside the program without opening a browser, i want to eliminate the call to the batch file.
Re: Needs C/C++ Help With Cleaning Up Codes For A Sendmail Application by Nobody: 4:34am On Jun 18, 2012
I was able to use the curl stuff to handle that. Next, i will clean up the php/mysql codes a bit, and start testing it across the popular local servers:-
wamp, xamp and others. . .

1 Like

Re: Needs C/C++ Help With Cleaning Up Codes For A Sendmail Application by lordZOUGA(m): 5:57am On Jun 18, 2012
seems you fixed it urself. Gud for u
Re: Needs C/C++ Help With Cleaning Up Codes For A Sendmail Application by Nobody: 9:37am On Jun 18, 2012
Thanks, i have been trying to do this particular fix for months, but it was not that straight forward at all. Thanks for volunteering to help.

(1) (Reply)

Host A Wordpress / Job Vacancy For .net Programmer / How To Turn One Topic Into Six Blog Posts

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 21
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.