Solved

C Programming: strcpy

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intel debugger, idb, only seeing .c and .h, but no .f90 1 298
Problem to show menu 5 87
sameEnds challenge 3 135
Acrinis True image 2 61
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now