creating files---newbie question 
Author Message
 creating files---newbie question

Hi I appreciate your time--

I Need to write a program which will create files for use on another program    
 I want to be able to type the file name once and have the program create
numbered files automaticly - if i was to type JOE it would create

JOE1
JOE2
JOE3....upto
JOE52

I guess i'm not understanding the while loop properly as i cannot see
why this won't work.
any help would be appreciated.

Matt

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXPATH 64
#include <io.h>
#define total 00
int count;

main(void)
{

        FILE *fp;
        char pathname[MAXPATH];
        int x;
        int count;

 start: ;
        printf("File Name ");
        scanf("%s",&pathname);
        count = 1;

   while (count <= 52)
                count++;

         {
                fp = fopen(pathname,"%d",count,"a+");/*opens file assigns 1-52
*/
                fprintf(fp , "%02d",total); /* sets value of file to 00 */
                fclose(fp);

          }

                printf("\n\nanother? type 1: ");
                scanf("%d",&x);
                if (x == 1)
                  {
                  goto start;
                  }
       else
                return (0);

        }



Fri, 08 Feb 2002 03:00:00 GMT  
 creating files---newbie question

   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #define MAXPATH 64
   #include <io.h>

That's not a standard header file.

   #define total 00

   int count;

   main(void)

Implicit int will be outlawed in C9x, so I suggest that you write `int
main' explicitly.

   {

           FILE *fp;
           char pathname[MAXPATH];
           int x;
           int count;

    start: ;

Since the label `start:' is followed by the printf() call, there's no
need to write ; here.  Also, the use of a label in this context is
questionable at best.

           printf("File Name ");

You should fflush (stdout) to make sure that the prompt appears before
the scanf().

           scanf("%s",&pathname);

That's a poor way to get a line of input, since there's no limit on
the number of characters that can be put into pathname[], possibly
causing undefined behavior.  I suggest use of fgets() instead.

           count = 1;

      while (count <= 52)
                   count++;

That should be inside the block below.

            {
                   fp = fopen(pathname,"%d",count,"a+");/*opens file assigns 1-52
   */

fopen() only takes two arguments.  Your compiler should give you a
diagnostic on that construct.  I suggest use of sprintf() followed by
fopen().

In addition, you should check the return value of fopen().

                   fprintf(fp , "%02d",total); /* sets value of file to 00 */
                   fclose(fp);

You should check for success of these two calls.

             }

                   printf("\n\nanother? type 1: ");

Again, you need to execute fflush (stdout);

                   scanf("%d",&x);
                   if (x == 1)
                     {
                     goto start;
                     }
          else
                   return (0);

That indentation style is ugly.  I suggest checking a style guide for
better ideas.

           }

--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz



Fri, 08 Feb 2002 03:00:00 GMT  
 creating files---newbie question

Quote:


>   #include <stdio.h>
>   #include <stdlib.h>
>   #include <string.h>
>   #define MAXPATH 64
>   #include <io.h>

>That's not a standard header file.

>   #define total 00

>   int count;

>   main(void)

>Implicit int will be outlawed in C9x, so I suggest that you write `int
>main' explicitly.

>   {

>           FILE *fp;
>           char pathname[MAXPATH];
>           int x;
>           int count;

>    start: ;

>Since the label `start:' is followed by the printf() call, there's no
>need to write ; here.  Also, the use of a label in this context is
>questionable at best.

>           printf("File Name ");

>You should fflush (stdout) to make sure that the prompt appears before
>the scanf().

>           scanf("%s",&pathname);

>That's a poor way to get a line of input, since there's no limit on
>the number of characters that can be put into pathname[], possibly
>causing undefined behavior.  I suggest use of fgets() instead.

>           count = 1;

>      while (count <= 52)
>                   count++;

>That should be inside the block below.

