help! strings

D

David. E. Goble

Hi all;

I have a function that reads in a list of filenames wrps them in
javascript and ten writes them to a file. It all works, now I want to
randomly store one for later use. But the string has extar garbage on
the end of it.

EG mymenu infile test
Read in

007.gif
1luvu.gif
1zhelp.gif
22_yikes.gif
2evil.gif
test.jpg
2twocents.gif
321.gif
test.html
ak47.gif
ald.gif
alien.gif
angry.gif

and randomly pick one and store in char afile[]. Lets say we select
randomly test.jpg. The output should be;

testImages[0]="test/007.gif"
testImages[1]="test/1luvu.gif"
testImages[2]="test/1zhelp.gif"
testImages[3]="test/22_yikes.gif"
testImages[4]="test/2evil.gif"
testImages[5]="test/test.jpg"
testImages[6]="test/2twocents.gif"
testImages[7]="test/321.gif"
testImages[8]="test/ak47.gif"
testImages[9]="test/ald.gif"
testImages[10]="test/alien.gif"
testImages[11]="test/angry.gif"

afile = test/test.jpg

But it ends up as test/test.jpg/ald.gif

Here is the function;

int myheader(FILE **infile, FILE **header, char b[], char afile[])
{
char line[LINESIZE]="\0";
char base[LINESIZE]="\0";
int i=0;

strcpy(afile, "\0");
fprintf(*header, "<script type=\"text/javascript\">\n\n");
fprintf(*header, "%sImages=new Array()\n\n", b);
while(fgets(line, LINESIZE, *infile)!=NULL)
{
line[strlen(line)-1] = '\0';
if(strstr(line, ".gif")!=NULL || strstr(line, ".jpg")!=NULL)
{
strcpy(base, b);
strcat(base, "/");
strcat(base, line);
if(!rand()%1) strcpy(afile, base);
fprintf(*header, "%sImages[%d]=\"%s\"\n", b, i++, base);
}
}
therest(header, b);
return 0;
}


Regards David. E. Goble
http://www.pnc.com.au/~degoble
degoble[AT]pnc.com.au | dgoble[AT]pnc.com.au
Po Box 648 (9 Murray St), Kingscote, Kangaroo Island SA 5223
 
L

Lawrence Kirby

Hi all;

I have a function that reads in a list of filenames wrps them in
javascript and ten writes them to a file. It all works, now I want to
randomly store one for later use. But the string has extar garbage on
the end of it.

EG mymenu infile test
Read in

007.gif
1luvu.gif
1zhelp.gif
22_yikes.gif
2evil.gif
test.jpg
2twocents.gif
321.gif
test.html
ak47.gif
ald.gif
alien.gif
angry.gif

and randomly pick one and store in char afile[]. Lets say we select
randomly test.jpg. The output should be;

testImages[0]="test/007.gif"
testImages[1]="test/1luvu.gif"
testImages[2]="test/1zhelp.gif"
testImages[3]="test/22_yikes.gif"
testImages[4]="test/2evil.gif"
testImages[5]="test/test.jpg"
testImages[6]="test/2twocents.gif"
testImages[7]="test/321.gif"
testImages[8]="test/ak47.gif"
testImages[9]="test/ald.gif"
testImages[10]="test/alien.gif"
testImages[11]="test/angry.gif"

afile = test/test.jpg

But it ends up as test/test.jpg/ald.gif

Here is the function;

int myheader(FILE **infile, FILE **header, char b[], char afile[])

You need to show how it is called. Perhaps there is a problem in the way
space for b or afile is allocated. Why are infile and header pointers to
pointers?
{
char line[LINESIZE]="\0";
char base[LINESIZE]="\0";
int i=0;

strcpy(afile, "\0");

You can write this more simply as

afile[0] = '\0';
fprintf(*header, "<script type=\"text/javascript\">\n\n");
fprintf(*header, "%sImages=new Array()\n\n", b);
while(fgets(line, LINESIZE, *infile)!=NULL)
{
line[strlen(line)-1] = '\0';

There are some cases that this doesnt cope with, e.g. if the line is to
large to read in in one go or we hit end of file and the data doesn't end
in a '\n'. Potentially naster is if strlen(line) is zero.
if(strstr(line, ".gif")!=NULL || strstr(line, ".jpg")!=NULL)

This doesn't guarantee that these are the last part of the string.
{
strcpy(base, b);
strcat(base, "/");
strcat(base, line);

sprintf() is your friend:

sprintf(base, "%s/%s", b, line);

There are no tests here to ensure that the data written to base will fit
in it.
if(!rand()%1) strcpy(afile, base);

Any value % 1 results in zero, also !rand() will mostly be zero anyway.
While not a perfect solution consider !(rand() % (i+1)) instead.
fprintf(*header, "%sImages[%d]=\"%s\"\n", b, i++, base);
}
}
therest(header, b);
return 0;
}

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top