Questions tagged [strcpy]

The C standard library function, "strcpy()," is used to copy non-overlapping, null-terminated strings. It is also defined as "std::strcpy()" in the C++ standard library.

Used to copy null-terminated, non-overlapping strings, it is defined in the <string.h> standard header. It is also defined in the C++ standard library in the <cstring> header.

Documentation for C strcpy.

Documentation for C++ std::strcpy.

947 questions
1
vote
1 answer

strcpy not copying the full string

I'm using Visual C++ 6.0, and I have the code below. char filename[1000], string[5]; FILE *fin; strcpy(filename, argv[3]); if ((fin = fopen(filename, "r")) != NULL) { fgets(string, 100, fin); string[strlen(string)-1] = NULL; …
Rayne
  • 14,247
  • 16
  • 42
  • 59
1
vote
4 answers

Buffer too small during object construction. Think it has something to do with strcpy_s()

The code that I am putting up is the result of the third week of working on this class. I had a pretty good handle on things, (or so I thought), but this week is focusing on pointers and I am clueless as to why I keep getting this error. I keep…
Nyxm
  • 881
  • 3
  • 11
  • 13
1
vote
3 answers

C++ Seg Fault at end of function. Reference line = closing brace

In conclusion: Thanks so much everyone! All the responses posted below were correct. The initial error was me forgetting to leave room for the NULL terminator. Strcpy() is a dangerous function because when I used it, it didn't know when the end of…
ghn
  • 119
  • 7
1
vote
2 answers

Why is strcpy() also copying \n? Can I get rid of it?

I debugged a function and it is working. So yay, teaching myself C seems to be going along well. But I want to make it better. That is, it reads a file like this: want to program better And puts each individual line of string into an array of…
GeekyOmega
  • 1,235
  • 6
  • 16
  • 34
1
vote
3 answers

C strange error with strcpy and even stranger solution

I'm creating a program to open .txt files in a given directory, I have an array with all the absolute paths of the files inside the directory in question and I'm creating a function to extract and return the name of the files, the function is…
user1493813
  • 981
  • 1
  • 8
  • 16
1
vote
1 answer

strcpy_s for char** and char[][]

I used strcpy_s as below: char names[2][20]; strcpy_s(names[0],"Michael"); strcpy_s(names[1],"Danny"); and it worked all right. But when I changed to char **, int size1=2; int size2=20; char **names=new char*[size1]; for(int i=0;i
Michael
  • 673
  • 2
  • 5
  • 23
1
vote
1 answer

C/C++ strcpy unhandled read violation

unsigned char* Data::getAddress(unsigned char* address) { strcpy((char*)address, (char*)this->_address); return (unsigned char*)address; } int main() { Data d; d.makealinkedlisthere(); while (d) { unsigned char…
user1334943
  • 127
  • 1
  • 1
  • 10
0
votes
4 answers

C: strncpy more characters than allocated then printing... unexpected output?

In some sample code given by a professor: #include #include #include int main() { char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; printf( "%s\n", alpha ); printf( "%c\n", alpha[8] ); alpha[8] = 'Z'; /*…
Joseph
  • 12,678
  • 19
  • 76
  • 115
0
votes
3 answers

strcpy() creates error

I have this structure which i am trying to initialize using the following code. It gets run time error when trying to initialize "finger_print" What is wrong with it? typedef struct fpinfo { unsigned long chunk_offset; unsigned long…
John
  • 794
  • 2
  • 18
  • 34
0
votes
5 answers

copy allocated char array to "normal" char array

Please, tell me, what is the correct way of copying allocated char array to "normal" char array? I have attempted to do the following, but it fails : char * buf = (char *) malloc (BUFSIZ * sizeof(char)); // filling up the allocated array with…
Jake Badlands
  • 1,016
  • 3
  • 23
  • 46
0
votes
5 answers

strcpy and strcmp, what am I doing wrong?

User should enter a few strings and input space as string when he is done. Code should return longest and shortest word entered. strcmp always returns -1... what am i doing wrong? #include #include using namespace std; int…
eqagunn
  • 221
  • 1
  • 4
  • 11
0
votes
1 answer

Using read() and putting the buffer into a string

this is my second question as i've had trouble with the first one due to this problem. I have a file which i have to read using the read() statment, no no fget() or fread() etc i use the line. read(fileRead, buffer, blocksize); where as you know…
MrEwok
  • 25
  • 6
0
votes
2 answers

Garbage printed with string after strcpy

I am a beginner in C and I am trying to copy a string inside another one. I have the following code: #include #include typedef struct { float mort_2009, mort_2015, indices; char estado[]; } Mortalidade; void…
0
votes
1 answer

What is the difference between char *p and char p[] in strcpy

What is the difference between char *p and char p[] in two below code: #include #include void main() { char source[]="stackflow"; char target[0]; strcpy(target,source); printf("%s",…
Kousaka
  • 3
  • 2
0
votes
0 answers

"strcpy-ssse3.S: No such file or directory", why does it happen? (C, Ubuntu)

I'm new to C and to the Ubuntu os, I have three files: main.c, dosomething.c, dosomething.h main.c #include #include #include "dosomething.h" int main(int argc, char *argv[]) { int i; char *source; if(argc < 2) …