>            {
>                   fp = fopen(pathname,"%d",count,"a+");/*opens file assigns
1-52
>   */

>fopen() only takes two arguments.  Your compiler should give you a
>diagnostic on that construct.  I suggest use of sprintf() followed by
>fopen().

>In addition, you should check the return value of fopen().

>                   fprintf(fp , "%02d",total); /* sets value of file to 00 */
>                   fclose(fp);

>You should check for success of these two calls.

>             }

>                   printf("\n\nanother? type 1: ");

>Again, you need to execute fflush (stdout);

>                   scanf("%d",&x);
>                   if (x == 1)
>                     {
>                     goto start;
>                     }
>          else
>                   return (0);

>That indentation style is ugly.  I suggest checking a style guide for
>better ideas.

>           }

>--
>"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz

Thanks for  your help - i made the change with sprintf() but how do you get the
pathname to print in front of numbr

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#define total 00

main(void)
{

        FILE *fp;
        char pathname[10];
        int x;
        int count;

 start: ;
 printf("File Name ");
         scanf("%s",&pathname);

         count = 1;

         while (count <= 5)

         {

                sprintf(pathname,"%d",count);
                count++;
                fp = fopen(pathname,"a+");/*opens file assigns 1-52 */
                fprintf(fp , "%02d",total); /* sets value of file to 00 */
                fclose(fp);

          }

                printf("\n\nanother? type 1: ");
                scanf("%d",&x);
                if (x == 1)
                  {
                  goto start;
                  }
       else
                return (0);

        }

all i get is files created numbered 1 to 5  i do not understand how to get file
name in front of number

ps- i will work on your other suggestions as far as my style - i was using the
style from an old SAMS text



Fri, 08 Feb 2002 03:00:00 GMT  
 creating files---newbie question

: I Need to write a program which will create files for use on another
: program I want to be able to type the file name once and have the program
: create numbered files automaticly - if i was to type JOE it would create

: JOE1
: JOE2
: JOE3....upto
: JOE52

Try this code (uncompiled and untested):

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

#define LINE_LEN  80
#define FILE_NAME_LEN  32
#define MAGIC_NUMBER   52

int main( void )
{
    char line[LINE_LEN],
         *ptr,
         file_name[FILE_NAME_LEN];

    FILE *fp;
    int i;

    fputs(stdout,
          "Enter file basename (keep less than %d characters long): ",
           FILE_NAME_LEN - 3 );
    fflush( stdout );

    if( NULL == fgets(line, sizeof(line), stdin) )
        fputs( "fgets() died. Exiting.", stderr );
        exit( EXIT_FAILURE );
    }

/* squash newline at the end of 'line' */
    if( NULL != (ptr = strchr(line, '\n')) ) *ptr = '\0';

/* use sprintf() to build the file names you want */
    for( i = 1; i <= MAGIC_NUMBER; i++ ) {
       sprintf( file_name, "%s%d", line, i );
       fp = fopen( file_name, "w" );
       if( !fp ) {
           fprintf( stderr, "**Warning** Couldn't open %s. Exiting.\n",
                    file_name );
           exit( EXIT_FAILURE );
       }
/* write some stuff to file if you want, use fprintf( fp, "blah" ); */
       fclose( fp );
   }

   exit( EXIT_SUCCESS );

Quote:
}

Good Luck,

Paul

--

Director, X-ray Structural Facility |   phone: (919) 515-7362
Department of Chemistry - Box 8204  |   FAX:   (919) 515-5079
North Carolina State University     |
Raleigh, NC, 27695-8204
http://laue.chem.ncsu.edu/web/xray.welcome.html



Fri, 08 Feb 2002 03:00:00 GMT  
 creating files---newbie question


Quote:

> Try this code (uncompiled and untested):

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

> #define LINE_LEN  80
> #define FILE_NAME_LEN  32
> #define MAGIC_NUMBER   52

