reading string from char array

N

Neo

Hi,
I have a programs that reads char ouput and should send a signal when
it reads "login". I am currently reading into a char array but how do
I check for the match? I don't think I can do-
char x[5];
----
----
if(x=="login") {}

I cant use strings here as I get a char at a time.

Thanks,
Neo
 
J

Jim Langston

Neo said:
Hi,
I have a programs that reads char ouput and should send a signal when
it reads "login". I am currently reading into a char array but how do
I check for the match? I don't think I can do-
char x[5];
----

C way:
#include <string.h>
if ( strcmp( x, login ) == 0 ) {}

#include <cstring>
if ( std::strcmp( x, login ) == 0 ) {}

C++ way:
#include <string>
std::string Text(x);
if ( Text == "login" ) {}
 
F

FabioAng

Neo said:
Hi,
I have a programs that reads char ouput and should send a signal when
it reads "login". I am currently reading into a char array but how do
I check for the match? I don't think I can do-
char x[5];
----
----
if(x=="login") {}

I cant use strings here as I get a char at a time.

Thanks,
Neo

see strcmp() function

Fabio
 
R

Reetesh Mukul

Hi,
I have a programs that reads char ouput and should send a signal when
it reads "login". I am currently reading into a char array but how do
I check for the match? I don't think I can do-
char x[5];
I cant use strings here as I get a char at a time.
Thanks,
Neo

see strcmp() function

Fabio

const char login_[] = "login";
size_t i = 0;
char ch;
while(i!= 5)
{
ch = readchar(); //since you are reading char by char, for example
getchar()
if(ch == login_)
++i;
else
i = 0;
}
std::cout<<"\nLogin Done!";

You can try this code.

Regards,
RM
 
R

Rolf Magnus

Neo said:
Hi,
I have a programs that reads char ouput and should send a signal when
it reads "login". I am currently reading into a char array but how do
I check for the match? I don't think I can do-
char x[5];

Right. That would result in a pointer compare, i.e. the address of the first
element of the array x would be compared to the address of the first
element of the string literal.
To do a string compare on C style strings, you need to use strcmp. However,
I guess that's not what you need, since otherwise, your array would be too
short to ever match "login" (which includes a \0 string terminator). You
can use memcmp to check equality of a specific number of bytes.
I cant use strings here as I get a char at a time.

So? Just append your chars with push_back().
 
J

Jim Langston

Rolf Magnus said:
Neo said:
Hi,
I have a programs that reads char ouput and should send a signal when
it reads "login". I am currently reading into a char array but how do
I check for the match? I don't think I can do-
char x[5];

Right. That would result in a pointer compare, i.e. the address of the
first
element of the array x would be compared to the address of the first
element of the string literal.
To do a string compare on C style strings, you need to use strcmp.
However,
I guess that's not what you need, since otherwise, your array would be too
short to ever match "login" (which includes a \0 string terminator). You
can use memcmp to check equality of a specific number of bytes.
I cant use strings here as I get a char at a time.

So? Just append your chars with push_back().

or +=

std::string MyString;
char x = 'H';
MyString += x;
x = "i';
MyString += x;

MyString now contains "Hi"
 
O

Old Wolf

Neo said:
I check for the match? I don't think I can do-
char x[5];
if(x=="login") {}
I cant use strings here as I get a char at a time.

Huh? Use strings and put chars in the string as they arrive.
C way:
#include <string.h>
if ( strcmp( x, login ) == 0 ) {}

strcmp only works with C strings, i.e. null-terminated
arrays of char. x has dimension 5 so it cannot contain
'l','o','g','i','n','\0'. The memcmp() function would
probably be a more appropriate choice.
 
B

BobR

Old Wolf said:
Huh? Use strings and put chars in the string as they arrive.


strcmp only works with C strings, i.e. null-terminated
arrays of char.

Like std::string.c_str().

char const cLine[] = "login";
std::string Line("login");
if( strcmp( cLine, Line.c_str() ) == 0 ){
cout<<"The strings are equal"<<std::endl;
}
else{
cout<<"The strings are NOT equal"<<std::endl;
}

// out:The strings are equal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top