Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Displaying a file to stdout

Posted on 2000-03-23
3
Medium Priority
?
298 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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

877 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