Solved

Displaying a file to stdout

Posted on 2000-03-23
3
236 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

Do email signature updates give you a headache?

Are you constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Question has a verified solution.

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

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…
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 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.

895 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

15 Experts available now in Live!

Get 1:1 Help Now