newbie having trouble 
Author Message
 newbie having trouble

hello, im really lame at c, forgive me for any lame stuff i write, but heres
my prob:

i want to create a basic html page based on what the user inputs, i want the
user to choose a name to be put into the header section, this all works

everything else outputs fine. heres the code:

#include <stdio.h>

main()

{
 char title [20];
 FILE *outfile;

   printf("Enter the title of your page: ");
   scanf("%s", title);
   printf("Your page will be saved as %s.html", title);

   outfile = fopen (title, "w");

   fprintf(outfile, "<html>\n<head><title>%s</title></head>\n");

   fclose (outfile);

Quote:
}

thats it pretty simple so far. Also d like to know if there is anyway to
change the file extension of the file created? i.e. to .html instead of
nothing.

thanks

rob bell



Sun, 30 May 2004 07:40:34 GMT  
 newbie having trouble

Quote:

> hello, im really lame at c, forgive me for any lame stuff i write, but
> heres my prob:

> i want to create a basic html page based on what the user inputs, i want
> the user to choose a name to be put into the header section, this all

> page title, everything else outputs fine. heres the code:

> #include <stdio.h>

> main()

> {
>  char title [20];
>  FILE *outfile;

>    printf("Enter the title of your page: ");
>    scanf("%s", title);
>    printf("Your page will be saved as %s.html", title);

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

>    fprintf(outfile, "<html>\n<head><title>%s</title></head>\n");

You forgot the title:
fprintf(outfile, "your html stuff %s\n", title);

Quote:
> thats it pretty simple so far. Also d like to know if there is anyway to
> change the file extension of the file created? i.e. to .html instead of
> nothing.

strcat() the ".html" extension to the file-name.

--
Home: http://users.skynet.be/bk333466/
OS: RedHat Linux 7.2 (kernel 2.4.16)
X: KDE 2.2.2-1 (XFree86-4.1.0-3, QT-2.3.2-1)



Sun, 30 May 2004 08:06:12 GMT  
 newbie having trouble
thanks alot guys, thats helped me out loads.

rob bell



Sun, 30 May 2004 08:17:15 GMT  
 newbie having trouble

Quote:

> hello, im really lame at c, forgive me for any lame stuff i write, but heres
> my prob:

> i want to create a basic html page based on what the user inputs, i want the
> user to choose a name to be put into the header section, this all works

> everything else outputs fine. heres the code:

> #include <stdio.h>

> main()

> {
>  char title [20];
>  FILE *outfile;

>    printf("Enter the title of your page: ");
>    scanf("%s", title);
>    printf("Your page will be saved as %s.html", title);

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

>    fprintf(outfile, "<html>\n<head><title>%s</title></head>\n");

>    fclose (outfile);
> }

> thats it pretty simple so far. Also d like to know if there is anyway to
> change the file extension of the file created? i.e. to .html instead of
> nothing.

> thanks

> rob bell

Rob, how r ya?

Heres the solution

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

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

#define FALSE 0
#define TRUE 1

int main()
{
  char title [20];
  char *extension;
  FILE *outfile;
  int i = 0;
  int extRequired = TRUE;

  printf("Enter the title of your page: ");
  scanf("%s", title);

  do {
    if (title[i] == '.')
      extRequired = FALSE;
    i++;
  } while (i < strlen(title));

  if (extRequired)
    strcat(title, ".html");

  printf("Your page will be saved as %s", title);
  outfile = fopen (title, "w");

  fprintf(outfile, "<html>\n<head><title>%s</title></head>\n");
  fclose (outfile);

Quote:
}

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


Sun, 30 May 2004 09:54:52 GMT  
 newbie having trouble
--- FIXED ----
sorry, i forgot about your first query

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

#define FALSE 0
#define TRUE 1

int main()
{
  char title [20];
  char *extension;
  FILE *outfile;
  int i = 0;
  int extRequired = TRUE;

  printf("Enter the title of your page: ");
  scanf("%s", title);

  do {
    if (title[i] == '.')
      extRequired = FALSE;
    i++;
  } while (i < strlen(title));

  if (extRequired)
    strcat(title, ".html");

  printf("Your page will be saved as %s", title);
  outfile = fopen (title, "w");

  fprintf(outfile, "<html>\n<head><title>%s</title></head>\n", title);
  fclose (outfile);

Quote:
}



Sun, 30 May 2004 10:00:04 GMT  
 newbie having trouble
hi, heres the solution

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

#define FALSE 0
#define TRUE 1

int main()
{
  char title [20];
  char *extension;
  FILE *outfile;
  int i = 0;
  int extRequired = TRUE;

  printf("Enter the title of your page: ");
  scanf("%s", title);

  do {
    if (title[i] == '.')
      extRequired = FALSE;
    i++;
  } while (i < strlen(title));

  if (extRequired)
    strcat(title, ".html");

  printf("Your page will be saved as %s", title);
  outfile = fopen (title, "w");

  fprintf(outfile, "<html>\n<head><title>%s</title></head>\n", title);
  fclose (outfile);

Quote:
}

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


Sun, 30 May 2004 10:03:15 GMT  
 newbie having trouble

Quote:

> hello, im really lame at c, forgive me for any lame stuff i write, but heres
> my prob:

> i want to create a basic html page based on what the user inputs, i want the
> user to choose a name to be put into the header section, this all works

> everything else outputs fine. heres the code:

> #include <stdio.h>

> main()

int main(void)

Quote:

> {
>  char title [20];
>  FILE *outfile;

>    printf("Enter the title of your page: ");
>    scanf("%s", title);

Very bad idea.  You don't do anything to ensure that the filename
isn't larger than you have space for.  KerPLOW!  There goes your
stack!

...assuming, of course <cough>, that your C implementation has one...

Quote:
>    printf("Your page will be saved as %s.html", title);

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

Pretty{*filter*}y, huh?  Don't need to be bothered with such piddly things
as checking that the call to fopen() succeeded?  ;)

Quote:

>    fprintf(outfile, "<html>\n<head><title>%s</title></head>\n");

Forgot to put the title argument after the format string here.  That's
why you got the garbledy gook.

Quote:
>    fclose (outfile);
> }
> thats it pretty simple so far. Also d like to know if there is anyway to
> change the file extension of the file created? i.e. to .html instead of
> nothing.

