473,387 Members | 1,628 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help broke prog up into functions now not working

#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
Nov 14 '05 #1
4 1216
David. E. Goble wrote:
#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 <stdlib.h>).
mymenu(infile, outfile);
return 0;
}


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2
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"

Nov 14 '05 #3
On Sun, 30 Jan 2005 23:09:54 +0100, Michael Mair wrote:

....
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.


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
Nov 14 '05 #4
THanks for the replies guy! I got it working.
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
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
0
by: Mike Chirico | last post by:
Hopefully this will help someone... Helpful Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Last Updated: Fri Apr 16 11:47:34 EDT 2004 The latest version of this...
3
by: JamesLomuscio | last post by:
To all who know C++, Im working on a prog that will be able to predict the weather, its too complicated to go into here, but I do need some help. I need to grab all of the information in the...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
26
by: Robert Baer | last post by:
The following passes the test as valid, and the mouseover for the six indicated areas also work. I need various areas to link to another page, including the six mentioned. However either the MAP...
16
by: C++ Hell | last post by:
Hey everyone just designing a quiz for school and managed to write the code for the questions and answers thou i have to add scores and at the end an overall score does anyone have any idea what to...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
11
by: cybervigilante | last post by:
I can't seem to change the include path on my local winmachine no matter what I do. It comes up as includ_path .;C:\php5\pear in phpinfo() but there is no such file. I installed the WAMP package...
10
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.