Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
something wierd in c++

 
Post new topic   Reply to topic Printable version
 View previous topic  make c++ read from ini files Post :: Post Open source client-server security  View next topic  
Author Message
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Thu Jul 24, 2008 7:46 pm    Post subject: something wierd in c++ Reply to topic Reply with quote

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 sa_tongue.gif )

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
Code: Show/Hide
#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
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:40
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3614
Location: Las Vegas
Offline

PostPosted: Thu Jul 24, 2008 8:30 pm    Post subject: Re: something wierd in c++ Reply to topic Reply with quote

Code: Show/Hide
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
View users profile Send private message Add User to Ignore List Send email
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Thu Jul 24, 2008 9:49 pm    Post subject: Reply to topic Reply with quote

Yeah you're right, I put the & because Dev-C++ wrote this
Code: Show/Hide
error: invalid conversion from `char' to `char*'
But I did try to make the char 80 characters but then Dev-C++ wrote this
Code: Show/Hide
cannot convert `char (*)[80]' to `char*
and this is how what I did
Code: Show/Hide
     char value[80];
     cin>>value;
I also tried
Code: Show/Hide
value[79]='\0';
But nothing changed, so how can I fixed it?
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Thu Jul 24, 2008 11:22 pm    Post subject: Reply to topic Reply with quote

Code: Show/Hide
#include <string>
#include <iostream>
using namespace std;

int main()
{
  string name;

  cout << "enter name: ";
  cin >> name;

  cout << "you entered " << name.c_str() << endl;

  return 0;
}

_________________
SubSpace Discretion: A Third Generation SubSpace Client
Back to top
View users profile Send private message Add User to Ignore List AIM Address
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Fri Jul 25, 2008 12:08 am    Post subject: Reply to topic Reply with quote

um this is not what i was trying to do bak, I was trying to get user input and put in in this funtion
Code: Show/Hide
WritePrivateProfileStringA(section,key,writeThis,path);
and writeThis must be a *char
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Fri Jul 25, 2008 12:57 am    Post subject: Reply to topic Reply with quote

name.c_str() is a char*
Back to top
View users profile Send private message Add User to Ignore List AIM Address
k0zy
Server Help Squatter


Gender:Gender:Male
Joined: Jan 11 2003
Posts: 571
Location: Germany
Offline

PostPosted: Fri Jul 25, 2008 1:01 am    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:40
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3614
Location: Las Vegas
Offline

PostPosted: Fri Jul 25, 2008 10:15 am    Post subject: Reply to topic Reply with quote

Code: Show/Hide
cannot convert `char (*)[80]' to `char*

When you called the writeINI, did you pass it &value or value ? Because you shouldn't be using & then, because you are already passing a pointer to a char array.

You might want to get a bit of understand about data types and pointers. Books at your local library for C/C++ programming can give you a lot of help with this.
Back to top
View users profile Send private message Add User to Ignore List Send email
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Fri Jul 25, 2008 12:23 pm    Post subject: Reply to topic Reply with quote

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
Code: Show/Hide
     string value;
     cin>>value;

     writeINI(section,key,value.c_str(),"A3_Config.ini");
It said
Code: Show/Hide
error: invalid conversion from `const char*' to `char*'
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
k0zy
Server Help Squatter


Gender:Gender:Male
Joined: Jan 11 2003
Posts: 571
Location: Germany
Offline

PostPosted: Fri Jul 25, 2008 1:11 pm    Post subject: Reply to topic Reply with quote

You have to cast the "const char *" into a "char *"

Code: Show/Hide
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.
Back to top
View users profile Send private message Add User to Ignore List
Samapico
No, these DO NOT look like penises, ok?


Joined: May 08 2003
Posts: 1252
Offline

PostPosted: Fri Jul 25, 2008 2:54 pm    Post subject: Reply to topic Reply with quote

Bob Dole.. Bob Dole... Bob Dole...... bob dole.... bob... dole.... wrote:
You have to cast the "const char *" into a "char *"

Code: Show/Hide
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.

Code: Show/Hide
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
View users profile Send private message Add User to Ignore List
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Fri Jul 25, 2008 4:03 pm    Post subject: Reply to topic Reply with quote

okay thanks both methods work I'm saving this page to my lesson folder icon_smile.gif
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
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
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 664 page(s) served in previous 5 minutes.

phpBB Created this page in 0.460723 seconds : 37 queries executed (91.1%): GZIP compression disabled