[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

C Programming: strcpy

Posted on 2013-10-27
1
Medium Priority
?
273 Views
Last Modified: 2013-10-28
Why is the code below wrong? It says that strlen() is 4, and printing "req" gives empty string.

char *req = malloc( sizeof(char) * 200);
strcpy(req, "GET ");
strcpy(req, request);
strcpy(req, " HTTP/1.1\r\nHOST: ");
strcpy(req, hostName);
strcpy(req, "\r\n\r\n");

fprintf(fp, "%d\n", strlen(req));
fprintf(fp, "%s\n", req);

Open in new window

0
Comment
Question by:pzozulka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 39604830
The way you're doing that, you're writing over the characters each time and the last time, "\r\n\r\n", is 4 characters long but they're all control characters.  After the first 'strcpy', you probably want to use 'strcat' to add the remaining characters to the string.
http://man7.org/linux/man-pages/man3/strncat.3.html
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question