Sign in to follow this  
Fixxer

Need help adding onto a character array

Recommended Posts

I am trying to add .txt to the end of a character array but its not working. Here is my code. cUsername in the client code is limited to 17 characters. This is server code.
while(1){
		AcceptSocket = accept(m_socket,NULL,NULL);
		if(AcceptSocket != SOCKET_ERROR){
			cout<<"New Connection."<<endl;
		}
		int iBytesRecv = SOCKET_ERROR;
		char cRecvbuf[64] = "";
		iBytesRecv = recv(AcceptSocket,cRecvbuf,64,0);
		if(iBytesRecv > 0){
			char cUsername[21];
			char cPassword[18];
			strcpy(cUsername,cRecvbuf);
			strcpy(cPassword,&cRecvbuf[strlen(cRecvbuf)+1]);
			cout<<"Login Requested. Username:"<<cUsername<<" Password:"<<cPassword<<endl;
			int iStartTxt = strlen(cUsername) - 1;
			cUsername[iStartTxt] = ".";
			cUsername[iStartTxt + 1] = "t";
			cUsername[iStartTxt + 2] = "x";
			cUsername[iStartTxt + 3] = "t";
			cout<<cUsername<<endl;
			//ofstream a_file ();
		}

}

Errors
--------------------Configuration: CTA_Login_Server - Win32 Debug--------------------
Compiling...
main.cpp
C:\Documents and Settings\Owner\Desktop\Lightning Round\CTA_Login_Server\main.cpp(83) : error C2440: '=' : cannot convert from 'char [2]' to 'char'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Documents and Settings\Owner\Desktop\Lightning Round\CTA_Login_Server\main.cpp(84) : error C2440: '=' : cannot convert from 'char [2]' to 'char'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Documents and Settings\Owner\Desktop\Lightning Round\CTA_Login_Server\main.cpp(85) : error C2440: '=' : cannot convert from 'char [2]' to 'char'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Documents and Settings\Owner\Desktop\Lightning Round\CTA_Login_Server\main.cpp(86) : error C2440: '=' : cannot convert from 'char [2]' to 'char'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.

CTA_Login_Server.exe - 4 error(s), 0 warning(s)

Share this post


Link to post
Share on other sites
This is your problem:

cUsername[iStartTxt] = ".";
cUsername[iStartTxt + 1] = "t";
cUsername[iStartTxt + 2] = "x";
cUsername[iStartTxt + 3] = "t";

change it to this:

cUsername[iStartTxt] = '.';
cUsername[iStartTxt + 1] = 't';
cUsername[iStartTxt + 2] = 'x';
cUsername[iStartTxt + 3] = 't';

Problem: You are assigning string literals to a byte (index). That's impossible.
You need character assignment, not string literal assingment.

relient

Share this post


Link to post
Share on other sites
Quote:
Original post by stylin
I may be mistaken but "t" is two bytes (one for the null). Try 't' instead.


It is four bytes which point at a sequence of two bytes, really. (Not to mention, it's of a completely different type.)

With std::string we can do this much more easily, of course:


string userName(cRecvbuf);
// ugly hacking to find the second string hidden in your buffer
string password(cRecvbuf + strlen(cRecvbuf) + 1);

cout<<"Login Requested. Username:"<<userName<<" Password:"<<password<<endl;
userName += ".txt"; // see how easy?
cout<<userName<<endl; // I assume this is for debugging :s

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by stylin
I may be mistaken but "t" is two bytes (one for the null). Try 't' instead.


It is four bytes which point at a sequence of two bytes, really. (Not to mention, it's of a completely different type.)

To recap:

const char * string_literal; // so string literals are interpreted this way
const char * const string_literal; // or this way ?

Thanks for the clarification.

Share this post


Link to post
Share on other sites
Quote:
Original post by stylin
Quote:
Original post by Zahlman
Quote:
Original post by stylin
I may be mistaken but "t" is two bytes (one for the null). Try 't' instead.


It is four bytes which point at a sequence of two bytes, really. (Not to mention, it's of a completely different type.)

To recap:

const char * string_literal; // so string literals are interpreted this way
const char * const string_literal; // or this way ?

Thanks for the clarification.


Too bad he's wrong.

cout << sizeof( "hi" ) << endl; //result: 3

A string literal's type would be:

const char string_literal[ string_literals_size ];

The fact that this can be used as a (const char * [const]) could does not make it that type, only convertable to that type. Other evidence you may notice would be the referencing of 'char [2]' in the error messages (not 'char *' - 'char [2]') as well as the fact that given the option between a reference to an array or a reference to a pointer, the compiler will choose the reference to an array:

#include <iostream>
using namespace std;

template < typename value_t , size_t size >
void what_is_it( const value_t (&) [ size ] )
{
cout << "An array of size " << size << endl;
}

template < typename value_t >
void what_is_it( const value_t * )
//or: what_is_it( const value_t * const )
//or: what_is_it( const value_t * & )
//or: what_is_it( const value_t * const & )
{
cout << "A pointer" << endl;
}

int main () {
what_is_it( "string literal" ); //result: An array of size 15
}



[Edited by - MaulingMonkey on September 5, 2005 5:28:16 PM]

Share this post


Link to post
Share on other sites
Oops. Sorry, I knew that.

So... if a guy with a 1700+ rating can mess this kind of stuff up, why aren't *you* making things easy on yourself and using std::string? ;)

Share this post


Link to post
Share on other sites
Because Im working with winsock also.

Ive changed it to what you said, but now it couts this:

Fixxer.txt╠╠╠╠╠╠╠╠╠╠╠╠╠╠Fixxer

Share this post


Link to post
Share on other sites
btw, you also have an off-by-one. You shouldn't be subtracting 1 on the line below.
Also, cUsername may be too small, so you have a likely buffer overflow as well.
			char cUsername[21];

int iStartTxt = strlen(cUsername) - 1;
Try using
strcat(cUsername, ".txt");
instead as that's much simpler.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this