> int main( void )
> {
>     char line[LINE_LEN],
>          *ptr,
>          file_name[FILE_NAME_LEN];

>     FILE *fp;
>     int i;

>     fputs(stdout,
>           "Enter file basename (keep less than %d characters long): ",
>            FILE_NAME_LEN - 3 );

ITYM fprintf, not fputs (fputs takes only two args, the LAST of which is
FILE *).

The rest looked okay though, at a casual glance.

--
Richard Heathfield

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.



Fri, 08 Feb 2002 03:00:00 GMT  
 creating files---newbie question


Quote:

>: I Need to write a program which will create files for use on another
>: program I want to be able to type the file name once and have the program
>: create numbered files automaticly - if i was to type JOE it would create

>: JOE1
>: JOE2
>: JOE3....upto
>: JOE52

...

Quote:
>       sprintf( file_name, "%s%d", line, i );

It is very often neater to have names of the form

   JOE01
   JOE02
   JOE03
   JOE52

If you want that replace %d with %02d or %0.2d

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


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



Fri, 08 Feb 2002 03:00:00 GMT  
 creating files---newbie question
: >
: >     fputs(stdout,
: >           "Enter file basename (keep less than %d characters long): ",
: >            FILE_NAME_LEN - 3 );

: ITYM fprintf, not fputs (fputs takes only two args, the LAST of which is
: FILE *).

: The rest looked okay though, at a casual glance.

<homer_simpson attribute=forehead_auto_slap>
Doh!
</homer_simpson>

As you know Richard, this is one of my favorite errors to make.
Thanks for pointing it out.  What happens is that I begin writing the
line one way, then I think it would be cooler to give a more informative
message, and I forget to change the function call to reflect that change
in thinking.

Paul

--

Director, X-ray Structural Facility |   phone: (919) 515-7362
Department of Chemistry - Box 8204  |   FAX:   (919) 515-5079
North Carolina State University     |
Raleigh, NC, 27695-8204
http://laue.chem.ncsu.edu/web/xray.welcome.html



Fri, 08 Feb 2002 03:00:00 GMT  
 creating files---newbie question


Quote:

> : >
> : >     fputs(stdout,
> : >           "Enter file basename (keep less than %d characters long):
",
> : >            FILE_NAME_LEN - 3 );

> : ITYM fprintf, not fputs (fputs takes only two args, the LAST of which
is
> : FILE *).

> : The rest looked okay though, at a casual glance.

> <homer_simpson attribute=forehead_auto_slap>
> Doh!
> </homer_simpson>

> As you know Richard, this is one of my favorite errors to make.
> Thanks for pointing it out.  What happens is that I begin writing the
> line one way, then I think it would be cooler to give a more informative
> message, and I forget to change the function call to reflect that change
> in thinking.

That's how it happens to me too. And that's why I've become relatively good
at spotting the error!

At least, when I used puts("%s", blah), I only screwed up on a Website in
front of a few hundred people. You managed it in front of several thousand.
:-)



Sat, 09 Feb 2002 03:00:00 GMT  
 creating files---newbie question
Quote:
> Hi I appreciate your time--<BR>
> <BR>
> I Need to write a program which will create files for use on

