Solved

Displaying a file to stdout

Posted on 2000-03-23
3
255 Views
Last Modified: 2010-04-15
Ok I have written this function but it doesn't work! Can you tell me why, what I need to do to fix it - or if there is a better way of doing it?

Cheers

/* Function */

int OpenFile (void)
{
FILE *read_rules;
int character;

      if ((read_rules = fopen(FILE_NAME, "r")) == NULL)
      {
            fprintf (stderr, "Failed to open %s\n", FILE_NAME);
            perror ("Because");

            return ERROR;
      }
      do
      {
            fgets (stdout, , read_rules);
      } while (!foef(read_rules));
      fclose (FILE_NAME);

      return SUCCESS;
}

/* And how it is called from main */

char *question;

      printf ("Do you wish to read the help file? [y/n] ");
      if (scanf ("%c", &question) == 'y')
      {
            if (OpenFile ())
            {
                  printf ("Error calling function OpenFile\n");

                  return ERROR;
            }
      }

If the formatting is messed up sorry - but it's the browser not me!

Jim
0
Comment
Question by:Jim2000
3 Comments
 
LVL 4

Accepted Solution

by:
feenix earned 100 total points
ID: 2650764
It doesn't work because fgets wants a pointer as its first parameter, not a file handle. The easiest way would probably be to define a buffer and use that. For example:

//in the beginning
char buf[256];

//the while loop
do
{
fgets (buf, 255 , read_rules);
fputs(buf, stdout);
} while (!foef(read_rules));

0
 
LVL 3

Expert Comment

by:marcjb
ID: 2653666
Just a comment on efficiency:
fgets returns NULL on EOF, so there is no need to do separate checks on EOF.

while ( fgets(buf, 255, read_rules) != NULL )
    printf("%s", buf);

This should print your file for you.  Also, in the fgets call, fgets actuall reads n - 1 characters, so you don't automatically have to subtract 1 from the size of what you are reading in.  For example, in the comment above, the array buf is 256 in size.  You can call fgets with 256, and there will be no problem.  This is useful if you decide to use #defines for array size:

#define  CMAX 256
char buf[CMAX];

while ( fgets (buf, CMAX , read_rules) != NULL )
    printf("%s", buf);


Hope this helps,

marc
0
 

Author Comment

by:Jim2000
ID: 2654881
Cheers all! Got it!
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
My eclipse editor won't start ? 4 162
Read file descriptor 3 in C 14 113
Finding a good hash function 4 127
Using popen() and gunzip() to open file in HTTPServer 6 67
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question