-1

I'm doing a small app for evaluating and analyzing transfer functions. As boring as the subject might seem to some, I want it to at least look extra cool and pro and awesome etc... So:

  • Step 1: Gimme teh coefficients! [A bunch of numbers]
  • Step 2: I'll write the polynomial with its superscripts. [The bunch of numbers in a string]

So, I write a little C parser to just print the polynomial with a decent format, for that I require a wchar_t string that I concatenate on the fly. After the string is complete I quickly try printing it on the console to check everything is ok and keep going. Easy right? Welp, I ain't that lucky...

wchar_t *polynomial_description( double *polyArray, char size, char var ){

    wchar_t *descriptionString, temp[100];
    int len, counter = 0;
    SUPERSCRIPT superscript;

    descriptionString = (wchar_t *) malloc(sizeof(wchar_t) * 2);
    descriptionString[0] = '\0';

    while( counter < size ){

        superscript = polynomial_utilities_superscript( size - counter );
        len = swprintf(temp, 100, L"%2.2f%c%c +", polyArray[counter], var, superscript);
        printf("temp size: %d\n", len);

        descriptionString = (wchar_t *) realloc(descriptionString, sizeof(wchar_t) * (wcslen(descriptionString) + len + 1) );
        wcscat(descriptionString, temp);

        counter++;

    }

    //fflush(stdout); //Already tried this
    len = wprintf(L"%ls\n", descriptionString);
    len = printf("%ls**\n", descriptionString);
    len = fprintf(stdout, "%ls*\n", descriptionString);
    len = printf("FFS!! Print something!");

    return descriptionString;

}

During the run we can see temp size: 8 printed the expected number of times ONLY WHILE DEBUGGING, if I run the program I get an arbitrary number of prints each run. But after that, as the title states, wprintf, printf and fprintf don't print anything, yet len does change its size after each call.

In the caller function, (application:(UIApplication *)application didFinishLaunchingWithOptions:, while testing) I put an NSLog to print the return string, and I dont get ANYTHING not even the Log part.

What's happening? I'm at a complete loss.

Im on XCode 4.2 by the way.

whtlnv
  • 2,109
  • 1
  • 25
  • 26

1 Answers1

2

What's the return value from printf/wprintf in the case where you think it's not printing anything? It should be returning either -1 in the case of a failure or 1 or more, since if successful, it should always print at least the newline character after the description string.

  • If it's returning 1 or more, is the newline getting printed? Have you tried piping the output of your program to a hex dumper such as hexdump -C or xxd(1)?
  • If it's returning -1, what is the value of errno?

If it turns out that printf is failing with the error EILSEQ, then what's quite likely happening is that your string contains some non-ASCII characters in it, since those cause wcstombs(3) to fail in the default C locale. In that case, the solution is to use setlocale(3) to switch into a UTF-8 locale when your program starts up:

int main(int argc, char **argv)
{
    // Run "locale -a" in the Terminal to get a list of all valid locales
    setlocale(LC_ALL, "en_US.UTF-8");
    ...
}
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • The printf´s are returning the expected lengths of the lines they are supposed to print, Yet nothing is printed in the console, not a new line, not single char, nothing, nada, nil. Ill try UTF8, and Ill get back to you. – whtlnv Mar 29 '12 at 03:29