Hi all,
I wrote myself a little program, just to prove a concept to myself, which came back with very unexpected and varied results. Could someone take the time to explain these findings to me?
Test 0
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main()
{
cout << "Computer Name : " << system("uname -n") << endl;
output:
MyComputerName
Computer Name : 0
Test 1
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main()
{
char hostName[128] = system("uname -n");
cout << "Computer Name : " << hostName << endl;
output:
test.cpp:12: error: array must be initialized with a brace-enclosed initializer
Why does this error occur everytime I try to initialize a variable with a function?
I also noticed somewhere whilst browsing the web for this problem, that #include <cstdlib> seems to be used instead of (stdio/stdlib).h Is this correct?
Test 2
#include <iostream>
#include <string>
using namespace std;
int main()
{
FILE *hostName;
hostName = popen("uname -n","r");
pclose(hostName);
cout << "Computer Name : " << hostName << endl;
output:
Computer Name : 0xbbd010
Test3
#include <iostream>
#include <string>
using namespace std;
int main()
{
char hostName[128];
cout << "Computer Name : " << gethostname(hostName,128) << endl;
output:
Computer Name : 0
And finally!
Test4
#include <iostream>
#include <string>
using namespace std;
int main()
{
char hostName[128];
gethostname(hostName, 128);
cout << "Computer Name : " << hostName << endl;
output:
Computer Name : MyComputerName
yay, it did what it was meant to do :)
The main concepts learnt seemed to be:
1) I can't initialise variables with functions
2) I can't call a function in the middle of a cout line and expect to get away with it.
Are these correct? And is there some reference that explains the functions in the common/standard C++ libraries? i.e. how do I find out what functions do what?
And now as it relates to TinyMud,
After all this testing, I added the following code:
globals.cpp
char hostName[128];
globals.h
extern char hostName[128];
comms.cpp
gethostname(hostName,128);
p* << "Welcome to TinyMud Server << VERSION << " running on " << hostName << "\n";
Is this the most efficient/correct way of implementing this?
And one final question: Is there a neater way of shutting down TinyMud, as opposed to killall tinymudserver ?
Sorry if this post is convoluted and lacking in logical progression, my mind is erratic at best.
Thanks heaps!
I wrote myself a little program, just to prove a concept to myself, which came back with very unexpected and varied results. Could someone take the time to explain these findings to me?
Test 0
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main()
{
cout << "Computer Name : " << system("uname -n") << endl;
output:
MyComputerName
Computer Name : 0
Test 1
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main()
{
char hostName[128] = system("uname -n");
cout << "Computer Name : " << hostName << endl;
output:
test.cpp:12: error: array must be initialized with a brace-enclosed initializer
Why does this error occur everytime I try to initialize a variable with a function?
I also noticed somewhere whilst browsing the web for this problem, that #include <cstdlib> seems to be used instead of (stdio/stdlib).h Is this correct?
Test 2
#include <iostream>
#include <string>
using namespace std;
int main()
{
FILE *hostName;
hostName = popen("uname -n","r");
pclose(hostName);
cout << "Computer Name : " << hostName << endl;
output:
Computer Name : 0xbbd010
Test3
#include <iostream>
#include <string>
using namespace std;
int main()
{
char hostName[128];
cout << "Computer Name : " << gethostname(hostName,128) << endl;
output:
Computer Name : 0
And finally!
Test4
#include <iostream>
#include <string>
using namespace std;
int main()
{
char hostName[128];
gethostname(hostName, 128);
cout << "Computer Name : " << hostName << endl;
output:
Computer Name : MyComputerName
yay, it did what it was meant to do :)
The main concepts learnt seemed to be:
1) I can't initialise variables with functions
2) I can't call a function in the middle of a cout line and expect to get away with it.
Are these correct? And is there some reference that explains the functions in the common/standard C++ libraries? i.e. how do I find out what functions do what?
And now as it relates to TinyMud,
After all this testing, I added the following code:
globals.cpp
char hostName[128];
globals.h
extern char hostName[128];
comms.cpp
gethostname(hostName,128);
p* << "Welcome to TinyMud Server << VERSION << " running on " << hostName << "\n";
Is this the most efficient/correct way of implementing this?
And one final question: Is there a neater way of shutting down TinyMud, as opposed to killall tinymudserver ?
Sorry if this post is convoluted and lacking in logical progression, my mind is erratic at best.
Thanks heaps!