what am i doing wrong! 
Author Message
 what am i doing wrong!

hi sorry to bug you all again but i changed all the scanf's to fgets.
Everything compiles fine, but then as i run the program, as it attempts to
open the file, the thing crashes!!!

heres the code:

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

int main()

{
 char fName[20], title[20], bgColor[20], fontFace[15], fontSize[5],
bodyText[40];
 FILE *outfile;
 char html [6] = ".html";

     printf("QuickBuilder v1.0 by Robert Bell\n\n");
     printf("Enter the file name of your page: ");
     fgets (fName, sizeof fName, stdin);
     strcat (fName, html);

     printf("\nEnter the title of your page: ");
     fgets (title, sizeof title, stdin);

     printf("\nEnter the background colour: ");
     fgets (bgColor, sizeof bgColor, stdin);

     printf("\nChoose a font for your page: ");
     fgets (fontFace, sizeof fontFace, stdin);

     printf("\nWhat size font would you like to use: ");
     fgets (fontSize, sizeof fontSize, stdin);

     printf("\nWhat text would you like to appear: ");
     fgets (bodyText, sizeof bodyText, stdin);

   outfile = fopen (fName, "w");

   fprintf(outfile, "<html>\n<head><title>%s</title></head>\n\n<body bgcolor
= %s>\n<font face = %s size = %s>%s</body></html>", title, bgColor,
fontFace, fontSize, bodyText);

   fclose (outfile);

   printf("\nYour page %s has been created.\n\n", fName);

   return 0;

Quote:
}



Sat, 05 Jun 2004 05:59:05 GMT  
 what am i doing wrong!

Quote:

> hi sorry to bug you all again but i changed all the scanf's to fgets.
> Everything compiles fine, but then as i run the program, as it attempts to
> open the file, the thing crashes!!!

> heres the code:

Here are a few comments:

Quote:
> #include <stdio.h>
> #include <stdlib.h>

> int main()

> {
>  char fName[20], title[20], bgColor[20], fontFace[15], fontSize[5],
> bodyText[40];
>  FILE *outfile;
>  char html [6] = ".html";

It is better not to rely on your counting skills and let the compiler
do it for you: char html[]=".html";

Quote:

>      printf("QuickBuilder v1.0 by Robert Bell\n\n");
>      printf("Enter the file name of your page: ");

Put a fflush(stdout); here to make sure the text goes where it's
supposed to (like the screen!) and isn't stuck in the buffer of
the stdio library

Quote:
>      fgets (fName, sizeof fName, stdin);

You should check the return value of fgets agains NULL to make sure it
didn't fail.
Also, if the user entered "mybetterandimprovedhomepage\n", fgets will
put
mybetterandimproved\0 in fName (19+1 chars) and leave the rest in the
input
stream. You should do something about that left over stuff, like discard
it

if(strchr(fName,'\n')==NULL){
   /* No newline in the buffer -> discard rest of line */
   int c;
   while((c=getchar())!=EOF && c!='\n')
      ;

Quote:
}
>      strcat (fName, html);

We now have mybetterandimproved\0 in fName. There's no space left
to add the contents of html. You scribble over memory and your
prog goes kaboum. change the sizeof fName in the fgets call to
sizeof fName-strlen(html)

Quote:

>      printf("\nEnter the title of your page: ");

Starting output with a \n is ugly IMHO. They should be at the end
instead.

Quote:
>      fgets (title, sizeof title, stdin);

>      printf("\nEnter the background colour: ");
>      fgets (bgColor, sizeof bgColor, stdin);

>      printf("\nChoose a font for your page: ");
>      fgets (fontFace, sizeof fontFace, stdin);

>      printf("\nWhat size font would you like to use: ");
>      fgets (fontSize, sizeof fontSize, stdin);

>      printf("\nWhat text would you like to appear: ");
>      fgets (bodyText, sizeof bodyText, stdin);

>    outfile = fopen (fName, "w");

>    fprintf(outfile, "<html>\n<head><title>%s</title></head>\n\n<body bgcolor
> = %s>\n<font face = %s size = %s>%s</body></html>", title, bgColor,
> fontFace, fontSize, bodyText);

You can break a statement across lines. This makes your source more
readable:
     fprintf(outfile,"<html>\n"
                     "<head><title>%s</title></head>\n\n"
                     "<body bgcolor> = %s>\n"
                     "<font face = %s size = %s>%s"
                     "%s</body></html>"
                     ,title,bgColor,fontFace,fontSize,
      );

Tobias.



Sat, 05 Jun 2004 06:25:21 GMT  
 what am i doing wrong!
Hiya,



Quote:
> hi sorry to bug you all again but i changed all the scanf's to fgets.
> Everything compiles fine, but then as i run the program, as it attempts
> to open the file, the thing crashes!!!

> heres the code:

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

> int main()

Well, you've not changed that - try reading what other post eh....

Quote:

> {
>  char fName[20], title[20], bgColor[20], fontFace[15], fontSize[5],
> bodyText[40];

fontsize should be an int really.

Quote:
>  FILE *outfile;
>  char html [6] = ".html";

better to have

char html[]={".html"};

you don't need to worry about getting the size of the array correct to
include \0 terminators.

Quote:
>      printf("QuickBuilder v1.0 by Robert Bell\n\n");
>      printf("Enter the file name of your page: ");
>      fgets (fName, sizeof fName, stdin);
>      strcat (fName, html);

Aroooooooogah! Aroooooooooooogah!

What happens if fName is 18 characters long and you try and concatenate
the next string on... As I said with scanf previously, you'll get a trashed
machine. What you may want here is sizeof fName - sizeof html to ensure you
have enough space.

I would also include a flush here.

Quote:
>      printf("\nEnter the title of your page: ");
>      fgets (title, sizeof title, stdin);

>      printf("\nEnter the background colour: ");

Just for clarity, add if this is a real colour (such as DarkRed) or the
Netscape cube format.

Quote:
>      fgets (bgColor, sizeof bgColor, stdin);

>      printf("\nChoose a font for your page: ");
>      fgets (fontFace, sizeof fontFace, stdin);

>      printf("\nWhat size font would you like to use: ");
>      fgets (fontSize, sizeof fontSize, stdin);

Again, a small amount of thought here could save you a problem. Change the
line to

printf("\nWhat size font would you like to use (1 - 7, 3 = normal): ");
do
{
 scanf("%d",&fontSize);

Quote:
}

while (fontSize < 1 || fontsize > 7);

Quote:
>      printf("\nWhat text would you like to appear: ");
>      fgets (bodyText, sizeof bodyText, stdin);

>    outfile = fopen (fName, "w");

Oh come on. READ THE OTHER POSTINGS! You've made the same mistakes here as
you have before. There is no check to see if fName has been opened so the
above can fail quite merrily, especially if your OS requires a path.

Quote:
>    fprintf(outfile, "<html>\n<head><title>%s</title></head>\n\n<body
> bgcolor = %s>\n<font face = %s size = %s>%s</body></html>", title,
> bgColor, fontFace, fontSize, bodyText);

broken. See other post. If you use the int version, the line becomes

... size = +%d ...

Quote:
>    fclose (outfile);

Which will have undefined (on some compilers) behaviour if the stdout had
failed to open.

Quote:
>    printf("\nYour page %s has been created.\n\n", fName);

>    return 0;
> }

One word of advise. Skip back a bit and read the other postings about this
code. Then go to your local library and get a book (or 3) out on C. I
believe there is a very good book by some blokes called Kerrigan and
Ritchie about C ;-)

