f



Need help with strings, read text and writing text to file...

Hi all;

I have writen a program to read in a list of filenmaes and to output
javascript and html code. The resulting html code is then run thru a
html to js program ( Easy HTML To Any Script Converter].

All this to display the pictures in a slideshow.

The code did work at one stage... before I fully tested. But now I
don't know whats gone wrong. As the html and javascript look right, as
does the js code. 

There are three slide show, each with their own buttons... However
some work, while others don't!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define FALSE 0
#define TRUE  1
#define LINESIZE 255

int setupoutfiles(char filename[], FILE **header, FILE **body)
{
char tempstr[LINESIZE]="\0";

    strcpy(tempstr, filename);
    strcat(filename, "header.htm");
    *header=fopen(filename, "w+");
    if(!header) return -1;
    strcat(tempstr, "body.htm");
    *body=fopen(tempstr, "w+");
    if(!body) return -1;  
return 0;
}

int setupinfile(char filename[], FILE **infile)
{
    *infile=fopen(filename, "r+");
    if(!infile) return -1;    
return 0;
}

void therest(FILE **header, char b[])
{      
     fprintf(*header, "\n%scounter=%sImages.length-1\n", b, b);
     fprintf(*header, "%si=0\n\n", b);
     fprintf(*header, "function %sfirst()\n", b);
     fprintf(*header, "{\n");
     fprintf(*header,
"document.getElementById(\'%sviewer\').src=%sImages[0]\n", b, b);
     fprintf(*header, "%si=0\n", b);
     fprintf(*header, "}\n\n");
     
     fprintf(*header, "function %sprevious()\n", b);
     fprintf(*header, "{\n");
     fprintf(*header, "if (%si>0)\n", b);
     fprintf(*header, "{\n");
     fprintf(*header, "%si--\n", b);
     fprintf(*header,
"document.getElementById(\'%sviewer\').src=%sImages[%si]\n", b, b, b);
     fprintf(*header, "}\n");
     fprintf(*header, "}\n\n");
     
     fprintf(*header, "function %snext()\n", b);
     fprintf(*header, "{\n");
     fprintf(*header, "if (%si<%scounter)\n", b);
     fprintf(*header, "{\n");
     fprintf(*header, "%si++\n", b);
     fprintf(*header,
"document.getElementById(\'%sviewer\').src=%sImages[%si]\n", b, b, b);
     fprintf(*header, "}\n");
     fprintf(*header, "}\n\n");
     
     fprintf(*header, "function %slast()\n", b);
     fprintf(*header, "{\n");
     fprintf(*header,
"document.getElementById(\'%sviewer\').src=%sImages[%scounter]\n", b,
b, b);
     fprintf(*header, "}\n\n");
     fprintf(*header, "</script>");
}

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;
}

void mybuttons(FILE **body, char b[])
{
     fprintf(*body, "<form>\n");
     fprintf(*body, "<input value=\"First\" onclick=\"%sfirst()\"
type=\"button\">\n", b);
     fprintf(*body, "<input value=\"Previous\"
onclick=\"%sprevious()\" type=\"button\">\n", b);
     fprintf(*body, "<input value=\"Next\" onclick=\"%snext()\"
type=\"button\">\n", b);
     fprintf(*body, "<input value=\"Last\" onclick=\"%slast()\"
type=\"button\">\n", b);
     fprintf(*body, "</form>\n");
}
void mybody(FILE **body, char b[], char afile[])
{
     fprintf(*body, "<center>\n");
     fprintf(*body, "<h1>%s</h1>\n", b);
     
     mybuttons(body, b);
     
     fprintf(*body, "<table border=\"1\">\n");
     fprintf(*body, "<tr>\n");
     fprintf(*body, "<td align=\"center\">");
     fprintf(*body, "<img id=\"%sviewer\"
src=\"%s/alien.gif\"></td>\n", b, b);
     fprintf(*body, "</tr>\n");
     fprintf(*body, "</table>\n");
     fprintf(*body, "<p></p>\n");
     
     mybuttons(body, b);

     fprintf(*body, "<p></p>\n");
     fprintf(*body, "<p>If you have a smilely</p>\n");
     fprintf(*body, "</center>\n");        
}

int main(int argc, char *argv[])
{
FILE *infile, *header, *body;
char outfname[LINESIZE]="\0",
     tempstr[LINESIZE]="\0";

     if(argc>2) 
       {
       if(setupoutfiles(strcpy(outfname, argv[2]), &header, &body))
         {
         printf("problem opening outfiles %s\n", outfname);
         return -1;
         }
       if(setupinfile(argv[1], &infile))
         {
         printf("problem opening infile %s\n", argv[1]);
         return -1;
         }
       }
     else
       {
       printf("mymenu infname [basefolder] -> basefolderheader.htm,
basefolderbody.htm\n");
       return -1;
       } 
     strcpy(outfname, argv[2]);
     myheader(&infile, &header, outfname, tempstr);
     mybody(&body, outfname, tempstr);
     
     fclose(header);
     fclose(body);
     fclose(infile);
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
-1
David
2/1/2005 12:24:09 AM
comp.std.c 1570 articles. 0 followers. Post Follow

1 Replies
1019 Views

Similar Articles

[PageSpeed] 17

David. E. Goble <degoble@gtech.computing> writes:
> I have writen a program to read in a list of filenmaes and to output
> javascript and html code. The resulting html code is then run thru a
> html to js program ( Easy HTML To Any Script Converter].
[snip]

The comp.std.c newsgroup is for discussion of the C standard, not of
standard C.  C programming questions like yours are more appropriate
in comp.lang.c (or in comp.lang.c.moderated if you don't mind paying
for a higher signal/noise ration with longer latency).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
-1
Keith
2/1/2005 12:34:15 AM
Reply: