Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
110 user(s) are online (69 user(s) are browsing Forums)

Members: 1
Guests: 109

davec555, more...

Headlines

 
  Register To Post  

C functions Left/Right for string usage
Quite a regular
Quite a regular


See User information
Hi,

I try to develop my own Left/Right functions for C but when I use them, they crash my app...

Here are the two functions :
char *strgetrightchar *sourceint lentoread ){
  
char teststring;
  
int txtlen =strlensource );
  
// Si le texte source est plus grand que le texte cible
  
if ( txtlen lentoread ){
    
source source + ( txtlen lentoread );
    
strcpyteststringsource );
   }
  
// Si le texte source est de la meme dimension que la cible demandee
  
if ( txtlen == lentoread ){
    
strcpyteststringsource );
   }
  
// Si le texte source est moins long que la cible demandee
  
if ( txtlen lentoread ){
   
teststring "";
   }
  return &
teststring;
 }

char *strgetleftchar *sourceint lentoread ){
  
char teststring;
  
int txtlen strlensource );
  
// Si le texte source est plus grand que le texte cible
  
if ( txtlen lentoread ){
    
strncpyteststringsourcelentoread );
    
teststring teststring "\0";
   }
  
// Si le texte source est de la meme longueur que la cible demandee
  
if ( txtlen == lentoread ){
    
strcpyteststringsource );
   }
  
// Si le texte source est moins long que la cible demandee
  
if ( txtlen lentoread ){
    
teststring "";
   }
  return &
teststring;
 }

All we have to decide is what to do with the time that is given to us.
Go to top
Re: C functions Left/Right for string usage
Quite a regular
Quite a regular


See User information
@freddix

Ouch you are strcpy'ing a string into a char !! This should have thrown warning in gcc. Then even if you would change teststring from char to char* this would still crash as before strcpy'ing into a string you should assure yourself that the target string is large enough to hold the data (i.e. you should allocate it accordingly before).

Back to a quiet home... At last
Go to top
Re: C functions Left/Right for string usage
Supreme Council
Supreme Council


See User information
Apart from copying to the wrong type of receiver, you should consider strlcpy() which checks the length, and only copies a certain amount of characters so as to avoid writing beyond the buffer.

One other point, if you are going to post code into an English forum, you might want to do something about the Italian comments. :)

Simon

Go to top
Re: C functions Left/Right for string usage
Amigans Defender
Amigans Defender


See User information
and also use this:

char *strgetright( const char *source, int lentoread )

to avoid write strings

i'm really tired...
Go to top
Re: C functions Left/Right for string usage
Amigans Defender
Amigans Defender


See User information
@Rigo

they arre french comments..

i'm really tired...
Go to top
Re: C functions Left/Right for string usage
Supreme Council
Supreme Council


See User information
@afxgroup

See, instant confusion :)

Simon

Go to top
Re: C functions Left/Right for string usage
Quite a regular
Quite a regular


See User information
@Rigo
ok I'll remove comments next time ;)

I've found an easier way to do the left/right functions but now I get problem for comparizon ...
char szBuffer1024 ];

void clearszBuffervoid ){
  
int iLoop;
  for( 
iLoop 0iLoop 1024iLoop++){
    
szBufferiLoop ] = "\0";
   }
 }


strgetleftchar szStrint nLeft ){
  
int iLoop;
  
clearszBuffer();
  if ( 
nLeft strlenszStr ) ){
    for( 
iLoop iLoop nLeftiLoop++){
      
szBuffer[iLoop] = szStr[iLoop];  // copie szStr dans szBuffer jusqu'a ce que i = nleft
     
}
   }
  
szBuffer[iLoop +] =  "\0";
}

strgetrightchar szStrint nRight ){
  
int iLoop;
  
clearszBuffer();
  
int nPos + ( strlenszStr ) - nRight );
  for( 
iLoop nPos iLoop strlenszStr ); iLoop++ ){
    
szBuffer[iLoop nPos ] = szStr[iLoop];
   }
  
szBufferiLoop +] = "\0";
 }


Theorically, it should work.

but if I try to compare : it with for example :

