Solved

C Programming: strcpy

Posted on 2013-10-27
1
266 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 83

Accepted Solution

by:
Dave Baldwin earned 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

688 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