Word to the wise though, if it has the word Schildt on it, don't touch it.
By removing the letters c & ld, you'll find what the book is really like...

TTFN

Paul

--
Sent from the heart of British Technology at its best - the all
new RiscStation R7500 - now running with the new 50ns upgrade
All opinions expressed are solely mine and do not represent any
other persons or companies.



Sat, 05 Jun 2004 07:40:37 GMT  
 what am i doing wrong!
I modified you program a bit....

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

int main()

{
 char fName [20], title [20], bgColor [20], fontFace [15], fontSize [5],
bodyText [40];
 FILE *outfile;
 char html [6] = ".html";

     printf("\n            QuestionHTML version 1.0 by Psonetwo            \n");
printf("           ////////////////////////////////////    ");
     printf("\n\nEnter the file name of your page: ");
     fgets(fName, sizeof fName, stdin);
     strcat (fName, html);

     printf("\nEnter the title of your page: ");
     fgets(title, sizeof title, stdin);

     printf("\nEnter the background colour: ");
     fgets(bgColor, sizeof bgColor, stdin);

     printf("\nChoose a font for your page: ");
     fgets(fontFace, sizeof fontFace, stdin);

     printf("\nWhat size font would you like to use: ");
     fgets(fontSize, sizeof fontSize, stdin);

     printf("\nWhat text would you like to appear: ");
     fgets(bodyText, sizeof bodyText, stdin);

   outfile = fopen (fName, "w");

   fprintf(outfile, "<html>\n<head><title>%s</title></head>\n\n<body bgcolor
= %s>\n<font face = %s size = %s>%s</body><a
href=http://www.geocities.com/psonetwo><font color=black size=8 face=Comic
Sans MS>Generated by QuestionHTML version 1.0: Program made by

face=Comic Sans MS>Email for feedback</a></html>", title, bgColor,
fontFace, fontSize, bodyText);
   fclose (outfile);

   printf("\nYour page %s has been created.\n\n", fName);

   return 0;

Quote:
}



Sat, 05 Jun 2004 10:57:28 GMT  
 what am i doing wrong!



Quote:
> hi sorry to bug you all again but i changed all the scanf's to fgets.
> Everything compiles fine, but then as i run the program, as it attempts
to
> open the file, the thing crashes!!!

I would think so!

Quote:

> heres the code:

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

> int main()

