public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* gcc and file reading HELP!!
@ 1997-10-26 12:46 Steven I. Pollmann
  1997-10-26 20:18 ` $Bill Luebkert
  0 siblings, 1 reply; 2+ messages in thread
From: Steven I. Pollmann @ 1997-10-26 12:46 UTC (permalink / raw)
  To: gnu-win32

        Sorry about the message, but I'm having a problem with my "c"
program reading lines of text from a file with gnu-win32.  The file compiles
fine, but when it executes, 
the lines are displayed, and then stack errors are reported to the screen at
the end, terminating the program.  The program appears to work fine on the
Unix operating system we have at school.  Please help, I'd like to get this
working with windows.

here is the program:

#include <stdio.h>
#define maxFilename 40

int main()
{
int OpenFile(FILE **filePoint, char fileName [maxFilename]);
void CloseFile(FILE **filePoint);
void PrintFileContents(FILE **filePoint);

char userFile[maxFilename];
FILE *sourcefp;


   printf("Enter a file to open: \n");
   scanf("%s",userFile);
   printf("file \"%s\" is being opened...",userFile);
   
   if (OpenFile(&sourcefp, userFile) == 1)
   {
      printf("file \"%s\" opened.\n",userFile); 
      
      PrintFileContents(&sourcefp);

      CloseFile(&sourcefp);
      printf("file \"%s\" closed.\n",userFile);
   }

return 0;
}


/* open file for reading */

int OpenFile(FILE **filePoint, char fileName [maxFilename])
{
   if ((*filePoint = fopen(fileName,"r")) == NULL)
   {
      printf("Can't open %s for reading \n",fileName);
      return 0;
   }
   else
   {
      *filePoint = fopen(fileName,"r");
      return 1;
   }
}


/*close file */

void CloseFile(FILE **filePoint)
{
   fclose(*filePoint);
}


/* Print File contents */

void PrintFileContents(FILE **filePoint)
{
   char info[20];

   while ((fscanf(*filePoint,"%s",info)) != EOF)
   {
      printf("%s\n",info);

   }
}


-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: gcc and file reading HELP!!
  1997-10-26 12:46 gcc and file reading HELP!! Steven I. Pollmann
@ 1997-10-26 20:18 ` $Bill Luebkert
  0 siblings, 0 replies; 2+ messages in thread
From: $Bill Luebkert @ 1997-10-26 20:18 UTC (permalink / raw)
  To: Steven I. Pollmann; +Cc: gnu-win32

Steven I. Pollmann wrote:
> 
>         Sorry about the message, but I'm having a problem with my "c"
> program reading lines of text from a file with gnu-win32.  The file compiles
> fine, but when it executes,
> the lines are displayed, and then stack errors are reported to the screen at
> the end, terminating the program.  The program appears to work fine on the
> Unix operating system we have at school.  Please help, I'd like to get this
> working with windows.
> 
> here is the program:

More than likely you're overflowing a buffer.

> #include <stdio.h>
> #define maxFilename 40

40 is too small for long filenames.

> int main()
> {
> int OpenFile(FILE **filePoint, char fileName [maxFilename]);
> void CloseFile(FILE **filePoint);
> void PrintFileContents(FILE **filePoint);
> 
> char userFile[maxFilename];
> FILE *sourcefp;
> 
>    printf("Enter a file to open: \n");
>    scanf("%s",userFile);
>    printf("file \"%s\" is being opened...",userFile);
> 
>    if (OpenFile(&sourcefp, userFile) == 1)
>    {
>       printf("file \"%s\" opened.\n",userFile);
> 
>       PrintFileContents(&sourcefp);
> 
>       CloseFile(&sourcefp);
>       printf("file \"%s\" closed.\n",userFile);
>    }
> 
> return 0;
> }
> 
> /* open file for reading */
> 
> int OpenFile(FILE **filePoint, char fileName [maxFilename])
> {
>    if ((*filePoint = fopen(fileName,"r")) == NULL)
>    {
>       printf("Can't open %s for reading \n",fileName);
>       return 0;
>    }
>    else
>    {
>       *filePoint = fopen(fileName,"r");
>       return 1;
>    }
> }
> 
> /*close file */
> 
> void CloseFile(FILE **filePoint)
> {
>    fclose(*filePoint);
> }
> 
> /* Print File contents */
> 
> void PrintFileContents(FILE **filePoint)
> {
>    char info[20];

I would change this ridiculously small buffer from 20 to BUFSIZ.
fscanf is basically a terrible routine in that it can overwrite 
your buffer.

>    while ((fscanf(*filePoint,"%s",info)) != EOF)
>    {
>       printf("%s\n",info);
> 
>    }
> }

HTH,
-- 
  ,-/-  __      _  _         $Bill Luebkert
 (_/   /  )    // //       DBE Collectibles
  / ) /--<  o // //      http://www.wgn.net/~dbe/
-/-' /___/_<_</_</_    Email: dbe@wgn.net
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-10-26 20:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-10-26 12:46 gcc and file reading HELP!! Steven I. Pollmann
1997-10-26 20:18 ` $Bill Luebkert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).