printf()、fprintf()、wprintf()和NSlog()不会在XCode上打印。

时间:2022-09-07 07:42:30

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]
  • 步骤2:我将用它的超级脚本编写这个多项式。[一串数字]

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...

因此,我编写了一个小的C解析器,只需要用一种合适的格式来打印这个多项式,因为我需要一个wchar_t字符串,我将它连接在一起。在字符串完成后,我快速地在控制台上打印它,检查一切是否正常并继续运行。简单的对吧?我可没那么幸运…

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.

在运行过程中,我们可以看到temp大小:8打印出预期的次数,只有在调试的时候,如果我运行程序,我将会得到任意数量的打印输出。但在此之后,作为标题状态,wprintf、printf和fprintf不打印任何东西,但是len确实在每次调用之后更改它的大小。

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.

在调用函数中,(应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:在测试时)我使用NSLog来打印返回字符串,而我没有得到任何不包括日志部分的内容。

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

发生什么事情了?我完全不知所措。

Im on XCode 4.2 by the way.

顺便说一下,我在XCode 4.2上。

1 个解决方案

#1


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.

printf/wprintf的返回值是什么?如果你认为它没有打印任何东西?它应该在失败或1或更多的情况下返回-1,因为如果成功,它应该总是在描述字符串之后至少打印换行符。

  • 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)?
  • 如果返回1或更多,换行符会被打印吗?您是否尝试过将程序的输出设置为十六进制或xxd(1)的十六进制转储文件?
  • If it's returning -1, what is the value of errno?
  • 如果返回-1,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:

如果结果是printf在错误EILSEQ中失败,那么很可能发生的情况是,您的字符串中包含了一些非ascii字符,因为这些原因导致wcstombs(3)在默认的C语言环境中失败。在这种情况下,解决方案是使用setlocale(3)在程序启动时切换到UTF-8语言环境:

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");
    ...
}

#1


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.

printf/wprintf的返回值是什么?如果你认为它没有打印任何东西?它应该在失败或1或更多的情况下返回-1,因为如果成功,它应该总是在描述字符串之后至少打印换行符。

  • 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)?
  • 如果返回1或更多,换行符会被打印吗?您是否尝试过将程序的输出设置为十六进制或xxd(1)的十六进制转储文件?
  • If it's returning -1, what is the value of errno?
  • 如果返回-1,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:

如果结果是printf在错误EILSEQ中失败,那么很可能发生的情况是,您的字符串中包含了一些非ascii字符,因为这些原因导致wcstombs(3)在默认的C语言环境中失败。在这种情况下,解决方案是使用setlocale(3)在程序启动时切换到UTF-8语言环境:

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");
    ...
}