if( szBuffer ".mrw" ){

it does not work ...

All we have to decide is what to do with the time that is given to us.
Go to top
Re: C functions Left/Right for string usage
Just can't stay away
Just can't stay away


See User information
@freddix

Quote:
if( szBuffer = ".mrw" ){

First of all you forgot the second equal sign and it doesn't work that way in C language. In C you can only compare two memory pointers that way and not any string content.

Rock lobster bit me - so I'm here forever
X1000 + AmigaOS 4.1 FE
"Anyone can build a fast CPU. The trick is to build a fast system." - Seymour Cray
Go to top
Re: C functions Left/Right for string usage
Just popping in
Just popping in


See User information
@freddix

A little test on the nright value (like nLeft in the first function) is necessary if you don't want to have crash.

As rigo wrote, you must use 2 equals.
For comparison, you can write a comparison function which compares each char one by one until it is false or it the end of the word.
I will give you one way to do that.
I code this very rapidly so don't compile that directly.
I don't like C code, I prefer C++ so verify the initializations and bool type and min function.


bool compare(firstword, secondword, sz)
char *firstword, *secondword;
int sz;

s1=strlen(firstword);
s2=strlen(secondword);
int i;
bool continue=true;

if ((s1<>s2) || (sz <0)) return false;
for (i=0;(i<min(s1,sz)) && continue;i++)
if (firstword[i]!=secondword[i]) continue=false;

return continue;



Hope that can help you.

Go to top
Re: C functions Left/Right for string usage
Not too shy to talk
Not too shy to talk


See User information
@freddix

Quote:

if( szBuffer ".mrw" ){

it does not work ...


Using a single = in C doesn't work like that. It's an assignment, meaning that it copies ".mrw" into the variable szBuffer in your code. And usually it ends up being true because the assignment was successful.
if (== 3) {

is how you compare two things. However, you can't compare strings like that in C as they're effectively an array of variables, not just a single variable. You need to use the strcmp() function, which will return 0 if they're identical (be careful with that!)

Go to top
Re: C functions Left/Right for string usage
Not too shy to talk
Not too shy to talk


See User information
@freddix

I dont want to be rude, but your comprension of
the C language actually it's a bit to low,
you actually cannot understand how comparision
and assignement works in C, so you have so many
problems with your code. I suggest you to read/study
a good C language manual.

Sorry for the rant, but I am sure you can have
much satisfaction knowing the language better.

P.S. it's not a good idea to use a global
variable for the use you want to, because
if you invoke yours' function more than once
the result it's overwritten.

Go to top
Re: C functions Left/Right for string usage
Just can't stay away
Just can't stay away


See User information
@Daedalus

Quote:

Quote:

if( szBuffer = ".mrw" ){

Using a single = in C doesn't work like that. It's an assignment, meaning that it copies ".mrw" into the variable szBuffer in your code. And usually it ends up being true because the assignment was successful.


This is wrong. It will not "copy" ".mrw" into szBuffer. What will happen is that a pointer to the string literal ".mrw" will be assigned to szBuffer. That the expression will always be true is correct, but not because the assignment is "successful" but because the value being assigned is not a 0 or a NULL pointer and is therefore regarded as true in the C language.

To copy a string you need to use the strcpy() or the strlcpy() function.

Quote:

is how you compare two things. However, you can't compare strings like that in C as they're effectively an array of variables, not just a single variable. You need to use the strcmp() function, which will return 0 if they're identical (be careful with that!)


For a case insensitive string comparison (might be more appropriate in this case) one should use the stricmp()/strcasecmp() function.

strcmp() does a case sensitive string comparison (i.e. ".MRW" is not the same as ".mrw" or ".MrW").

Go to top
Re: C functions Left/Right for string usage
Quite a regular
Quite a regular


See User information
@freddix

First of all your code only works for strings smaller than 1023 characters. Then using a global variable to return a result is generally something to be avoided, you should better allocate a new string and return it from your functions.

And last everyone told you you can't compare strings using '==' in C, but noone tought to tell you that this is done using strcmp() (ANSI C = portable) or locale.library/StrnCmp() (Amiga specific). But then the caller should remember to free it when it does not need it anymore. Just for your information maybe strncmp or strnicmp might interest you see :
char extPos strrchr(filename,'.'); // find the extension pos

if( extPos )
{
    
// an extension was found
    
if( strncmp(filename+extPos,".mrz")
    {
        
// ... do what you want
    
}
}


EDIT: hum it seems salass00 was faster than me

Back to a quiet home... At last
Go to top
Re: C functions Left/Right for string usage
Not too shy to talk
Not too shy to talk


See User information
@salass00

Thanks, I'm quite familiar with the intricacies of C I was trying to keep things as simple as possible by not adding that strings can't be assigned like that. I meant that in general, that's how the statement reads. Since the OP wasn't looking to copy the string buffer I just left it at that...

I guess I was a bit off with the assignment being successful - it was early in the morning

Go to top
Re: C functions Left/Right for string usage
Quite a regular
Quite a regular


See User information
@YesCop : It's something similar to this that I planed to do after posting the message .. I was sure that the problem was here that I can't compare string directly ...

@Daedalus & @Pvanni:
Heh Guys :p
Concerning comparizons, I know how they work ... just in that case I've forgotten to put == instead of = ... it's something I've understood but when I code ... I write so many lines that sometimes an error happen ...

@Pvanni :
You're wrong in your facts .. It's better for me to begin with a big project because it forces me to learn the language. It's only a period of adaptation for me to learn C ... regarding to other languages I already know.
The use of global is deliberately defined because this variable will be used in definitive, in a slightly different way (from commands) and it's wanted that the result is overwritten ... you cannot actually understand why but maybe later when the project will be more advanced, you'll understand why ;)

@Salass00 : Ok

@abalaban : Thank you for this advice :) It's a really good and optimised one :p

All we have to decide is what to do with the time that is given to us.
Go to top
Re: C functions Left/Right for string usage
Just popping in
Just popping in


See User information
@freddix

If this function is for your Basic compiler, you might want to note that Basic generally uses length-terminated strings while C uses the older-style null-terminated strings. If you're using C++ you can use the standard template library function std::string to get length-terminated functionality.

For an example of how length-terminated strings can be implemented in C, you might want to look at the old code for mb_strings.c and mb_strings.h.

Go to top
Re: C functions Left/Right for string usage
Quite a regular
Quite a regular


See User information
@freddix

I just noted my previous example was false, the line with strncmp should be :
if( stricmp(extPos,".mrz")
note the change from strncmp to stricmp and removal of the addition because extPos already point to the start of the extension...

Back to a quiet home... At last
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project