Solved

Displaying a file to stdout

Posted on 2000-03-23
3
224 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
Comment Utility
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
Comment Utility
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
Comment Utility
Cheers all! Got it!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now