Thread: printing error using fprintf

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    25

    printing error using fprintf

    i don't understand the difference between using

    fprintf and printf for error statements.

    e.g.

    Code:
    void mergeSort(float array[], int size)
    {
      int* tmpArrayPtr = (int*)malloc(size*sizeof(int)); 
    
      if (tmpArrayPtr != NULL) 
      {
        mergeSortRec(array, size, tmpArrayPtr);
      }
      else
      {
        fprintf(stderr, “Not enough memory to sort list.\n”);
        exit(1);
      }
    
      free(tmpArrayPtr);
    }
    as to ->

    Code:
    void mergeSort(float array[], int size)
    {
      int* tmpArrayPtr = (int*)malloc(size*sizeof(int)); 
    
      if (tmpArrayPtr != NULL) 
      {
        mergeSortRec(array, size, tmpArrayPtr);
      }
      else
      {
        printf(“Not enough memory to sort list.\n”);
        exit(1);
      }
    
      free(tmpArrayPtr);
    }

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    fprintf allows you to tell it where to print it. In this case, it prints it to stderr rather than stdout. Normally, you see stderr and stdout going to the same place (the screen) but they can be redirected to different places such as a file.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    printf() prints to stdout, and fprintf() you used prints to stderr. In the good old days, programs would write output to stdout that a majority of the time people wanted to store in a file. Take UNIX's sort command for instance. It writes the sorted data to stdout so a lot of people would do something like sort > somefile.txt. That worked fine unless there was an error. Redirecting output using > only redirects stdout. So by printing to stderr, the error messages would go to the terminal instead of getting stuck in the file.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    25
    oohhh i see. so fprintf gives you more control over where it is being directed to either stderr or stdout. even though printf uses stdout, i don't see how it'd get stuck in a file, as it always prints to the screen?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    But stdout doesn't print on the screen if you redirect it. Look:
    Code:
    itsme@itsme:~/C$ cat spoon.c
    #include <stdio.h>
    
    int main(void)
    {
      printf("I like spoons.\n");
      fprintf(stderr, "You like spoons.\n");
    
      return 0;
    }
    itsme@itsme:~/C$ ./spoon
    I like spoons.
    You like spoons.
    itsme@itsme:~/C$ ./spoon > spoon.txt
    You like spoons.
    itsme@itsme:~/C$ cat spoon.txt
    I like spoons.
    itsme@itsme:~/C$
    If I didn't fprintf() to stderr on the second line then it would have gotten stuck in spoon.txt along with the first line.
    If you understand what you're doing, you're not learning anything.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    itsme86 showed how to do it in his post (in bold)

    edit: never mind
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Look at this:
    Code:
    itsme@itsme:~/C$ cat list.txt
    carrot
    apple
    donkey
    banana
    itsme@itsme:~/C$ sort list.txt
    apple
    banana
    carrot
    donkey
    itsme@itsme:~/C$ sort list.txt > sortedlist.txt
    itsme@itsme:~/C$ sort list.duh > sortedlist.txt
    sort: open failed: list.duh: No such file or directory
    itsme@itsme:~/C$
    If sort printed the error to stdout it would have just been thrown in sortedlist.txt in that last attempt.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So in other words, you should use printf() for regular ouput, and fprintf(stderr, "error") for error messages.

    Note that
    Code:
    printf("Hello, World!\n");
    Is the same as
    Code:
    fprintf(stdout, "Hello, World!\n");
    They both print to stdout.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. fprintf in void function Not Printing
    By thetinman in forum C Programming
    Replies: 5
    Last Post: 10-17-2006, 05:13 PM
  3. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  4. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  5. formatted printing with fprintf
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 11-20-2001, 09:04 AM