another program <BR>
Quote:
>  I want to be able to type the file name once and have the
program create <BR>
> numbered files automaticly - if i was to type JOE it would
create <BR>
> <BR>
> JOE1<BR>
> JOE2 <BR>
> JOE3....upto<BR>
> JOE52<BR>
> <BR>
> I guess i'm not understanding the while loop properly as i
cannot see <BR>
> why this won't work.<BR>
> any help would be appreciated.<BR>
> <BR>
> Matt<BR>
> <BR>
> #include <stdio.h><BR>
> #include <stdlib.h><BR>
> #include <string.h><BR>
> #define MAXPATH 64<BR>
> #include <io.h><BR>
> #define total 00<BR>
> int count;<BR>
> <BR>
> main(void)<BR>
> {<BR>
> <BR>
>  FILE *fp;<BR>
>  char pathname[MAXPATH];<BR>
>  int x;<BR>
>  int count;<BR>
> <BR>
> <BR>
>  start: ;<BR>
>  printf("File Name ");<BR>
>  scanf("%s",&pathname);<BR>
>  count = 1;<BR>
> <BR>
>    while (count <= 52)<BR>
>   count++;<BR>
> <BR>
>   {<BR>
>   fp = fopen(pathname,"%d",count,"a+");/*opens file assigns
1-52 <BR>
> */<BR>
>   fprintf(fp , "%02d",total); /* sets value of file to 00
*/<BR>
>   fclose(fp);<BR>
> <BR>
>    }<BR>
> <BR>
> <BR>
>   printf("\n\nanother? type 1: ");<BR>
>   scanf("%d",&x);<BR>
>   if (x == 1)<BR>
>     {<BR>
>     goto start;<BR>
>     }<BR>
>        else<BR>
>   return (0);<BR>
>   <BR>
>  }<BR>
> <BR>
> <BR>

--------------end of your posting-------------------
Quote:
> scanf("%s",&pathname);<BR>

pathname is already a pointer ...
In C array variables are pointers to the first element of the
array.
so you can write 'scanf("%s",pathname);'
Quote:
>       count = 1;
>    while (count <= 52)
>               count++;

your first file will be named "...2"
since you start with 1 and add 1 before doing anything else.
Quote:
>fp=fopen(pathname,"%d",count,"a+");

the "," seperates arguments from each other ..
you should construct the pathname first and call fopen with the
complete
name.
sprintf(realname,"%s%d",pathname,count);
fp = fopen(realname,"a+");
Quote:
> fprintf(fp , "%02d",total); /* sets value of file to 00 */

you dont actually set the "value" of the file to 00 ... you
just
write "00" in that file.
but i guess thats what you want to do.
Quote:
>  goto start;

using "goto" is very bad style...
why not use a loop like that one:
  do {
    all the stuff ...
    the continue question ...
  } while (x==1);
hope that helped ...
-heinrich
anyway .. here's a simple program that does the same ..
#include <stdio.h>
int number;
int i,x;
FILE *f;
char name[64];
char realname[64];

int main( ) {
  do {
    printf("name? ");
    scanf("%s", name);
    printf("number? ");
    scanf("%d", &number);
    for (i=1;i<=number;i++){
      sprintf(realname,"%s%d",name,i);
      f=fopen(realname,"a+");
      fprintf(f,"%02d",0);
      fclose(f);
    }
    printf("\n\nanother? type 1: ");
    scanf("%d",&x);
  } while (x==1);
  return 0;

Quote:
}

--
  This answer is courtesy of QuestionExchange.com
  http://www.questionexchange.com/servlet1/showUsenetGuest?ans_id=3002&...


Sat, 09 Feb 2002 03:00:00 GMT  
 creating files---newbie question
QuestionExchange schrieb:

Quote:

> >  char pathname[MAXPATH];<BR>

> > scanf("%s",&pathname);<BR>
> pathname is already a pointer ...
> In C array variables are pointers to the first element of the
> array.

It is not that simple. "pathname" is an array of characters. It is not a
pointer and you should not think of it as a pointer. It is an array and
the name of an array when used in an expression context will generate a
pointer to the first element of the array.

As an example where your wrong assumption will hurt you, think of
"sizeof". Using "sizeof" on an array will give you the size of the whole
array, not the aize of a pointer.

There is an excellent explanation of this subject in the comp.lang.c FAQ
list. Have a look at setion 6: "Arrays and Pointers".

Quote:
> so you can write 'scanf("%s",pathname);'

That OTOH is correct. But for the reason I stated above.

Quote:
> >       count = 1;
> >    while (count <= 52)
> >               count++;
> your first file will be named "...2"

