Solved

Displaying a file to stdout

Posted on 2000-03-23
3
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

751 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