{
char *a, *b;
printf("%lx\n",(b-a));
}
Run Code Online (Sandbox Code Playgroud)
通常可行,实际上,我无法想象它会在32位或64位机器上发出警告或失败.但对于ANSI C和尺寸感知,这是正确的做法吗?我希望这些代码可以在每个平台上运行,包括非Unix和嵌入式系统.
在我正在开发的程序(Linux)中,我需要非常简单的基于文本的IPC.为此,使用标准输入/输出管道非常容易.我可以相信发送到进程'stdin的消息不能被任何人读取吗?另外,我可以相信,如果我将管道保持在标准输出端,只有我可以读取输出的内容吗?我只是想确保没有基于procfs的技巧可以从这些中读取.
我有这个代码在Python 2.5中运行良好,但在2.7中没有:
import sys
import traceback
try:
from io import StringIO
except:
from StringIO import StringIO
def CaptureExec(stmt):
oldio = (sys.stdin, sys.stdout, sys.stderr)
sio = StringIO()
sys.stdout = sys.stderr = sio
try:
exec(stmt, globals(), globals())
out = sio.getvalue()
except Exception, e:
out = str(e) + "\n" + traceback.format_exc()
sys.stdin, sys.stdout, sys.stderr = oldio
return out
print "%s" % CaptureExec("""
import random
print "hello world"
""")
Run Code Online (Sandbox Code Playgroud)
我得到:
string argument expected, got 'str' Traceback (most recent call last): File "D:\3.py", line 13, …
似乎当我运行以下代码时:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv)
{
int i=0;
setvbuf(stdout, NULL, _IOLBF,0);
while (1)
printf("%d ",i++);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
无论我为 setvbuf() 定义的大小如何,它都会以 1024 个字符为单位进行打印。问题是在这种情况下大小是否会产生某种影响以及 1024 个字符的定义来自哪里。
我可以轻松启动一个进程,重定向STD I/O但是如何重定向现有进程的STD I/O.
Process process = Process.GetProcessById(_RunningApplication.AttachProcessId);
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
string text = process.StandardOutput.ReadToEnd(); //This line blows up.
Run Code Online (Sandbox Code Playgroud)
例外:
StandardOut尚未重定向或进程尚未开始.
旁注:如果您知道如何在C/C++中执行此操作,我很乐意重新标记并接受.我只需要知道它是否可能.
基本上我想做的是为一个程序提供两个输出终端窗口.一个是显示程序正在执行的操作的日志,第二个将显示类似值的表.这些终端的所有输出都将由程序本身生成.用stdio在C中可以做到这一点吗?
我意识到这可能更适合GUI或ncurses,但我仍然很好奇是否可以这样做.
编辑:也许应该提一下我在linux上做这个(特别是xubuntu).
在编写更大的程序时,我偶然发现了彩色文本输出的小问题.这是一个更简单的程序,可以重现这个问题.
#include <stdio.h>
#define COL_RESET "\033[0m"
#define COL_BG_RED "\x1B[41m"
char *str = "the quick brown fox jumped over the lazy dog";
int main(int argc, char *argv[])
{
int i = 10;
while (i) {
puts(COL_BG_RED);
puts(str);
puts(COL_RESET);
puts(str);
i--;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在这是我运行程序时得到的:
第一次 - 预期的结果

第二次

如您所知,即使重置红色后,程序也会决定随机打印行.在新终端中启动时,它始终打印预期结果.除非我跑clear,否则无法保证输出不会像第二张图片那样受损.
在我正在使用的图片中xterm,虽然其他终端做同样的事情.
我该怎么做才能防止这种情况发生?
在C中似乎有许多不同的寻求方式:
fseek()fsetpos()fseeko()lseek()许多人似乎有*64()版本:
fseeko64()lseek64()更复杂的是,许多人似乎要求宏定义(如_LARGEFILE64_SOURCE或_GNU_SOURCE)可用或使用64位版本.
在Windows,Linux,Mac,BSD,Solaris等上使用ANSI C保证64位IO的最简单方法是什么?从什么时候开始,每个操作系统都支持它?
我在C99中编写了一个完整的应用程序,并在两个基于GNU/Linux的系统上进行了彻底的测试.当尝试使用Windows上的Visual Studio编译它导致应用程序行为不端时,我感到很惊讶.起初我无法断言出了什么问题,但我尝试使用VC调试器,然后我发现了与fscanf()声明的函数有关的差异stdio.h.
以下代码足以证明问题:
#include <stdio.h>
int main() {
unsigned num1, num2, num3;
FILE *file = fopen("file.bin", "rb");
fscanf(file, "%u", &num1);
fgetc(file); // consume and discard \0
fscanf(file, "%u", &num2);
fgetc(file); // ditto
fscanf(file, "%u", &num3);
fgetc(file); // ditto
fclose(file);
printf("%d, %d, %d\n", num1, num2, num3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
假设file.bin包含512\0256\0128\0:
$ hexdump -C file.bin
00000000 35 31 32 00 32 35 36 00 31 32 38 00 |512.256.128.|
Run Code Online (Sandbox Code Playgroud)
现在,当在Ubuntu机器上的GCC 4.8.4下编译时,生成的程序按预期读取数字并打印512, 256, 128 …
在标准 C 中,您如何可靠地检查写入标准 I/O 流的所有输出是否已成功保存到磁盘?
C 标准规定fclose成功时将返回 0,如果“检测到任何错误” ,则返回 EOF。
但这是否意味着“在fclose通话过程中检测到任何错误”?或者这是否意味着“自上次调用clearerr”以来检测到任何错误?
换句话说,程序只检查 的返回值就足够了fclose,还是还需要检查ferror?是否有任何实现,如果ferror返回非零,则后续调用fclose可能返回 0?
这同样适用于fflush:如果fflush返回 0,后续调用也ferror将返回 0,如果fflush返回 EOF,后续调用ferror将返回非零值是否总是这样?是否有任何实现不是这种情况?
(当然,我不考虑停电、小鬼等。是的,需要保证耐用性的程序应该使用fsync,但这超出了标准 C 的范围。)