Odd behavior: explanation?


#1

Hello, I entered this simple program in the text (below). When I compile and run, sometimes I get the full output, and sometimes I don’t. I don’t understand why! Here’s the code:

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    NSDate *now = [NSDate date];
    NSLog(@"The date is %@", now);
            
    double seconds = [now timeIntervalSince1970];
    NSLog(@"It has been %.0f seconds since the start of 1970.", seconds);
    
    NSDate *later = [now dateByAddingTimeInterval:100000];
    NSLog(@"In 100,000 seconds it will be %@", later);
    
}
return 0;

}
[/code]

Here are the different output examples:

  1. Without the "In 100,000 seconds…"
    2012-07-03 12:48:58.615 TimeAfterTime[15291:403] The date is 2012-07-03 19:48:58 +0000
    2012-07-03 12:48:58.616 TimeAfterTime[15291:403] It has been 1341344939 seconds since the start of 1970.

  2. With the "In 100,000 seconds…"
    2012-07-03 12:52:59.694 TimeAfterTime[15300:403] The date is 2012-07-03 19:52:59 +0000
    2012-07-03 12:52:59.695 TimeAfterTime[15300:403] It has been 1341345180 seconds since the start of 1970.
    2012-07-03 12:52:59.695 TimeAfterTime[15300:403] In 100,000 seconds it will be 2012-07-04 23:39:39 +0000

I didn’t touch the code between compiles! Can anyone explain this? It’s kinda frustrating!

Thanks,

Barry


#2

This is a very frequently asked question.

Before the logs make it to the console, your program is exiting to quickly (when running under Xcode’s control.)

You need to flush the process’s output streams, by calling [color=#FF0000]fflush (NULL)[/color]:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        NSDate *now = [NSDate date];
        NSLog(@"The date is %@", now);
                
        double seconds = [now timeIntervalSince1970];
        NSLog(@"It has been %.0f seconds since the start of 1970.", seconds);
        
        NSDate *later = [now dateByAddingTimeInterval:100000];
        NSLog(@"In 100,000 seconds it will be %@", later);
    }
    fflush (NULL);
    return 0;
}

If the problem still persists, see your doctor. No, just, kidding. I meant: open up the terminal application and run the program from the command line.

What is fflush ()?
Open up the Terminal Application, and enter man fflush:


#3

@ ibex10

Oh man, I really want to Believe! I immediately added fflush (NULL); just prior to return(0), recompiled and ran… and it all looked good. I then got excited and started recompiling and running, repeatedly - only to find that once again I get the same behavior I saw earlier.

I’m not sure exactly what you mean by running it manually from the Terminal application. Would you show me the CLI command, please? The project I’m editing is dateList.xcodeproj if that helps.

Thanks so much,

Barry


#4

To run a Command Line Tool (a program), built by Xcode, manually from the command line: locate the folder (or directory) containing the program, start the Terminal Application (for a command line process), change the default working directory of the command line process to the folder (or directory) containing the program, and execute the program.

Let’s say that you have built a project named fflush for such a program; here is one way to run it manually from the command line (using Xcode 4.3.2):

  1. Click on the Products group in Xcode navigator.
    You should see the name of your program: fflush.
  2. Select it (and keep it selected.)
  3. Show the Utilities and click on the File Inspector tab.
    In the Identity and Type section, you should see fflush and its full path in the file system.
    After the full path, you should see also a tiny right-pointing arrow.
  4. Copy the path to the folder containing fflush:
  • In Xcode, select the full path of the folder and copy (Command-C) it to the pasteboard; or
  • Click the tiny arrow to open the containing folder (Debug or Release.); a finder Window should open for the folder containing your program. In the Finder window, navigate one level up to the Products folder; select the folder named Debug and copy it (Command-C) to the pasteboard.
  1. Run (launch) the Terminal Application
    A window should open for your command line process which you must feed manually.
  2. Change the default working directory of the command line process to the folder containing your program:
    cd
    Where you type in cd verbatim and replace with the content of your pasteboard from step 4.
  3. Hit on your keyboard.
    Now, if you type in ls (little l and little s (short for list)) and hit the return key, you will see the name of your program.
  4. Execute the program by typing in its name and hitting the key.
    ./fflush
    Now you should see its output.

Here is the output:

2012-07-05 00:38:39.679 fflush[10348:503] int main(int, const char **): 16
2012-07-05 00:38:39.680 fflush[10348:503] int main(int, const char **): 15
2012-07-05 00:38:39.681 fflush[10348:503] int main(int, const char **): 14
2012-07-05 00:38:39.681 fflush[10348:503] int main(int, const char **): 13
2012-07-05 00:38:39.682 fflush[10348:503] int main(int, const char **): 12
2012-07-05 00:38:39.682 fflush[10348:503] int main(int, const char **): 11
2012-07-05 00:38:39.683 fflush[10348:503] int main(int, const char **): 10
2012-07-05 00:38:39.683 fflush[10348:503] int main(int, const char **): 9
2012-07-05 00:38:39.683 fflush[10348:503] int main(int, const char **): 8
2012-07-05 00:38:39.684 fflush[10348:503] int main(int, const char **): 7
2012-07-05 00:38:39.684 fflush[10348:503] int main(int, const char **): 6
2012-07-05 00:38:39.685 fflush[10348:503] int main(int, const char **): 5
2012-07-05 00:38:39.685 fflush[10348:503] int main(int, const char **): 4
2012-07-05 00:38:39.685 fflush[10348:503] int main(int, const char **): 3
2012-07-05 00:38:39.686 fflush[10348:503] int main(int, const char **): 2
2012-07-05 00:38:39.686 fflush[10348:503] int main(int, const char **): 1

Of the program in a project named fflush:

//
//  main.m
//

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSUInteger n = 16;
        while (n)
           NSLog (@"%s: %lu", __PRETTY_FUNCTION__, n--);
    }
    fflush (NULL);      
    return 0;
}

#5

Hello ibex10 dear friend,

Thanks so very much for your detailed and instructional guidance. I followed it all, and it really helps a lot. Hopefully one of these days I’ll know enough that I can return favors of this kind to others.

Best regards,

Barry