Click to See Complete Forum and Search --> : the c equiv
EscapeCharacter
09-19-2001, 07:14 AM
how would this be done in c?
#!/usr/bin/perl -w
use strict;
my($host);
my($ext);
my($picstr);
my($wholestr);
my($i);
$i = 0;
$host = "wget <A HREF="http://www.geocities.com/branephood/ln";" TARGET=_blank>http://www.geocities.com/branephood/ln";</A>
$ext = ".jpg";
while($i < 1000){
$picstr = $i;
$wholestr = "$host$picstr$ext";
system($wholestr);
print $wholestr;
$i++;
}
primarily adding the two cahracter and one int varibles together, ive tried accomplishing it in c but im getting seg faults and cant figure out why.
#include <unistd.h>
#include <stdio.h>
char newchar(const char *, int);
int main(){
char *temp2, *temp4;
unsigned int n = 1;
const char *temp = "wget <A HREF="http://www.geocities.com/branephood/lb";" TARGET=_blank>http://www.geocities.com/branephood/lb";</A>
const char *temp3 = ".jpg";
while(1){
sprintf(temp2, "%d", n);
temp4 = (char*)newchar(temp, 0);
cout << temp4;
system((char*)temp4);
n++;
}
}
char newchar(const char *newchr, int passed){
char *temp;
unsigned int x = passed;
for(x; x < strlen(newchr); x++)
temp[x] = newchr[x];
return (char)temp;
}
sans-hubris
09-19-2001, 07:59 AM
You never allocated any memory for temp2 before you tried to use it.
Use printf, not cout, since this is just C and not C++.
EscapeCharacter
09-19-2001, 08:18 AM
forgot to change that cout, guess im kinda tired :)
is this the correct way to allocate memory for temp2?
temp2 = (char*)malloc(sizeof(char*));
pointers are my enemy i cant get a hold of how and when i need to do all that stuff.
EscapeCharacter
09-19-2001, 08:25 AM
ive been playing with gdb a bit and found something wierd, im getting this when i run the program
Program received signal SIGSEGV, Segmentation fault.
0x4000e0b3 in strcmp () from /lib/ld-linux.so.2
weird thing is i dont use strcmp in the code anywhere, could this be the cause of the seg fault somehow?
EscapeCharacter
09-19-2001, 08:56 AM
w00t got it working, turned out i had to realloc temp4 everytime i changed it, this memory stuff isnt as hard as i thought. thanks for pointing me in the right direction.
[ 19 September 2001: Message edited by: EscapeCharacter ]
The Kooman
09-19-2001, 10:38 AM
Originally posted by EscapeCharacter:
<STRONG>how would this be done in c?
#include <unistd.h>
#include <stdio.h>
char newchar(const char *, int);
int main(){
char *temp2, *temp4;
unsigned int n = 1;
const char *temp = "wget <A HREF="http://www.geocities.com/branephood/lb";" TARGET=_blank>http://www.geocities.com/branephood/lb";</A>
const char *temp3 = ".jpg";
while(1){
sprintf(temp2, "%d", n);
temp4 = (char*)newchar(temp, 0);
cout << temp4;
system((char*)temp4);
n++;
}
}
char newchar(const char *newchr, int passed){
char *temp;
unsigned int x = passed;
for(x; x < strlen(newchr); x++)
temp[x] = newchr[x];
return (char)temp;
}
</STRONG>
Man, this is loaded with bombs ;)!
Basically, remember to allocate memory before using a pointer (if it isn't already pointing to a valid memory location)! By valid memory location, I mean a memory location where you're allowed to write. In your example, you'd have to take care of allocation for:
temp2
temp (inside function newchar)
Also remember, the "temp" variable in your main() function is a pointer to a string WHICH CANNOT BE ALTERED! Similarly for temp3. Your code doesn't alter them so its ok but don't forget ... trying to write to these "constant content locations" often lead to segfaults.
Lastly, why not use sprintf() throughout instead of just for temp2?
I suppose "a code is worth a thousand words" ;), so here it is:
Warning: Unteseted code
#include <unistd.h>
#include <stdio.h>
int main(void)
{
char *command; /* this will hold the entire string */
unsigned int picnum;
const char *host = "wget <A HREF="http://www.geocities.com/branephood/lb";" TARGET=_blank>http://www.geocities.com/branephood/lb";</A>
const char *ext = ".jpg";
for (picnum = 0; picnum < 1000; picnum++)
{
/* Allocate space for the command:
* length of host string +
* length of extension string +
* 20 for "picstr" (since "int"s are less than 20 digits long) +
* 1 for '\0'
*
* NOTE: 1. we're talking of only max 32-bit integers here ;)
* 2. THOU SHALL ALWAYS CHECK THE RETURN VALUE OF malloc()!
*/
if ((command = (char *)malloc(strlen(host)+strlen(ext)+20+1)) != NULL)
{
sprintf(command, "%s%d%s", host, picnum, ext);
printf("%s\n", command);
system(command);
/* THOU SHALL FREE EVERY SUCCESSFUL malloc() */
free(command);
}
else
{
printf("Could not malloc space for picnum = %d\n", picnum);
}
}
exit(0);
}
HTH!
EscapeCharacter
09-19-2001, 12:05 PM
excellent great job on cleaning up my mess :), your untested code works just fine