printf no longer prints text to the console

Discussion in 'Mac Programming' started by RossOliver, Jul 5, 2008.

  1. macrumors regular

    Joined:
    Nov 6, 2006
    #1
    Hey,

    I've managed to bugger my XCode somehow - printf no longer prints anything to the console. I have no idea what I've changed to stop this working - can anyone give me a clue?

    I'm sure it's not the project that's bust since I created a new one and the first thing I put in the applciationDidFinishLaunching method was printf( "something" ) and I got nothing in the console...

    Thanks for your time,

    -Ross
     
  2. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    What happens when you run it from Terminal?
     
  3. macrumors newbie

    Joined:
    Feb 9, 2005
    #3
    if the string doesn't end with a newline character it probably wont be displayed until a newline is sent to that file descriptor.

    So if you really meant "something" and not "something\n" then that is likely to be your problem
     
  4. thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #4
    Upon opening Terminal I was presented with:

    "Could not open a new pseudo-tty."

    I guess something was having a fit - a restart seems to have fixed it :eek:

    Cheers
     
  5. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    Why are you using printf with Cocoa rather than NSLog?
     
  6. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #6
    printf() does not require a newline to print.
     
  7. thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #7
    I tried NSLog when printf wasn't working and it didn't work either. I use printf for temporarily checking variable values and to give indications of where in the code execution is currently at (I know I should really use gdb but I find printing simple messages for some debugging tasks is quicker)...

    -Ross
     
  8. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    The questions still stands... NSLog is fine for debug statements, so why printf with it's occasionally difficult to decipher format specifiers, etc? I don't know if you can print address pointers using NSLog, but that's a rare thing to need to do, I'd say.

    -Lee
     
  9. thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #9

    I guess I'm just used to using printf from programming in C, I haven't really looked at NSLog's capabilities...
     
  10. macrumors regular

    Joined:
    Jan 19, 2006
    #10
    But it forces a flush?
     
  11. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #11
    Nope. There is absolutely no need for a \n in any of your printf() statements if you don't want them.
     
  12. macrumors regular

    Joined:
    Jan 19, 2006
    #12
    According to this it forces a flush http://www.informit.com/articles/article.aspx?p=350919&seqNum=7. If your program terminates and you don't flush the buffer printf won't/may not print on the screen.
     
  13. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #13
    That is the first time I have ever seen anyone claim a \n flushes the buffer for printf(). The man page is pretty detailed and mentions nothing. I'm sceptical but happy to be proved wrong.

    printf() is meant for formatted output, requiring a newline to flush the buffer is pretty stupid given it's use.

    Plus, have you actually tried it? Works fine for me (and I forget to put \n in all the time in my printf() statements).
     
  14. macrumors regular

    Joined:
    Jan 19, 2006
    #14
    Well I experience that behavior a couple of times when i was debugging with printf statements. Some printfs before the segfault code wouldn't print until I added newline characters.

    Edit: I found a more credible source here
     
  15. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #15
    fprintf() and printf()'s buffering is implementation specific. If you need output flushed use flush() and fflush(). Those will flush.

    -Lee
     
  16. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #16
    I'm not sure you can make an association between a newline character and an output stream getting flushed. I suspect that's why there is (in C) a fflush() function. In C++, you have std::end (newline) and std::endl (newline AND flush). Don't know Obj-C yet, but it stands to reason that Obj-C didn't change the name of the game and cause a \n to flush any buffers.

    Todd
     
  17. macrumors regular

    Joined:
    Jan 19, 2006
    #17
    Well I'm looking at glibc source trying to figure out how the buffer is handled. Thank god vfprintf is a 1k line function.
     
  18. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #18
    Copy/paste this into a text file and compile and run it and tell me how a newline character doesn't flush the buffer for stdout:

    Code:
    #include <stdio.h>
    
    main(int argn, char **args) {
    
    	printf("Will this be seen?\t");
    
    	printf("Or will this be seen first?\n");
    
    	printf("Lets use fflush() now.");
    
    	fflush(stdout);
    
    	printf("\n");
    
    	return 0;
    
    }
     
  19. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #19
    Seems fine to me. Works fine if you remove all \n characters and the fflush() function as well.
     
  20. macrumors regular

    Joined:
    Jan 19, 2006
    #20
    Compile and run this :
    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    int main (int argc, char const *argv[])
    {
    	int* go;
    	printf("lol");
    	
    	*go = 1;
    	
    
    	return 0;
    }

    Then complie and run this:

    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    int main (int argc, char const *argv[])
    {
    	int* go;
    	printf("lol\n");
    	
    	*go = 1;
    	
    
    	return 0;
    }
    Do you see the difference in the output?
     
  21. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #21
    What is the point of the go pointer?
     
  22. macrumors regular

    Joined:
    Jan 19, 2006
    #22
    To segfault the program.
     
  23. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #23
    If your program is segfaulting I think the last thing you will worry about is whether your printf() statements are working correctly. Still, I see your point.
     
  24. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #24

Share This Page