The situation is a bit more funny here. His first and only file will be
named "...53", because the "count++;" is the only thing inside of the
loop.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 10 Feb 2002 03:00:00 GMT  
 creating files---newbie question

This is what ended up working perfectly- This newbie has alot to learn -
most of my books do not even address "sprintf" or "fflush"
Time for some new Books !!

Thanks again for all your replies

ps. reason for  setting the file at a 00 value - is that the other program
i use accesses the file and uses  fseek(fp, -2, SEEK_END); to gain data from
file to use in program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define total 00
#define LINE_LEN 80
#define FILE_NAME_LEN 32
#define MAGIC_NUMBER 52

int main (void)
{
        char line[LINE_LEN],
         *ptr,
         file_name[FILE_NAME_LEN];

         FILE *fp;
         int i;

         fprintf(stdout,"Enter file basename:", FILE_NAME_LEN );
         fflush(stdout);

         if(NULL == fgets(line, sizeof(line), stdin))
         {
                fputs ( "fgets() died. exiting.", stderr);

         }

         if(NULL != (ptr = strchr(line,'\n')))  *ptr = '\0';

         for (i = 1; i <= MAGIC_NUMBER; i++)
                {
                sprintf( file_name, "%s%d", line, i);
                fp = fopen(file_name,"a+");
                fprintf(fp,"%02d",total);
                        fclose(fp);
                }
                if(!fp)

                        {
                        fprintf(stderr,"warning could not open %s. Exiting.\n",
                        file_name);
                        exit(EXIT_FAILURE);
                        }

Quote:
}


Quote:

>QuestionExchange schrieb:

>> >  char pathname[MAXPATH];<BR>

>> > scanf("%s",&pathname);<BR>
>> pathname is already a pointer ...
>> In C array variables are pointers to the first element of the
>> array.

>It is not that simple. "pathname" is an array of characters. It is not a
>pointer and you should not think of it as a pointer. It is an array and
>the name of an array when used in an expression context will generate a
>pointer to the first element of the array.

>As an example where your wrong assumption will hurt you, think of
>"sizeof". Using "sizeof" on an array will give you the size of the whole
>array, not the aize of a pointer.

>There is an excellent explanation of this subject in the comp.lang.c FAQ
>list. Have a look at setion 6: "Arrays and Pointers".

>> so you can write 'scanf("%s",pathname);'

>That OTOH is correct. But for the reason I stated above.

>> >       count = 1;
>> >    while (count <= 52)
>> >               count++;
>> your first file will be named "...2"

>The situation is a bit more funny here. His first and only file will be
>named "...53", because the "count++;" is the only thing inside of the
>loop.

>Stephan
>(initiator of the campaign against grumpiness in c.l.c)



Sun, 10 Feb 2002 03:00:00 GMT  
 creating files---newbie question

: This is what ended up working perfectly- This newbie has alot to learn -
: most of my books do not even address "sprintf" or "fflush"
: Time for some new Books !!

: Thanks again for all your replies

: ps. reason for  setting the file at a 00 value - is that the other program
: i use accesses the file and uses  fseek(fp, -2, SEEK_END); to gain data from
: file to use in program

: #include <stdio.h>
: #include <stdlib.h>
: #include <string.h>
: #define total 00
: #define LINE_LEN 80
: #define FILE_NAME_LEN 32
: #define MAGIC_NUMBER 52

