printf vs fprintf 
Author Message
 printf vs fprintf

I've been trying to read binary files in binary format with fgetc(). But I've
also been trying to printf the input as hex output with printf(). Should I use
fprintf() instead, I've never used it. Is it like printf()?


Sun, 11 Sep 2005 06:39:38 GMT  
 printf vs fprintf

Quote:
> I've been trying to read binary files in binary format with fgetc(). But I've
> also been trying to printf the input as hex output with printf(). Should I use
> fprintf() instead, I've never used it. Is it like printf()?

fprintf() is like printf() but it can print into any output stream, not
just stdout. In short, printf(a, b, c, ...) is equivalent to
fprintf(stdout, a, b, c, ...).

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Products like that make me wish I could menstruate."
   - Andy Richter



Sun, 11 Sep 2005 06:40:56 GMT  
 printf vs fprintf

Quote:

> I've been trying to read binary files in binary format with fgetc(). But I've
> also been trying to printf the input as hex output with printf().

What does your actual printf statement look like?

Quote:
> Should I use fprintf() instead, I've never used it.

Depends...do you want to send the output to some place other then
stdout?

Quote:
> Is it like printf()?

(from k&r)
printf() is equivalent to fprintf( stdout, ... )

--
== Eric Gorr ========= http://www.*-*-*.com/ ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like {*filter*}, are the last refuge of the incompetent... ===



Sun, 11 Sep 2005 06:47:22 GMT  
 printf vs fprintf

Quote:

> I've been trying to read binary files in binary format with fgetc(). But I've
> also been trying to printf the input as hex output with printf(). Should I use
> fprintf() instead, I've never used it. Is it like printf()?

printf(x, ...) is the same as fprintf(stdout, x, ...). fprintf is for when
you want to send it somewhere else, like stderr or a file.

--
Freenet distribution (temporary): http://24.25.175.161:8891/KHacq3VIsa0/
Q:      Who cuts the grass on Walton's Mountain?
A:      Lawn Boy.



Sun, 11 Sep 2005 08:20:01 GMT  
 printf vs fprintf

Quote:

> I've been trying to read binary files in binary format with fgetc().
> But I've also been trying to printf the input as hex output with
> printf(). Should I use fprintf() instead, I've never used it. Is it
> like printf()?

Something nobody's mentioned yet: the 'F' in 'fgetc' stands for
"FUNCTION", but the first 'F' in 'fprintf' stands for "FILE".  That
is:

  printf   PRINTs stufF
  fprintf  PRINTs stufF (to a File)

  getc     GETs a Character from a file (possibly as a macro)
  fgetc    GETs a Character from a file (as a Function)

IOW, fgetc works the same as getc, but fprintf is a completely
different function from printf.

HTH,
-Arthur



Sun, 11 Sep 2005 09:57:00 GMT  
 printf vs fprintf



Quote:


> > I've been trying to read binary files in binary format with fgetc().
> > But I've also been trying to printf the input as hex output with
> > printf(). Should I use fprintf() instead, I've never used it. Is it
> > like printf()?

> Something nobody's mentioned yet: the 'F' in 'fgetc' stands for
> "FUNCTION", but the first 'F' in 'fprintf' stands for "FILE".  That
> is:

This is so full of shit it hurts.

Quote:

>   printf   PRINTs stufF

printf stands for "print formatted", as in it does formatted printing.

Quote:
>   fprintf  PRINTs stufF (to a File)

This is file print formatted

Quote:
>   getc     GETs a Character from a file (possibly as a macro)

nobody should use this.

Tom



Sun, 11 Sep 2005 10:00:01 GMT  
 printf vs fprintf

Quote:
> This is so full of shit it hurts.

> >   printf   PRINTs stufF

> printf stands for "print formatted", as in it does formatted
> printing.

> >   fprintf  PRINTs stufF (to a File)

> This is file print formatted

> >   getc     GETs a Character from a file (possibly as a macro)

> nobody should use this.

Why not? Essentially getc and fgetc are the same except for the multiple
expansion issue. And if getc is a macro, it may be faster than fgetc. It's
not like you often have a side effect in the argument to getc, which could
be a problem in the macro expansion.

--
Simon.



Sun, 11 Sep 2005 10:21:01 GMT  
 printf vs fprintf
stdout is where I want the ouput to go. In hex form.
printf("%x",fp);
Is what I've been trying and fp is a pointer to FILE.


Sun, 11 Sep 2005 14:59:08 GMT  
 printf vs fprintf
This is about what I've been trying.
(in main)
int c;
FILE *fp=NULL;
fp=fopen("File name","rb");
c=fgetc(fp); /*should be &fp? */
if (c!=NULL){fgetc(fp);printf("%x",fp);}
else fclose(fp);

This makes me feel so dumb.



Sun, 11 Sep 2005 15:07:05 GMT  
 printf vs fprintf

Quote:

> stdout is where I want the ouput to go. In hex form.
> printf("%x",fp);
> Is what I've been trying and fp is a pointer to FILE.

I think you are after this (assuming I haven't done anything stupid whilst
writing it):

#include <stdio.h> /* header for fopen, FILE *, printf, etc */

#define LENGTH 16 /* bytes to display per line */

