Need help adding onto a character array

Started by
9 comments, last by iMalc 18 years, 7 months ago
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)

Advertisement
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

I may be mistaken but "t" is two bytes (one for the null). Try 't' instead.

EDIT: 3 seconds xllx_relient_xllx
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
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 bufferstring 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
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 wayconst char * const string_literal;     // or this way ?

Thanks for the clarification.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
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 wayconst 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]
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? ;)
Because Im working with winsock also.

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

Fixxer.txt╠╠╠╠╠╠╠╠╠╠╠╠╠╠Fixxer
bump
so? Just call c_str() when you need to.

This topic is closed to new replies.

Advertisement