: int main (void)
: {
:       char line[LINE_LEN],
:        *ptr,
:        file_name[FILE_NAME_LEN];

:        FILE *fp;
:        int i;

:        fprintf(stdout,"Enter file basename:", FILE_NAME_LEN );
A little problem here, MATT.  In your format specifier for the fprintf()
statement you don't have a conversion specifier.  You probably want either,
(a)
     fputs( "Enter file basename: ", stdout );
-- or --
(b)
     fprintf( stdout, "Enter file basename (use %d chars max): ",
                      FILE_NAME_LEN - 3);
You want the '- 3' because you need to make room for the digits you
plan to append to the filename using sprintf() and also to make room for
the '\0' terminating character.

:        fflush(stdout);

:        if(NULL == fgets(line, sizeof(line), stdin))
:        {
:               fputs ( "fgets() died. exiting.", stderr);

:        }

:        if(NULL != (ptr = strchr(line,'\n')))  *ptr = '\0';

:        for (i = 1; i <= MAGIC_NUMBER; i++)
:               {
:               sprintf( file_name, "%s%d", line, i);
:               fp = fopen(file_name,"a+");

Here is a problem.  You  should check for fopen() errors immediately after
the fopen() and using the FILE * in an I/O operation.

:                 fprintf(fp,"%02d",total);
:                       fclose(fp);
:               }

So, move the below block of code to just after the fopen() and before the
fprintf().

:               if(!fp)
:                  {
:                   fprintf(stderr,"warning could not open %s. Exiting.\n",
:                           file_name);
:                   exit(EXIT_FAILURE);
:                  }

:        
: }

Good Luck,

Paul

--

Director, X-ray Structural Facility |   phone: (919) 515-7362
Department of Chemistry - Box 8204  |   FAX:   (919) 515-5079
North Carolina State University     |
Raleigh, NC, 27695-8204
http://laue.chem.ncsu.edu/web/xray.welcome.html



Sun, 10 Feb 2002 03:00:00 GMT  
 creating files---newbie question
Groovy hepcat MATT was jivin' on Mon, 23 Aug 1999 14:18:56 GMT in
comp.lang.c.
creating files---newbie question's a cool scene! Dig it!

Quote:
>I Need to write a program which will create files for use on another program        
> I want to be able to type the file name once and have the program create
>numbered files automaticly - if i was to type JOE it would create

>JOE1
>JOE2
>JOE3....upto
>JOE52

>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>#define MAXPATH 64
>#include <io.h>

  This header is non-standard, and AFAICT isn't even needed in this
program.

Quote:
>#define total 00
>int count;

  IIWY, I'd avoid global variables. Your count variable doesn't need
to be global. You'd be better off, IMHO, putting it within main(). In
fact, you have done just that, so this variable at file scope is
totally redundant.

Quote:
>main(void)

  It's better to be explicit about main()'s return type, since the
next version of the standard will require this.

int main(void)

Quote:
>{

>    FILE *fp;
>    char pathname[MAXPATH];
>    int x;
>    int count;

> start: ;

  Ick! Get rid of this. (See below.)

Quote:
>    printf("File Name ");

  You should call fflush() here to make sure the prompt is displayed.
It may not be, depending on your platform and compiler, since stdout
is usually line buffered.

        fflush(stdout);

Quote:
>       scanf("%s",&pathname);

                   ^
  The address-of operator is superfluous here, since the name of an
array "decays" to a pointer.
  Anyhow, scanf() is not the best function to use for interactive
input. The prefered function for this is fgets().

        fgets(pathname, sizeof pathname, stdin);

Quote:
>    count = 1;

>   while (count <= 52)

  Poor indenting style, mixing tabs with spaces, thus resulting in
dischordant indentation. Tabs are expanded differently on different
software. So what may look perfectly OK on your text edidor looks bad
on my news client.

Quote:
>            count++;

  And here is the end of the loop. The loop has only one statement in
it, and that only increments count. So by the time the following code
is executed, the loop has already finished, and count is 53. I think
you intended this to go after the curly brace below, did you not?

Quote:
>     {
>            fp = fopen(pathname,"%d",count,"a+");/*opens file assigns 1-52

  And this is an error. fopen() is not a variadic function like
printf(). It takes a fixed number and fixed types of arguments. You
are passing too many arguments to fopen(). What you need is another
function to convert count to string format and concatenate that onto
pathname - before you call fopen(). There is such a function. Look up
sprintf() in your C manual.
  Also, you must remember to check the value returned from fopen(). If
it is NULL, then the file failed to open, so you should quit or
something. WARNING: don't try to use fp if it is NULL. You should
*always* check what certain functions return: malloc(), calloc(),
realloc(), fopen(), freopen() and tmpfile(), just to name a few.

Quote:
>*/
>            fprintf(fp , "%02d",total); /* sets value of file to 00 */
>            fclose(fp);
>      }
>            printf("\n\nanother? type 1: ");

  Again, use fflush() to ensure the prompt is flushed.

                fflush(stdout);

Quote:
>            scanf("%d",&x);

  And again, scanf() sux! It's better to use fgets().

Quote:
>            if (x == 1)
>              {
>              goto start;

  goto sux! Never use it unless there's no practical way not to. I
don't remember ever having seen a case in which goto was warranted,
although I've heard that there have been a couple of such cases. For
your purpose, there is a very practical way not to use goto. Use a
do...while loop instead. (See my code below.)

Quote:
>              }
>       else
>            return (0);

  The parentheses are unnecessary here (though not an error).

Quote:
>    }

  I've rewritten your code so that it works as expected. Here it is:

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

