help broke prog up into functions now not working

D

David. E. Goble

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

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

int setupoutfile(char filename[], FILE *outfile)
{
strcat(filename, ".js");
outfile=fopen(filename, "w+");
if(!outfile)
{
sleep(TRUE);
outfile=fopen(filename, "w+");
if(!outfile) return -1;
}
return 0;
}

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

int mymenu(FILE *infile, FILE *outfile)
{
char line[LINESIZE]="\0";
int i=0;

while(fgets(line, LINESIZE, infile)!=NULL)
if(strstr(line, ".gif")!=NULL || strstr(line, ".jpg")!=NULL)
{
line[strlen(line)-1] = '\0';
fprintf(outfile, "myImages[%d]=\"%s\"\n", i++, line);
}
return 0;
}

int main(int argc, char *argv[])
{
FILE *infile, *outfile;

if(argc>2) setupoutfile(argv[2], outfile);
if(argc>1) setupinfile(argv[1], infile);
else
{
printf("mymenu infname basefolder -> basefolder.js\n");
return -1;
}
mymenu(infile, outfile);
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
 
M

Michael Mair

David. E. Goble said:
#include <stdio.h>
#include <string.h>

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

int setupoutfile(char filename[], FILE *outfile)
{
strcat(filename, ".js");
You are passing argv[2] as filename; there is no space left
for ".js". You need an array of char or allocated storage
of at least strlen(filename)+strlen(".js")+1 bytes.
outfile=fopen(filename, "w+");
if(!outfile)
{
sleep(TRUE);
sleep() is not a standard C function.
outfile=fopen(filename, "w+");
You are assigning the return value to outfile. Unfortunately,
the value written to outfile will be lost to you as soon as
you leave this function.
Pass FILE **outfile to the function and assign to *outfile.
if(!outfile) return -1;
}
return 0;
}

int setupinfile(char filename[], FILE *infile)
{
infile=fopen(filename, "r+");

same as above
if(!infile)
{
sleep(TRUE);
infile=fopen(filename, "r+");
if(!infile) return -1;
}
return 0;
}

int mymenu(FILE *infile, FILE *outfile)
{
char line[LINESIZE]="\0";
int i=0;

while(fgets(line, LINESIZE, infile)!=NULL)
if(strstr(line, ".gif")!=NULL || strstr(line, ".jpg")!=NULL)
{
line[strlen(line)-1] = '\0';

Umh, what exactly do you think you are doing here?
If line[strlen(line)-1] is _not_ '\n', you may very well discard
the 'f' of gif or the 'g' of jpg.
fprintf(outfile, "myImages[%d]=\"%s\"\n", i++, line);
}
return 0;
}

int main(int argc, char *argv[])
{
FILE *infile, *outfile;

if(argc>2) setupoutfile(argv[2], outfile);
Here, with the changes suggested above, you need to pass
a large enough buffer containing a copy of the string in argv[2]
and &outfile.
if(argc>1) setupinfile(argv[1], infile);
else
{
printf("mymenu infname basefolder -> basefolder.js\n");
return -1;
}

This logic is badly broken.

What do you want? Probably:
if (argc > 2) {
char *buffer = malloc(strlen(argv[2])+4);
if (!buffer) {
fprintf(stderr,"Mem trouble\n");
exit(EXIT_FAILURE);
}
strcpy(buffer, argv[2]);
setupoutfile(buffer, &outfile);
setupinfile(argv[1], &infile);
free(buffer);
}
else {
/* usual stuff */
}

By the way, if you do not want to return 0, rather use
exit(EXIT_FAILURE) (and #include said:
mymenu(infile, outfile);
return 0;
}

Cheers
Michael
 
E

Emmanuel Delahaye

David. E. Goble wrote on 30/01/05 :
{
FILE *infile, *outfile;

How are these pointers initialized ?
if(argc>2) setupoutfile(argv[2], outfile);
if(argc>1) setupinfile(argv[1], infile);

Don't you meant

if(argc>2) setupoutfile(argv[2], &outfile);
if(argc>1) setupinfile(argv[1], &infile);

or the like... Of course the prototypes of the functions have to be
redesigned...

int setupoutfile(char filename[], FILE **outfile)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
L

Lawrence Kirby

On Sun, 30 Jan 2005 23:09:54 +0100, Michael Mair wrote:

....
You are assigning the return value to outfile. Unfortunately,
the value written to outfile will be lost to you as soon as
you leave this function.
Pass FILE **outfile to the function and assign to *outfile.

In this case you might just as well make it the return value of the
function, with a null pointer return indicating a failure.

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top