Hi everyone, am currently building a program with VS 6.0 but am having problems with
string coruption, especially with strncpy(),

#include <stdio.h>
#include <string.h>
 
int main()
 {
	 char prog[200];
	 int count;
	 int su2;
	 char mir[200];

	 strcpy(prog, "cmd.exe"); // we add cmd.exe to prog
     
	 count = strlen(prog);    // we measure the length of prog
	 printf("1.%d\n", count);

	 su2 = count - 1;         // we remove 1 form the count of characters we obtain from prog
	 printf("2.%d\n", su2);   // & store it in su2

	 strncpy(mir, prog, su2); // we remove 6 characters from prog & store it into mir
	 printf("3.%s\n", mir);

  return 0;
}

<< moderator edit: added [code][/code] tags >>

at ""printf("3.%s\n", mir);"" am supposed to receive "c" but i reveive cmd.ex

It's working as expected. The last parameter of strncpy doesn't say 'how many to skip', it says 'how many to copy'. Therefore, you are telling it to start at the beginning of prog and copy 6 characters.


Also note that it's safer to memset these strings before doing anything with them,
ie:

char prog[200];
char mir[200];
memset( prog, '\0', sizeof( prog ) );
memset( mir, '\0', sizeof( mir) );

http://www.cplusplus.com/ref/cstring/strncpy.html

Also note that it's safer to memset these strings before doing anything with them,
ie:

char prog[200];
char mir[200];
memset( prog, '\0', sizeof( prog ) );
memset( mir, '\0', sizeof( mir) );

This is not safer. It is only helpful when an algorithm is broken or if the algorithm deliberately relies on zeroed space. This just covers up the real problem of a broken algorithm.

It allows you to not crash while fixing your algorithm ;)

It allows you to not crash while fixing your algorithm ;)

You've got a point there :)

It's working as expected. The last parameter of strncpy doesn't say 'how many to skip', it says 'how many to copy'. Therefore, you are telling it to start at the beginning of prog and copy 6 characters.


Also note that it's safer to memset these strings before doing anything with them,
ie:

char prog[200];
char mir[200];
memset( prog, '\0', sizeof( prog ) );
memset( mir, '\0', sizeof( mir) );

http://www.cplusplus.com/ref/cstring/strncpy.html

YES but what if i use strncat, will it work.
what does memset do ???

If you're trying to concatenate "cmd.exe" with some further information, why not use snprintf?

#include <stdio.h>

int main()
{
   char command[200], filename[] = "filename";
   int result = snprintf(command, sizeof command, "cmd.exe %s", filename);
   if ( result >= 0 && result < sizeof command / sizeof *command )
   {
      puts(command);
   }
   return 0;
}

/* my output
cmd.exe filename
*/

Pedantic note: snprintf is not C89 standard. It is available as an extension on some C89ish compilers, and is C99 standard (although there may be a difference in return value).

Please explain what you want to do. Your // comments about 'remove' indicates that you are trying to not include parts from the original string. In effect, if you want just the first character, you should set your varaible to be 1, not 6. REMEMBER, the string functions that take an argument for length are asking for how many to copy, not how many to skip. So it wouldn't matter if you were doing strncpy or memcpy or strncat.

Please explain what you want to do. Your // comments about 'remove' indicates that you are trying to not include parts from the original string. In effect, if you want just the first character, you should set your varaible to be 1, not 6. REMEMBER, the string functions that take an argument for length are asking for how many to copy, not how many to skip. So it wouldn't matter if you were doing strncpy or memcpy or strncat.

Yeah you r right. But I was only testing this new algorithm with "cmd.exe" as string. The real purpose would be to enter any string, truncates all characters until only one remails and do the rest of the work with that one character

OK, 2 options. Either:
strncpy( mir, prog+su2-1, 1 );
OR
mir[0]=prog[su2-1];

#include <stdio.h>
#include <string.h>
int main()
{
char prog[200];
int count;
int su2;
char mir[200];
strcpy(prog, "cmd.exe");
count = strlen(prog);	
printf("1.%d\n", count);
su2 = count - 1;
printf("2.%d\n", su2); // & store it in su2
strncpy( mir, prog+su2-1, 1 );
// OR
// mir[0]=prog[su2-1];
printf("3.%s\n", mir);
return 0;
}

OK, 2 options. Either:
strncpy( mir, prog+su2-1, 1 );
OR
mir[0]=prog[su2-1];

#include <stdio.h>
#include <string.h>
int main()
{
char prog[200];
int count;
int su2;
char mir[200];
strcpy(prog, "cmd.exe");
count = strlen(prog);	
printf("1.%d\n", count);
su2 = count - 1;
printf("2.%d\n", su2); // & store it in su2
strncpy( mir, prog+su2-1, 1 );
// OR
// mir[0]=prog[su2-1];
printf("3.%s\n", mir);
return 0;
}

I made it, but the thanks goes to you all.

#include <stdio.h>
#include <string.h>
 
int main()
 {
	 int count;
	 int su;
	 char prog[200];
	 char mir [200];
	 memset(prog, '\0', sizeof(prog));
	 memset(mir, '\0', sizeof(prog));

	 strcpy(prog, "cmd.exe"); // we add cmd.exe to prog
	 
	 count = strlen(prog);    // we measure the length of prog
	 //printf("Prog= %d\n", count);

	 su = count - 1;         // we remove 1 form the count of characters we obtain from prog
	 //printf("Total Characters= %d\n", su);   // & store it in su

LABEL:
	 strcpy(mir, "");
	 //printf("MIR= %s Length= %d\n", mir, strlen(mir));
	 strncat(mir, prog, su); // we remove 6 characters from prog & store it into mir

	 //printf("Mir= %d\n", strlen(mir));

	 if (strlen(mir) != 1)
	 {
		 su = su - 1;
		 //printf("Su= %d\n", su);
		 goto LABEL;
	 }
	 printf("Mir= %s\n", mir);
	 
	 
	 
  return 0;
}

By the way, i used "strcpy(mir, "");" to clear the mir character, have u got any better way

What are you doing?

Why have you replaced a single clean piece of code:
strncpy( mir, prog+su2-1, 1 );

with a whole series of bad ideas? Goto?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.