|
Server Help Community forums for Subgame, ASSS, and bots
|
Author |
Message |
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Thu Jul 24, 2008 7:46 pm Post subject: something wierd in c++ |
|
|
|
|
well I started making a program "quick server set up" that would set up subgame server very quickly (also will help me learn about pointers )
so here is what program does, user puts it in the server folder, opens it and just types few values into the program that are needed to start server running.
here is the problem, when the program asks you to enter the name of the DefaultLevelFile I enter it, but for some reason it only saves to server.ini the first letter of what ever you typed, so if you typed in map.lvl it will say in server ini Quote: | DefaultLevelFile=m | why is that?
Here is the source code #include <windows.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
void c()
{
system("cls");
}
void p()
{
system("pause");
}
void writeINI(char *section, char *key, char *writeThis, char *path)
{
WritePrivateProfileStringA(section,key,writeThis,path);
}
void visual()
{
SetConsoleTitle("Quick Server Set Up");
system("color f0");
c();
}
void load(char *fileName)
{
CopyFile(fileName,"c:\\windows\\A3_Config.ini",false);
}
void save(char *fileName)
{
CopyFile("c:\\windows\\A3_Config.ini",fileName,false);
}
void config(string name, string hint, char *section, char *key)
{
c();
cout<<"\n\n\n\n\n\t\t\t["<<name<<" configuration]";
cout<<"\n\n\n\n\n\nAbout:\t"<<hint<<"\n\n\n\n\n\t\t\tEnter: ";
char value;
cin>>value;
writeINI(section,key,&value,"A3_Config.ini");
}
int main()
{
visual();
load("server.ini");
p();
config("DefaultLevelFile","DefaultLevelFile is the map that player will see when entering \n\tyour zone, below enter the name of the map file...","Misc","DefaultLevelFile");
save("server.ini");
} |
|
|
Back to top |
|
|
Mine GO BOOM Hunch Hunch What What
Age:40 Gender: Joined: Aug 01 2002 Posts: 3614 Location: Las Vegas Offline
|
Posted: Thu Jul 24, 2008 8:30 pm Post subject: Re: something wierd in c++ |
|
|
|
|
void writeINI(char *section, char *key, char *writeThis, char *path)
{
WritePrivateProfileStringA(section,key,writeThis,path);
}
char value;
cin>>value;
writeINI(section,key,&value,"A3_Config.ini"); |
You don't understand pointers/data types. char is one byte. If you wanted to make a string of bytes, you'd use char value[200] which would give you 200 bytes, but really only 199 to use safely (the last byte should be a \0 or NULL, otherwise all the strXXX and such will fail). Then, when you call writeINI() which accepts a char*, you only need to use writeINI(value) instead of writeINI(&value) which I assume you added the & because the compiler bitched at you.
Google can help with char arrays. |
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Thu Jul 24, 2008 9:49 pm Post subject: |
|
|
|
|
Yeah you're right, I put the & because Dev-C++ wrote this error: invalid conversion from `char' to `char*' | But I did try to make the char 80 characters but then Dev-C++ wrote this cannot convert `char (*)[80]' to `char* | and this is how what I did char value[80];
cin>>value; | I also tried But nothing changed, so how can I fixed it? |
|
Back to top |
|
|
Bak ?ls -s 0 in
Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
|
Back to top |
|
|
Bak ?ls -s 0 in
Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Fri Jul 25, 2008 12:57 am Post subject: |
|
|
|
|
name.c_str() is a char* |
|
Back to top |
|
|
k0zy Server Help Squatter
Gender: Joined: Jan 11 2003 Posts: 571 Location: Germany Offline
|
Posted: Fri Jul 25, 2008 1:01 am Post subject: |
|
|
|
|
hellzlaker wrote: | um this is not what i was trying to do bak |
What bak told you is exactly what you are trying to do!
You want to read in a char* (character array) but what you really do is reading a single character (hint: what mgb told you).
What bak did was reading a string and converting it into a char* using the method c_str().
So if you read it the way bak did, and pass stringx.c_str() to the WritePrivateProfileStringA it will work. _________________ It's a shark! Oh my god! Unbelievable! |
|
Back to top |
|
|
Mine GO BOOM Hunch Hunch What What
Age:40 Gender: Joined: Aug 01 2002 Posts: 3614 Location: Las Vegas Offline
|
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Fri Jul 25, 2008 12:23 pm Post subject: |
|
|
|
|
Yeah MGB you're I gotta understand it more, Im downloading couple e-books right now on c++,
to bak: Crap I didn't know that but even when i tried this string value;
cin>>value;
writeINI(section,key,value.c_str(),"A3_Config.ini"); | It said error: invalid conversion from `const char*' to `char*' |
|
|
Back to top |
|
|
k0zy Server Help Squatter
Gender: Joined: Jan 11 2003 Posts: 571 Location: Germany Offline
|
|
Back to top |
|
|
Samapico No, these DO NOT look like penises, ok?
Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Fri Jul 25, 2008 2:54 pm Post subject: |
|
|
|
|
Bob Dole.. Bob Dole... Bob Dole...... bob dole.... bob... dole.... wrote: | You have to cast the "const char *" into a "char *"
writeINI(section, key, const_cast<char *>(value.c_str()), "A3_Config.ini"); |
In C, you would have simply used (char *) to cast it, but above is the C++ way. | Would be simpler to change the header of the function to accept a const char*, actually.
void writeINI(const char *section, const char *key, const char *writeThis, const char *path)
{
WritePrivateProfileStringA(section,key,writeThis,path);
}
| You can pass char* to a function that takes const char*'s with no problem, the opposite does not work however.
What 'const' says is that the function is not allowed to modify the value pointed by the pointer. In this case, your function does not need to change any of the values, so it is good practice to make them const so you don't accidentally end up screwing up your values, which makes it hard to debug. _________________ (Insert a bunch of dead links here) |
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Fri Jul 25, 2008 4:03 pm Post subject: |
|
|
|
|
okay thanks both methods work I'm saving this page to my lesson folder |
|
Back to top |
|
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
Software by php BB © php BB Group Server Load: 1215 page(s) served in previous 5 minutes.
|