#define TOTAL 0

int yesno(const char *prompt);
char *readline(char *buf, size_t size, const char *prompt);

int main(void)
{
  int count;
  FILE *fp;
  char filename[FILENAME_MAX];
  char filespec[FILENAME_MAX - 2];

  /* get the filespec - with error checking */
  if(!readline(filespec, sizeof filespec, "File Name: "))
    return EXIT_FAILURE;

  count = 1;
  do
  {
    /* create filename from filespec & count */
    sprintf(filename, "%s%d", filespec, count);

    /* open the file - with error checking */
    fp = fopen(filename, "w");
    if(!fp)
    {
      fprintf(stderr, "Unable to open file %s.\n", filename);
      return EXIT_FAILURE;
    }

    /* write to file - with error checking */
    if(fprintf(fp , "%02d", TOTAL) < 0)
    {
      fprintf(stderr, "Could not write to file %s.\n", filename);
      fclose(fp);
      return EXIT_FAILURE;
    }

    fclose(fp);

  /* loop if count < 52 and user wants to continue */
  }while(count++ < 52 && yesno("\n\nAnother?"));

   return 0;

Quote:
}

int yesno(const char *prompt)
{
  char buf[10];
  char *p;

  /* display prompt */
  printf("%s (Y|N): ", prompt);
  fflush(stdout);

  /* get input from user */
  if(!(p = readline(buf, sizeof buf, NULL)))
    exit(EXIT_FAILURE);

  /* return true if first char is 'y' or 'Y' */
  return tolower(p[0]) == 'y';

Quote:
}

char *readline(char *buf, size_t size, const char *prompt)
{
  char *p;

  /* if prompt supplied, display it */
  if(prompt)
  {
    printf("%s", prompt);
    fflush(stdout);
  }

  /* get input - with error checking */
  if(!fgets(buf, size, stdin))
  {
    fprintf(stderr, "\nCouldn't read from stdin.\n");
    return NULL;
  }

  /* remove trailing newline from input */
  if(NULL != (p = strchr(buf, '\n')))
    *p = '\0';

  return buf;

Quote:
}

--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Tue, 12 Feb 2002 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Newbie question: using malloc() to create a string?

2. Help on creating Tables, Fields, Etc...(newbie question)

3. ATL newbie creating VB like collection question

4. Help, Newbie Question - create a new window out of old window

5. Newbie question on ActiveX creating ActiveX

6. Newbie question on creating worker threads

7. newbie: CDialog::Create question

8. newbie: CDialog::Create question

9. Creating DLL - newbie question

10. Newbie Question: Painting Dynamically created CWnds

11. Help, Newbie Question - create a new window out of old window

12. Creating Buttons? (newbie question)

 

 
Powered by phpBB® Forum Software