Of course.  Have them type the .html, append ".html" to the end of the
string, use strrchr() to find any final '.' that begins an extension
and replace the extension with ".html"...  post an attempt.

Micah

--
Computers are basically very fast idiots.



Sun, 30 May 2004 10:38:52 GMT  
 newbie having trouble

Quote:

> hello, im really lame at c, forgive me for any lame stuff i write, but heres
> my prob:

> i want to create a basic html page based on what the user inputs, i want the
> user to choose a name to be put into the header section, this all works

> everything else outputs fine. heres the code:

> #include <stdio.h>

> main()

> {
>  char title [20];
>  FILE *outfile;

>    printf("Enter the title of your page: ");
>    scanf("%s", title);
>    printf("Your page will be saved as %s.html", title);

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

You are getting garbage printed out because you haven't told this
fprintf statement what to print out. Maybe you want to add "title"
at the end of this statement.

Quote:

>    fprintf(outfile, "<html>\n<head><title>%s</title></head>\n");

>    fclose (outfile);
> }

> thats it pretty simple so far. Also d like to know if there is anyway to
> change the file extension of the file created? i.e. to .html instead of
> nothing.

You can use sscanf or strcat. For example with strcat:
#include <string.h>
.
.
 char title [20];
char h[6] = ".html";
 FILE *outfile;

   printf("Enter the title of your page: ");
   scanf("%s", title);
  strcat(title, h);
.
.

You also need error checking when opening that file by checking if the
filepointer (outfile == NULL).