> {
>  char fName[20], title[20], bgColor[20], fontFace[15], fontSize[5],
> bodyText[40];
>  FILE *outfile;
>  char html [6] = ".html";

better is
    char html[]=".html"; /* let the compiler count the length */
Quote:

>      printf("QuickBuilder v1.0 by Robert Bell\n\n");
>      printf("Enter the file name of your page: ");
>      fgets (fName, sizeof fName, stdin);

Bzzzt-----------------^^^^^^^^^^^
make that
        fgets (fName, sizeof fName-strlen(html), stdin);
so you have room to concatenate the html string.

Quote:
>      strcat (fName, html);

>      printf("\nEnter the title of your page: ");
>      fgets (title, sizeof title, stdin);

>      printf("\nEnter the background colour: ");
>      fgets (bgColor, sizeof bgColor, stdin);

>      printf("\nChoose a font for your page: ");
>      fgets (fontFace, sizeof fontFace, stdin);

>      printf("\nWhat size font would you like to use: ");
>      fgets (fontSize, sizeof fontSize, stdin);

>      printf("\nWhat text would you like to appear: ");
>      fgets (bodyText, sizeof bodyText, stdin);

>    outfile = fopen (fName, "w");

Here is your problem^^^^^^^^^^^^^^^^^
fgets() leaves the \n in the string if there is room for it.
If your user entered "MYFILE"  fopen() is trying to open the file
  "MYFILE\n.html" .  It seems that your OS will not let you use a filename
   with a newline character in it.  
So fopen() returns NULL telling you that it could not open that file.
AND you do not check that outfile is valid!

if(!outfile){
    printf("sorry could not create the file >%s<\n",fname);
    return EXIT_FAILURE;

Quote:
}

>    fprintf(outfile, "<html>\n<head><title>%s</title></head>\n\n<body
bgcolor
> = %s>\n<font face = %s size = %s>%s</body></html>", title, bgColor,
> fontFace, fontSize, bodyText);

Now you try to use  that NULL file pointer to write to the file that fopen
could not open... BOOM crash and burn.....

Quote:

>    fclose (outfile);

>    printf("\nYour page %s has been created.\n\n", fName);

>    return 0;
> }

Roger...


Sat, 05 Jun 2004 11:55:29 GMT  
 what am i doing wrong!


Quote:
>hi sorry to bug you all again but i changed all the scanf's to fgets.
>Everything compiles fine, but then as i run the program, as it attempts to
>open the file, the thing crashes!!!

...

Quote:
>   outfile = fopen (fName, "w");

>   fprintf(outfile, "<html>\n<head><title>%s</title></head>\n\n<body bgcolor
>= %s>\n<font face = %s size = %s>%s</body></html>", title, bgColor,
>fontFace, fontSize, bodyText);

Your main error is in failing to check the return value of fopen() to
see whether it succeeded or not. Fixing this may lead you to the
conclusion that there are other errors in your code but this is the
serious one that causes (or fails to prevent) your program to crash.

--
-----------------------------------------


-----------------------------------------



Sun, 06 Jun 2004 23:34:16 GMT  
 what am i doing wrong!


Quote:
> I modified you program a bit....

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

> int main()

> {
>  char fName [20], title [20], bgColor [20], fontFace [15], fontSize [5],
> bodyText [40];
>  FILE *outfile;
>  char html [6] = ".html";

>      printf("\n QuestionHTML version 1.0 by Psonetwo \n");
> printf(" //////////////////////////////////// ");
>      printf("\n\nEnter the file name of your page: ");
>      fgets(fName, sizeof fName, stdin);
>      strcat (fName, html);

>      printf("\nEnter the title of your page: ");
>      fgets(title, sizeof title, stdin);

>      printf("\nEnter the background colour: ");
>      fgets(bgColor, sizeof bgColor, stdin);

>      printf("\nChoose a font for your page: ");
>      fgets(fontFace, sizeof fontFace, stdin);

>      printf("\nWhat size font would you like to use: ");
>      fgets(fontSize, sizeof fontSize, stdin);

>      printf("\nWhat text would you like to appear: ");
>      fgets(bodyText, sizeof bodyText, stdin);

>    outfile = fopen (fName, "w");

>    fprintf(outfile, "<html>\n<head><title>%s</title></head>\n\n<body
bgcolor
> = %s>\n<font face = %s size = %s>%s</body><a
> href=http://www.geocities.com/psonetwo><font color=black size=8 face=Comic
> Sans MS>Generated by QuestionHTML version 1.0: Program made by

size=8
> face=Comic Sans MS>Email for feedback</a></html>", title, bgColor,
> fontFace, fontSize, bodyText);
>    fclose (outfile);

>    printf("\nYour page %s has been created.\n\n", fName);

>    return 0;
> }

Yer thanks buddy, that was funny, i think.

p.s the original all along the watchtower sucked, jimi was the man who
brought it places, eat that zimmerman!



Mon, 07 Jun 2004 06:48:19 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. what am i doing wrong?

2. what am I doing wrong here?

3. What am I doing wrong?

4. What am I doing wrong?

5. WHAT AM I DOING WRONG?

6. What am I doing wrong (part 2)

7. What am i doing wrong?

8. What am i doing wrong in this code?

9. What am I doing wrong here

10. What am I doing Wrong?

11. what am i doing wrong

12. what am i doing wrong???

 

 
Powered by phpBB® Forum Software