int main(void) /* function declarator */
{
  FILE *fp = NULL;    /* pointer to FILE structure -
                       * set to NULL because I'm paranoid. */
  int ch = 0;         /* we will read data with this */
  size_t count = 0;   /* for checking whether it's time for a newline */

  fp = fopen("filename", "rb"); /* open a file in binary
                                 * mode, for reading */

  if(fp != NULL)  /* Check that the file opened okay */
  {
    /* It /did/ open okay. Great! */
    while((ch = getc(fp)) != EOF) /* keep reading bytes from
                                   * the file. CARE: this line
                                   * has many parentheses. They
                                   * are all necessary. */

    {
      printf(" %02X", ch);        /* print the data to standard output */
      ++count;                    /* keep track of this byte */
      if(count == LENGTH)         /* time for a new line! */
      {
        printf("\n");             /* write a new line */
        count = 0;                /* reset counter */
      }
    }

    if(count != 0)    /* if we have written a partial line... */
    {
      printf("\n");   /* ...then finish off with a newline because it looks
nicer */
    }
    fclose(fp);       /* only close the file if we succeeded in opening it!
*/
  }
  else
  {
    fprintf(stderr, "Sorry, couldn't open the file.\n");
  }

  return 0;

Quote:
}

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Sun, 11 Sep 2005 15:30:15 GMT  
 printf vs fprintf

Quote:
> This is about what I've been trying.
> (in main)
> int c;
> FILE *fp=NULL;
> fp=fopen("File name","rb");
> c=fgetc(fp); /*should be &fp? */
> if (c!=NULL){fgetc(fp);printf("%x",fp);}

Why do you have fgetc(fp) and printf("%x",fp) there?
If you want to print the character you got from fp in hexadecimal,
just use printf("%x",c);.
Note that this will only print the first character in the file. If
you want to print several characters, you will have to use a loop.

Quote:
> else fclose(fp);
> This makes me feel so dumb.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Ice cream sales somehow cause drownings: both happen in summer."
   - Antti Voipio & Arto Wikla


Sun, 11 Sep 2005 15:44:54 GMT  
 printf vs fprintf

Quote:
>Something nobody's mentioned yet: the 'F' in 'fgetc' stands for
>"FUNCTION", but the first 'F' in 'fprintf' stands for "FILE".  That
>is:

>  printf   PRINTs stufF
>  fprintf  PRINTs stufF (to a File)

The final F stands for Formatted, not for stufF.

Quote:
>  getc     GETs a Character from a file (possibly as a macro)
>  fgetc    GETs a Character from a file (as a Function)

The standard doesn't make this distinction between getc and fgetc.
Both of them are available as functions in the standard library and both
of them can also be available as macros defined in <stdio.h>.  The ONLY
difference is that, if implemented as a macro, getc is allowed to evaluate
its argument multiple times, while fgetc must evaluate it only once.
This makes getc a better candidate for macro implementation, but there is
nothing preventing fgetc from being a macro, too.

The idea is that fgetc should be used ONLY if getc cannoti be (e.g.
because its argument contains side effects).  getc has no reason to be
slower than fgetc, but there is one reason (mentioned above) for getc
being faster than fgetc.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sun, 11 Sep 2005 22:21:22 GMT  
 printf vs fprintf

Quote:


>>   getc     GETs a Character from a file (possibly as a macro)

>nobody should use this.

That's news for me.  I've been happily using getc ever since I've learned
C without being aware that I'm doing something wrong.  Please elaborate
(or engage your brain before posting).

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sun, 11 Sep 2005 22:24:03 GMT  
 printf vs fprintf

Quote:


> >Something nobody's mentioned yet: the 'F' in 'fgetc' stands for
> >"FUNCTION", but the first 'F' in 'fprintf' stands for "FILE".  That
> >is:

> >  printf   PRINTs stufF
> >  fprintf  PRINTs stufF (to a File)

> The final F stands for Formatted, not for stufF.

:)

Quote:
> >  getc     GETs a Character from a file (possibly as a macro)
> >  fgetc    GETs a Character from a file (as a Function)

> The standard doesn't make this distinction between getc and fgetc.
> Both of them are available as functions in the standard library and both
> of them can also be available as macros defined in <stdio.h>.  The ONLY
> difference is that, if implemented as a macro, getc is allowed to evaluate
> its argument multiple times, while fgetc must evaluate it only once.

Ah.  I had not remembered that.  Still, as you say, the odds are that
getc is a macro and fgetc isn't, on (dare I say most?) systems.

Secondary and probably silly question: Is there actually any way for the
program to tell that fgetc is or isn't a macro, except through #ifdef or
#if defined (i.e., after the preprocessing stage is complete)?

-Arthur



Mon, 12 Sep 2005 04:15:17 GMT  
 printf vs fprintf
What kind of loop should I try? I've thought of a series of if and else if
statements and a do statement followed by a while statement. I want to see the
entire dump of a file in binary represented in hex.

Bill Cunningham



Mon, 12 Sep 2005 04:41:39 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. printf vs fprintf

2. (void)printf() vs. printf()

3. Re(2): Re(2): (void)printf() vs. printf()

4. Re(2): (void)printf() vs. printf()

5. questions about sprintf, sscanf, printf, scanf, fscanf, fprintf!

6. Differences between printf and fprintf plus stdout

7. replacing printf with my printf, then calling C lib's printf

8. cout vs printf()

9. printf %p vs. %x

10. 2 printf's vs. a ternary

11. cout vs printf

12. printf vs. vprintf

 

 
Powered by phpBB® Forum Software