Maxwelton



Sun, 30 May 2004 11:59:09 GMT  
 newbie having trouble

Quote:
> hi, heres the solution

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

> #define FALSE 0
> #define TRUE 1

Or you could just use 0 and 1.

Quote:

> int main()
> {

I think this has to be: int main(void)

c.l.c gods, is void required in empty parameter lists in the C
standards? What are the legal definitions for main in C89 and C99?
main() is only pre-ANSI (K&R) right? Could anyone clarify please?

Quote:

>   char title [20];
>   char *extension;
>   FILE *outfile;
>   int i = 0;
>   int extRequired = TRUE;

>   printf("Enter the title of your page: ");
>   scanf("%s", title);

See Question 12.20 in the comp.lang.c FAQ!
http://www.eskimo.com/~scs/C-faq/q12.20.html

Quote:

>   do {
>     if (title[i] == '.')
>       extRequired = FALSE;

What if I write "I.like.lots.of.dots"?

Quote:

>     i++;
>   } while (i < strlen(title));

>   if (extRequired)
>     strcat(title, ".html");

if (!strstr(title,".html") && !strstr(title,".htm"))
   strcat(title,".html");

Just seemed a bit easier ;)

Note that strstr is case sensitive, so in this case you have to make the
last 4 characters lowercase for the test to work properly.

Quote:

>   printf("Your page will be saved as %s", title);
>   outfile = fopen (title, "w");

What if fopen fails?

Quote:

>   fprintf(outfile, "<html>\n<head><title>%s</title></head>\n", title);
>   fclose (outfile);

main returns an int. Where is it?
Quote:
> }



Sun, 30 May 2004 15:24:06 GMT  
 newbie having trouble

Quote:


> > int main()

> I think this has to be: int main(void)

> c.l.c gods, is void required in empty parameter lists in the C
> standards?

No. In declarations that are not also definitions, it is taken to mean
"no information about parameters"; in definitions, it is taken to mean
"no parameters", i.e., void.

Quote:
> What are the legal definitions for main in C89 and C99?

  int main(void)
  int main(int argc, char **argv)

or equivalent, including int main().

Quote:
> main() is only pre-ANSI (K&R) right? Could anyone clarify please?

No. Pre-ANSI, void didn't exist, and main() (or int main()) was the only
way to specify a no-parameters main(). In ISO C, any version, main(void)
is allowed as well, and in fact preferred. In C99, implicit int is
disallowed. Summing up, ignoring argc and argv:

K&R:
  main()
  int main()
C89:
  main()
  main(void)
  int main()
  int main(void)
C99:
  int main()
  int main(void)

I look forward to C09, which should abolish K&R-style declarations, and
require int main(void).

Richard



Sun, 30 May 2004 17:22:42 GMT  
 newbie having trouble

Quote:


> > c.l.c gods, is void required in empty parameter lists in the C
> > standards?

> No.

Presumably we should now bow very low and lay our humble offerings at
your feet, yes?

;-)

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Mon, 31 May 2004 04:38:10 GMT  
 newbie having trouble

Quote:



> > > c.l.c gods, is void required in empty parameter lists in the C
> > > standards?

> > No.

> Presumably we should now bow very low and lay our humble offerings at
> your feet, yes?

You may call me Father; I am no c.l.c god, but I pretend to speak for
them.

His Holiness Richard



Mon, 31 May 2004 20:23:34 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Newbie having trouble finding grid samples

2. newbie trouble: CoCreateInstance Error + IWebBrowser trouble...

3. Having trouble converting from VC++ v6.0 to VC.NET

4. Having trouble with Events/Delegates

5. having some trouble directories

6. Having trouble with time_t

7. having trouble with #include<math.h>

8. STILL HAVING TROUBLE WITH FIRST DATES

9. really having trouble with linked lists

10. I am having troubles with the switch()

11. Having trouble with a sort

12. having trouble with return

 

 
Powered by phpBB® Forum Software